remove unwrap and todo! from attribute parsing

This commit is contained in:
ConnorSkees 2020-04-02 21:23:23 -04:00
parent 6923869b7e
commit 6e8c226834

View File

@ -23,29 +23,22 @@ impl Attribute {
super_selector: &Selector, super_selector: &Selector,
) -> SassResult<SelectorKind> { ) -> SassResult<SelectorKind> {
devour_whitespace(toks); devour_whitespace(toks);
let attr = if let Some(t) = toks.next() { let attr = match toks.next().ok_or("Expected identifier.")?.kind {
match t.kind {
v @ 'a'..='z' | v @ 'A'..='Z' | v @ '-' | v @ '_' => { v @ 'a'..='z' | v @ 'A'..='Z' | v @ '-' | v @ '_' => {
format!("{}{}", v, eat_ident(toks, scope, super_selector)?) format!("{}{}", v, eat_ident(toks, scope, super_selector)?)
} }
'#' if toks.next().unwrap().kind == '{' => { '#' if toks.next().ok_or("Expected expression.")?.kind == '{' => {
parse_interpolation(toks, scope, super_selector)?.to_string() parse_interpolation(toks, scope, super_selector)?.to_string()
} }
q @ '"' | q @ '\'' => { q @ '"' | q @ '\'' => parse_quoted_string(toks, scope, q, super_selector)?.to_string(),
parse_quoted_string(toks, scope, q, super_selector)?.to_string()
}
_ => return Err("Expected identifier.".into()), _ => return Err("Expected identifier.".into()),
}
} else {
todo!()
}; };
devour_whitespace(toks); devour_whitespace(toks);
let kind = if let Some(t) = toks.next() { let kind = match toks.next().ok_or("expected \"{\".")?.kind {
match t.kind {
v @ 'a'..='z' | v @ 'A'..='Z' => { v @ 'a'..='z' | v @ 'A'..='Z' => {
match toks.next().unwrap().kind { match toks.next().ok_or("expected \"]\".")?.kind {
']' => {} ']' => {}
_ => return Err("expected \"]\".".into()), _ => return Err("expected \"]\".".into()),
} }
@ -71,13 +64,10 @@ impl Attribute {
'$' => AttributeKind::Suffix, '$' => AttributeKind::Suffix,
'*' => AttributeKind::Contains, '*' => AttributeKind::Contains,
_ => return Err("Expected \"]\".".into()), _ => return Err("Expected \"]\".".into()),
}
} else {
todo!()
}; };
if kind != AttributeKind::Equals { if kind != AttributeKind::Equals {
match toks.next().unwrap().kind { match toks.next().ok_or("expected \"=\".")?.kind {
'=' => {} '=' => {}
_ => return Err("expected \"=\".".into()), _ => return Err("expected \"=\".".into()),
} }
@ -85,24 +75,17 @@ impl Attribute {
devour_whitespace(toks); devour_whitespace(toks);
let value = if let Some(t) = toks.next() { let value = match toks.next().ok_or("Expected identifier.")?.kind {
match t.kind {
v @ 'a'..='z' | v @ 'A'..='Z' | v @ '-' | v @ '_' => { v @ 'a'..='z' | v @ 'A'..='Z' | v @ '-' | v @ '_' => {
format!("{}{}", v, eat_ident(toks, scope, super_selector)?) format!("{}{}", v, eat_ident(toks, scope, super_selector)?)
} }
q @ '"' | q @ '\'' => { q @ '"' | q @ '\'' => parse_quoted_string(toks, scope, q, super_selector)?.to_string(),
parse_quoted_string(toks, scope, q, super_selector)?.to_string()
}
_ => return Err("Expected identifier.".into()), _ => return Err("Expected identifier.".into()),
}
} else {
todo!()
}; };
devour_whitespace(toks); devour_whitespace(toks);
let modifier = if let Some(t) = toks.next() { let modifier = match toks.next().ok_or("expected \"]\".")?.kind {
match t.kind {
']' => { ']' => {
return Ok(SelectorKind::Attribute(Attribute { return Ok(SelectorKind::Attribute(Attribute {
kind, kind,
@ -112,16 +95,13 @@ impl Attribute {
})) }))
} }
v @ 'a'..='z' | v @ 'A'..='Z' => { v @ 'a'..='z' | v @ 'A'..='Z' => {
match toks.next().unwrap().kind { match toks.next().ok_or("expected \"]\".")?.kind {
']' => {} ']' => {}
_ => return Err("expected \"]\".".into()), _ => return Err("expected \"]\".".into()),
} }
Some(v) Some(v)
} }
_ => return Err("Expected \"]\".".into()), _ => return Err("Expected \"]\".".into()),
}
} else {
return Err("expected \"]\".".into());
}; };
Ok(SelectorKind::Attribute(Attribute { Ok(SelectorKind::Attribute(Attribute {