Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/kleene

crates/kleene-plan/src/error.rs

use std::fmt;

use datafusion::error::DataFusionError;
use kleene_graph::GraphError;
use kleene_sparse::SparseError;

/// Why a plan could not be built, planned or run.
#[derive(Debug)]
#[non_exhaustive]
pub enum PlanError {
    /// A rule the plan refuses (head on an input, an unbound head variable, an atom of the
    /// wrong width, ...).
    Program(String),
    /// A construct that has a seam here but comes in a later phase.
    NotYet(&'static str),
    /// An input that is missing or does not fit its relation.
    Input(String),
    /// DataFusion failed.
    DataFusion(DataFusionError),
    /// The graph layer refused.
    Graph(GraphError),
    /// The sparse layer refused.
    Sparse(SparseError),
    /// A plan asked to run split over parts ([`crate::Engine::split`]) with a stratum that
    /// does not split ([`crate::Stratum::splittable`]); it runs on one machine.
    NotSplittable(String),
    /// A table of sets ([`crate::Plan::sets`]) would number more sets than its limit: the
    /// run stops, with no part answer.
    SetLimit {
        /// The table's name.
        table: String,
        /// Its limit.
        limit: u64,
    },
}

impl fmt::Display for PlanError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PlanError::Program(m) => write!(f, "program: {m}"),
            PlanError::NotYet(m) => write!(f, "not in this phase: {m}"),
            PlanError::Input(m) => write!(f, "input: {m}"),
            PlanError::DataFusion(e) => write!(f, "datafusion: {e}"),
            PlanError::Graph(e) => write!(f, "graph: {e}"),
            PlanError::Sparse(e) => write!(f, "sparse: {e}"),
            PlanError::NotSplittable(m) => write!(f, "not splittable: {m}"),
            PlanError::SetLimit { table, limit } => {
                write!(f, "sets `{table}`: more than {limit} sets")
            }
        }
    }
}

impl std::error::Error for PlanError {}

impl From<DataFusionError> for PlanError {
    fn from(e: DataFusionError) -> Self {
        PlanError::DataFusion(e)
    }
}

impl From<GraphError> for PlanError {
    fn from(e: GraphError) -> Self {
        PlanError::Graph(e)
    }
}

impl From<SparseError> for PlanError {
    fn from(e: SparseError) -> Self {
        PlanError::Sparse(e)
    }
}

impl From<datafusion::arrow::error::ArrowError> for PlanError {
    fn from(e: datafusion::arrow::error::ArrowError) -> Self {
        PlanError::DataFusion(e.into())
    }
}