Skip to content

Make it easier to get a TextPos from an error #7

Closed
@cormacrelf

Description

@cormacrelf

Hi, love your work on this, I'm using it to parse CSL for a new citation processor and it's fantastic.

CSL is mostly written by hand and involves big enums of allowed attribute values, so it's important to point out when and where these mishaps happen. I use the TextPos embedded in each variant of roxmltree::Error to produce a codespan for each error, and this works well.

However, it's probably better that this big match statement lives alongside its definition so other users can benefit and get the TextPos easily, and without having to update if roxmltree adds new variants. It would probably live on impl Error. Here is my source:

fn get_pos(e: &Error) -> TextPos {
    use xmlparser::Error as XP;
    match *e {
        Error::InvalidXmlPrefixUri(pos) => pos,
        Error::UnexpectedXmlUri(pos) => pos,
        Error::UnexpectedXmlnsUri(pos) => pos,
        Error::InvalidElementNamePrefix(pos) => pos,
        Error::DuplicatedNamespace(ref _name, pos) => pos,
        Error::UnexpectedCloseTag { pos, .. } => pos,
        Error::UnexpectedEntityCloseTag(pos) => pos,
        Error::UnknownEntityReference(ref _name, pos) => pos,
        Error::EntityReferenceLoop(pos) => pos,
        Error::DuplicatedAttribute(ref _name, pos) => pos,
        Error::ParserError(ref err) => match *err {
            XP::InvalidToken(_, pos, _) => pos,
            XP::UnexpectedToken(_, pos) => pos,
            XP::UnknownToken(pos) => pos,
        },
        _ => TextPos::new(1, 1)
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions