use crate::common::syntax::{parse_arguments, Mode};
use std::borrow::Cow;
use thiserror::Error;
use yash_env::semantics::Field;
use yash_env::Env;
use yash_syntax::source::pretty::{Annotation, AnnotationType, MessageBase};
#[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 MessageBase for Error {
fn message_title(&self) -> Cow<str> {
self.to_string().into()
}
fn main_annotation(&self) -> Annotation<'_> {
use Error::*;
match self {
CommonError(e) => e.main_annotation(),
UnexpectedOperands(operands) => Annotation::new(
AnnotationType::Error,
format!("{}: unexpected operand", operands[0].value).into(),
&operands[0].origin,
),
}
}
}
pub fn parse(env: &Env, 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))
}
}