lichtung_plot_ndarray/
error.rs1use std::collections::TryReserveError;
2
3#[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 return Err(binding(
62 role,
63 format!("{name:?}: expected {expected} slots, got {actual}"),
64 ));
65 }
66 Ok(())
67}
68pub(crate) fn unsupported<T>(role: &'static str) -> AdapterError {
69 AdapterError::UnsupportedElement {
70 role,
71 element: std::any::type_name::<T>(),
72 column: None,
73 }
74}
75
76pub(crate) fn unsupported_column<T>(role: &'static str, index: usize) -> AdapterError {
77 AdapterError::UnsupportedElement {
78 role,
79 element: std::any::type_name::<T>(),
80 column: Some(index),
81 }
82}