2023-07-31 15:46:39 +02:00
|
|
|
//! This contains error and result types that can happen while parsing
|
|
|
|
//! Derivations from ATerm.
|
|
|
|
use nom::IResult;
|
|
|
|
|
|
|
|
use crate::nixhash;
|
|
|
|
|
|
|
|
pub type NomResult<I, O> = IResult<I, O, NomError<I>>;
|
|
|
|
|
2023-10-15 10:29:48 +02:00
|
|
|
#[derive(Debug, thiserror::Error, PartialEq)]
|
2023-07-31 15:46:39 +02:00
|
|
|
pub enum ErrorKind {
|
2023-10-15 10:29:48 +02:00
|
|
|
/// duplicate key in map
|
|
|
|
#[error("duplicate map key: {0}")]
|
2023-07-31 15:46:39 +02:00
|
|
|
DuplicateMapKey(String),
|
|
|
|
|
2023-10-15 10:29:48 +02:00
|
|
|
/// 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}")]
|
2023-07-31 15:46:39 +02:00
|
|
|
NixHashError(nixhash::Error),
|
|
|
|
|
2023-10-15 10:29:48 +02:00
|
|
|
#[error("nom error: {0:?}")]
|
2023-07-31 15:46:39 +02:00
|
|
|
Nom(nom::error::ErrorKind),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Our own error type to pass along parser-related errors.
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct NomError<I> {
|
|
|
|
/// position of the error in the input data
|
|
|
|
pub input: I,
|
|
|
|
/// error code
|
|
|
|
pub code: ErrorKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I, E> nom::error::FromExternalError<I, E> for NomError<I> {
|
|
|
|
fn from_external_error(input: I, kind: nom::error::ErrorKind, _e: E) -> Self {
|
|
|
|
Self {
|
|
|
|
input,
|
|
|
|
code: ErrorKind::Nom(kind),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<I> nom::error::ParseError<I> for NomError<I> {
|
|
|
|
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<I> From<nom::error::Error<I>> for NomError<I> {
|
|
|
|
fn from(value: nom::error::Error<I>) -> Self {
|
|
|
|
Self {
|
|
|
|
input: value.input,
|
|
|
|
code: ErrorKind::Nom(value.code),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This essentially implements
|
2023-12-21 16:36:44 +01:00
|
|
|
/// `From<nom::Err<nom::error::Error<I>>>` for `nom::Err<NomError<I>>`,
|
|
|
|
/// which we can't because `nom::Err<_>` is a foreign type.
|
2023-07-31 15:46:39 +02:00
|
|
|
pub(crate) fn into_nomerror<I>(e: nom::Err<nom::error::Error<I>>) -> nom::Err<NomError<I>> {
|
|
|
|
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()),
|
|
|
|
}
|
|
|
|
}
|