Skip to main content

lichtung_plot_ndarray/
error.rs

1use std::collections::TryReserveError;
2
3/// A structural ndarray binding failure, distinct from native scientific validation.
4#[derive(Debug, thiserror::Error)]
5pub enum AdapterError {
6    #[error("{operation}: expected shape {expected:?}, got {actual:?}")]
7    Shape {
8        operation: &'static str,
9        expected: Vec<usize>,
10        actual: Vec<usize>,
11    },
12    #[error("{role}: column {index} requires an array source")]
13    MissingSource { role: &'static str, index: usize },
14    #[error("{role}: column {index} is outside {columns} logical columns")]
15    Column {
16        role: &'static str,
17        index: usize,
18        columns: usize,
19    },
20    #[error("{role}: element type {element} is not supported{context}", context = .column.map_or_else(String::new, |index| format!(" for logical column {index}")))]
21    UnsupportedElement {
22        role: &'static str,
23        element: &'static str,
24        column: Option<usize>,
25    },
26    #[error("{role}: {detail}")]
27    Binding { role: &'static str, detail: String },
28    #[error(
29        "source-row ID {value} at logical slot {slot}{context} must be present, nonnegative and fit usize", context = .column.map_or_else(String::new, |index| format!(" in logical column {index}"))
30    )]
31    SourceRow {
32        slot: usize,
33        value: String,
34        column: Option<usize>,
35    },
36    #[error("{operation}: cannot reserve conversion storage")]
37    Allocation {
38        operation: &'static str,
39        #[source]
40        source: TryReserveError,
41    },
42    #[error("{0}")]
43    Native(#[from] crate::Error),
44}
45
46pub(crate) type BindingResult<T> = std::result::Result<T, AdapterError>;
47
48pub(crate) fn binding(role: &'static str, detail: impl Into<String>) -> AdapterError {
49    AdapterError::Binding {
50        role,
51        detail: detail.into(),
52    }
53}
54pub(crate) fn length(
55    role: &'static str,
56    name: &str,
57    expected: usize,
58    actual: usize,
59) -> BindingResult<()> {
60    if actual != expected {
61        use crate::errors::{Diagnostic, DiagnosticKind, ErrorContext, OperationPhase};
62        return Err(crate::Error::Data(
63            Diagnostic::new(
64                DiagnosticKind::ShapeMismatch {
65                    expected: vec![expected],
66                    actual: vec![actual],
67                },
68                format!("{role}: {name:?}: expected {expected} slots, got {actual}"),
69            )
70            .with_context(ErrorContext::new().with_operation(OperationPhase::Bind)),
71        )
72        .into());
73    }
74    Ok(())
75}
76pub(crate) fn unsupported<T>(role: &'static str) -> AdapterError {
77    AdapterError::UnsupportedElement {
78        role,
79        element: std::any::type_name::<T>(),
80        column: None,
81    }
82}
83
84pub(crate) fn unsupported_column<T>(role: &'static str, index: usize) -> AdapterError {
85    AdapterError::UnsupportedElement {
86        role,
87        element: std::any::type_name::<T>(),
88        column: Some(index),
89    }
90}