Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adjust ParseError for indicating if an urn format was expected #397

Merged
merged 2 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ impl Uuid {
expected: "0123456789abcdefABCDEF-",
found: input[i_char..].chars().next().unwrap(),
index: i_char,
urn: parser::UrnPrefix::Optional,
});
}
}
Expand Down Expand Up @@ -875,6 +876,7 @@ impl Uuid {
expected: "0123456789abcdefABCDEF-",
found: input[i_char..].chars().next().unwrap(),
index: i_char,
urn: parser::UrnPrefix::Optional,
});
}
}
Expand Down Expand Up @@ -1066,6 +1068,7 @@ mod tests {
expected: EXPECTED_CHARS,
found: 'G',
index: 20,
urn: parser::UrnPrefix::Optional,
})
);

Expand Down Expand Up @@ -1107,6 +1110,7 @@ mod tests {
expected: EXPECTED_CHARS,
found: 'X',
index: 18,
urn: parser::UrnPrefix::Optional,
})
);

Expand Down Expand Up @@ -1159,6 +1163,7 @@ mod tests {
expected: EXPECTED_CHARS,
found: '%',
index: 15,
urn: parser::UrnPrefix::Optional,
})
);

Expand Down Expand Up @@ -1212,6 +1217,7 @@ mod tests {
expected: EXPECTED_CHARS,
found: 'X',
index: 6,
urn: parser::UrnPrefix::Optional,
})
);
assert_eq!(
Expand Down
17 changes: 16 additions & 1 deletion src/parser/core_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,23 @@ impl fmt::Display for parser::ParseError {
expected,
found,
index,
urn,
} => {
write!(f, "expected {}, found {} at {}", expected, found, index)
let urn_str = match urn {
parser::UrnPrefix::None => "",
parser::UrnPrefix::Optional => {
" an optional prefix of `urn:uuid:` followed by"
}
parser::UrnPrefix::Required => {
" a prefix of `urn:uuid` followed by"
}
};

write!(
f,
"expected{} {}, found {} at {}",
urn_str, expected, found, index
)
}
parser::ParseError::InvalidGroupCount {
ref expected,
Expand Down
17 changes: 17 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ pub enum Expected {
},
}

/// Urn prefix value.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum UrnPrefix {
/// No `urn:uuid:` prefix should be provided.
None,
/// The `urn:uuid:` prefix should optionally provided.
Optional,
/// The `urn:uuid:` prefix is required.
Required,
}

/// An error that can occur while parsing a [`Uuid`] string.
///
/// [`Uuid`]: ../struct.Uuid.html
Expand All @@ -48,6 +59,12 @@ pub enum ParseError {
found: char,
/// The invalid character position.
index: usize,
/// Indicates the [`Uuid`] starts with `urn:uuid:`.
///
/// This is a special case for [`Urn`] adapter parsing.
///
/// [`Uuid`]: ../Uuid.html
urn: UrnPrefix,
},
/// Invalid number of segments in the [`Uuid`] string.
///
Expand Down