Skip to main content

ephemeris_documents/error/
value.rs

1//! Errors from values a format cannot express.
2
3use std::fmt;
4
5use crate::format::Format;
6
7/// A capability a format may lack.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum Feature {
10    /// Null values.
11    Null,
12    /// Integers outside the signed 64-bit range.
13    LargeInteger,
14    /// Literal strings, or text the format cannot reproduce as one.
15    LiteralString,
16    /// Comments.
17    Comment,
18    /// Comments inside inline tables.
19    InlineComment,
20    /// Dotted tables.
21    DottedTable,
22}
23
24impl fmt::Display for Feature {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        f.write_str(match self {
27            Self::Null => "null values",
28            Self::LargeInteger => "integers above the signed 64-bit range",
29            Self::LiteralString => "literal strings",
30            Self::Comment => "comments",
31            Self::InlineComment => "comments in inline tables",
32            Self::DottedTable => "dotted tables",
33        })
34    }
35}
36
37/// Error returned while shaping a generic value for a structured format.
38#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
39pub enum ValueError {
40    /// A structured value contains a non-finite number.
41    #[error("value contains a non-finite number, which cannot be rendered")]
42    NonFinite,
43    /// An integer is outside the `i64` and `u64` ranges.
44    #[error("value contains an integer outside the 64-bit range")]
45    IntegerOutOfRange,
46    /// A value is of a kind documents cannot represent, such as bytes or a
47    /// date.
48    #[error("value contains a kind of value that documents cannot represent")]
49    UnsupportedKind,
50    /// A value read from a document is not a string.
51    #[error("value is not a string")]
52    NotString,
53    /// The format cannot represent a requested feature.
54    #[error("{format} does not support {feature}")]
55    Unsupported {
56        /// The structured format that rejected the value.
57        format: Format,
58        /// The feature the format lacks.
59        feature: Feature,
60    },
61}