use crate::common::syntax::{Mode, parse_arguments};
use thiserror::Error;
use yash_env::Env;
use yash_env::semantics::Field;
use yash_env::source::pretty::{Report, ReportType, Snippet};
#[derive(Clone, Debug, Eq, Error, PartialEq)]
#[non_exhaustive]
pub enum Error {
#[error(transparent)]
CommonError(#[from] crate::common::syntax::ParseError<'static>),
#[error("unexpected operand")]
UnexpectedOperands(Vec<Field>),
}
impl Error {
#[must_use]
pub fn to_report(&self) -> Report<'_> {
match self {
Self::CommonError(e) => e.to_report(),
Self::UnexpectedOperands(operands) => {
let mut report = Report::new();
report.r#type = ReportType::Error;
report.title = "unexpected operand".into();
report.snippets = Snippet::with_primary_span(
&operands[0].origin,
format!("{}: unexpected operand", operands[0]).into(),
);
report
}
}
}
}
impl<'a> From<&'a Error> for Report<'a> {
#[inline]
fn from(error: &'a Error) -> Self {
error.to_report()
}
}
pub fn parse<S>(env: &Env<S>, args: Vec<Field>) -> Result<(), Error> {
let (options, operands) = parse_arguments(&[], Mode::with_env(env), args)?;
debug_assert_eq!(options, []);
if operands.is_empty() {
Ok(())
} else {
Err(Error::UnexpectedOperands(operands))
}
}