use ::devise::syn;
#[derive(Debug)]
pub struct StrMatch {
pub lit: syn::LitStr,
}
#[derive(Debug)]
pub struct CaptureLookahead {
pub field: syn::Ident,
pub lookahead: syn::LitStr,
}
#[derive(Debug)]
pub struct Capture {
pub field: syn::Ident,
}
#[derive(Debug)]
pub enum Section {
StrMatch(StrMatch),
CaptureLookahead(CaptureLookahead),
Capture(Capture),
}
#[derive(Debug)]
pub struct Fmt {
pub sections: Vec<Section>,
}
impl syn::parse::Parse for Fmt {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let mut sections = vec![];
loop {
if input.is_empty() {
break;
}
let next = input.lookahead1();
if next.peek(syn::Lit) {
match input.parse().unwrap() {
syn::Lit::Str(lit) => {
let section = StrMatch { lit };
sections.push(Section::StrMatch(section));
}
_ => return Err(next.error()),
}
} else if next.peek(syn::Ident) {
let field: syn::Ident = input.parse().unwrap();
if input.is_empty() {
let section = Capture { field };
sections.push(Section::Capture(section));
continue;
}
let lookahead = input.lookahead1();
if lookahead.peek(syn::Lit) {
match input.parse().unwrap() {
syn::Lit::Str(lit) => {
let section = CaptureLookahead {
field,
lookahead: lit,
};
sections.push(Section::CaptureLookahead(section));
}
_ => return Err(lookahead.error()),
}
} else {
return Err(lookahead.error());
}
} else {
return Err(next.error());
}
}
Ok(Self { sections })
}
}