//! This contains error and result types that can happen while parsing //! Derivations from ATerm. use nom::IResult; use crate::{nixhash, store_path}; pub type NomResult = IResult>; #[derive(Debug, thiserror::Error, PartialEq)] pub enum ErrorKind { /// duplicate key in map #[error("duplicate map key: {0}")] DuplicateMapKey(String), /// Input derivation has two outputs with the same name #[error("duplicate output name {1} for input derivation {0}")] DuplicateInputDerivationOutputName(String, String), #[error("duplicate input source: {0}")] DuplicateInputSource(String), #[error("nix hash error: {0}")] NixHashError(nixhash::Error), #[error("store path error: {0}")] StorePathError(#[from] store_path::Error), #[error("nom error: {0:?}")] Nom(nom::error::ErrorKind), } /// Our own error type to pass along parser-related errors. #[derive(Debug, PartialEq)] pub struct NomError { /// position of the error in the input data pub input: I, /// error code pub code: ErrorKind, } impl nom::error::FromExternalError for NomError { fn from_external_error(input: I, kind: nom::error::ErrorKind, _e: E) -> Self { Self { input, code: ErrorKind::Nom(kind), } } } impl nom::error::ParseError for NomError { fn from_error_kind(input: I, kind: nom::error::ErrorKind) -> Self { Self { input, code: ErrorKind::Nom(kind), } } // FUTUREWORK: implement, so we have support for backtracking through the // parse tree? fn append(_input: I, _kind: nom::error::ErrorKind, other: Self) -> Self { other } } /// This wraps a [nom::error::Error] into our error. impl From> for NomError { fn from(value: nom::error::Error) -> Self { Self { input: value.input, code: ErrorKind::Nom(value.code), } } } /// This essentially implements /// `From>>` for `nom::Err>`, /// which we can't because `nom::Err<_>` is a foreign type. pub(crate) fn into_nomerror(e: nom::Err>) -> nom::Err> { match e { nom::Err::Incomplete(n) => nom::Err::Incomplete(n), nom::Err::Error(e) => nom::Err::Error(e.into()), nom::Err::Failure(e) => nom::Err::Failure(e.into()), } }