ferrochart_core/error.rs
1//! Error type of the public API. Hand-written in `thiserror` style; the core does not
2//! depend on `thiserror`.
3//!
4
5use core::fmt;
6
7/// Errors returned by the `ferrochart-core` public API. The public API never panics.
8#[derive(Clone, Debug, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum Error {
11 /// An argument is outside its valid domain. The payload names the argument and
12 /// the violated condition.
13 InvalidArgument(&'static str),
14 /// A time column is not strictly increasing. `index` is the position in the
15 /// input where `time[index] <= time[index - 1]`. When the input itself is sorted
16 /// but does not fit the existing data, it is `0` for an append (first element
17 /// not after the last existing time) and `len - 1` for a prepend (last element
18 /// not before the first existing time).
19 TimeNotIncreasing {
20 /// Position of the offending element in the input.
21 index: usize,
22 },
23 /// The input columns do not all have the same length. `column` names the
24 /// first column whose length differs from `time`.
25 LengthMismatch {
26 /// Name of the offending column.
27 column: &'static str,
28 },
29 /// Bar `index` of the input violates the OHLC constraints (a non-finite value,
30 /// or `low > min(open, close)` / `high < max(open, close)` in `Reject` mode).
31 InvalidOhlc {
32 /// Position of the offending bar in the input.
33 index: usize,
34 },
35 /// No series, pane or other object with this id exists.
36 UnknownId,
37 /// The operation is not supported (yet) for this kind of object. The payload
38 /// explains what is missing.
39 Unsupported(&'static str),
40 /// A required allocation could not be satisfied.
41 OutOfMemory,
42}
43
44impl fmt::Display for Error {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 match self {
47 Self::InvalidArgument(what) => write!(f, "invalid argument: {what}"),
48 Self::TimeNotIncreasing { index } => {
49 write!(f, "time is not strictly increasing at index {index}")
50 }
51 Self::LengthMismatch { column } => {
52 write!(f, "column `{column}` has a different length than `time`")
53 }
54 Self::InvalidOhlc { index } => write!(f, "invalid OHLC values in bar {index}"),
55 Self::UnknownId => f.write_str("unknown id"),
56 Self::Unsupported(what) => write!(f, "unsupported: {what}"),
57 Self::OutOfMemory => f.write_str("out of memory"),
58 }
59 }
60}
61
62impl std::error::Error for Error {}
63
64/// `Result` with [`Error`] as the default error type.
65pub type Result<T, E = Error> = core::result::Result<T, E>;