Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ae38ed

Browse files
committed
[url] infrastructure for nom errors, taken from git-object
1 parent 60aacf0 commit 0ae38ed

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

git-url/src/lib.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#![forbid(unsafe_code)]
22

3+
/// For convenience to allow using `bstr` without adding it to own cargo manifest
4+
pub use bstr;
5+
36
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
47
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
58
pub enum Protocol {
@@ -35,12 +38,38 @@ pub use borrowed::Url as Borrowed;
3538

3639
pub mod parse {
3740
use crate::borrowed;
41+
use nom::error::ParseError;
3842
use quick_error::quick_error;
3943

4044
quick_error! {
4145
#[derive(Debug)]
4246
pub enum Error {
43-
TBD
47+
NomDetail(input: bstr::BString, msg: &'static str) {
48+
display("{}: '{}' could not be parsed", msg, input)
49+
}
50+
}
51+
}
52+
53+
impl Error {
54+
fn set_parse_context(mut self, ctx: &'static str) -> Self {
55+
if let Error::NomDetail(_, ref mut message) = self {
56+
*message = ctx
57+
}
58+
self
59+
}
60+
61+
pub(crate) fn context(msg: &'static str) -> impl Fn(nom::Err<Self>) -> nom::Err<Self> {
62+
move |e: nom::Err<Self>| e.map(|e| e.set_parse_context(msg))
63+
}
64+
}
65+
66+
impl ParseError<&[u8]> for Error {
67+
fn from_error_kind(input: &[u8], _kind: nom::error::ErrorKind) -> Self {
68+
Error::NomDetail(input.into(), "parse error")
69+
}
70+
71+
fn append(_: &[u8], _: nom::error::ErrorKind, other: Self) -> Self {
72+
other
4473
}
4574
}
4675

0 commit comments

Comments
 (0)