Skip to main content

devela/media/font/format/bdf/
error.rs

1// devela/src/media/font/format/bdf/error.rs
2//
3//! Defines [`BdfError`].
4//
5
6use crate::{Debug, Display, Error, FmtResult, Formatter, Version, write};
7
8#[doc = crate::_tags!(font error_composite)]
9/// An error encountered while parsing BDF data.
10#[doc = crate::_doc_meta!{location("media/font")}]
11#[non_exhaustive]
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
13pub enum BdfError {
14    /// The input ended before a required BDF directive.
15    UnexpectedEof {
16        /// One-based line at which the directive was expected.
17        line: u32,
18    },
19
20    /// A directive was absent, misplaced, duplicated, or not recognized.
21    UnexpectedDirective {
22        /// One-based source line.
23        line: u32,
24    },
25
26    /// A directive contained a malformed or out-of-range value.
27    InvalidValue {
28        /// One-based source line.
29        line: u32,
30    },
31
32    /// The declared BDF version is syntactically valid but unsupported.
33    UnsupportedVersion(Version),
34}
35impl Error for BdfError {}
36impl Display for BdfError {
37    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult<()> {
38        match *self {
39            Self::UnexpectedEof { line } => {
40                write!(f, "unexpected end of BDF data at line {line}")
41            }
42            Self::UnexpectedDirective { line } => {
43                write!(f, "unexpected BDF directive at line {line}")
44            }
45            Self::InvalidValue { line } => {
46                write!(f, "invalid BDF value at line {line}")
47            }
48            Self::UnsupportedVersion(version) => {
49                write!(f, "unsupported BDF version {version}")
50            }
51        }
52    }
53}
54impl BdfError {
55    pub(crate) const fn invalid_value(line: u32) -> Self {
56        Self::InvalidValue { line }
57    }
58    pub(crate) const fn unexpected_directive(line: u32) -> Self {
59        Self::UnexpectedDirective { line }
60    }
61    pub(crate) const fn unexpected_eof(line: u32) -> Self {
62        Self::UnexpectedEof { line }
63    }
64}