macro: add spans to more errors

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-07-18 14:59:09 +02:00
parent f340968773
commit c54afe1be4

View File

@ -200,6 +200,13 @@ impl IntoIterator for Object {
} }
impl Expression { impl Expression {
pub fn span(&self) -> Span {
match self {
Expression::Expr(expr) => expr.span(),
Expression::Object(obj) => obj.span(),
}
}
pub fn expect_lit_str(self) -> Result<syn::LitStr, Error> { pub fn expect_lit_str(self) -> Result<syn::LitStr, Error> {
match self { match self {
Expression::Expr(expr) => match expr { Expression::Expr(expr) => match expr {
@ -209,7 +216,7 @@ impl Expression {
}, },
other => bail!("expected string literal, got: {:?}", other), other => bail!("expected string literal, got: {:?}", other),
}, },
_ => bail!("expected string literal"), other => c_bail!(other.span(), "expected string literal"),
} }
} }
@ -222,7 +229,7 @@ impl Expression {
}, },
other => bail!("expected boolean literal, got: {:?}", other), other => bail!("expected boolean literal, got: {:?}", other),
}, },
_ => bail!("expected boolean literal"), other => c_bail!(other.span(), "expected boolean literal"),
} }
} }
@ -235,21 +242,21 @@ impl Expression {
}, },
other => bail!("expected boolean literal, got: {:?}", other), other => bail!("expected boolean literal, got: {:?}", other),
}, },
_ => bail!("expected boolean literal"), other => c_bail!(other.span(), "expected boolean literal"),
} }
} }
pub fn expect_expr(self) -> Result<syn::Expr, Error> { pub fn expect_expr(self) -> Result<syn::Expr, Error> {
match self { match self {
Expression::Expr(expr) => Ok(expr), Expression::Expr(expr) => Ok(expr),
_ => bail!("expected expression, found {:?}", self), other => c_bail!(other.span(), "expected expression, found {:?}", other),
} }
} }
pub fn expect_object(self) -> Result<Object, Error> { pub fn expect_object(self) -> Result<Object, Error> {
match self { match self {
Expression::Object(obj) => Ok(obj), Expression::Object(obj) => Ok(obj),
_ => bail!("expected object, found an expression"), other => c_bail!(other.span(), "expected object, found an expression"),
} }
} }
@ -259,7 +266,7 @@ impl Expression {
Expr::Path(path) => Ok(path), Expr::Path(path) => Ok(path),
other => bail!("expected a type name, got {:?}", other), other => bail!("expected a type name, got {:?}", other),
}, },
_ => bail!("expected a type name, got {:?}", self), other => c_bail!(other.span(), "expected a type name, got {:?}", other),
} }
} }