Skip to main content

harfrust/
error.rs

1use crate::{Direction, Script};
2
3/// The reason a call to [`shape`](crate::shape) could not produce glyphs.
4///
5/// Each of these is a misuse of the API. Running out of room is not among
6/// them: pathological input can provoke it, so it is reported through
7/// [`Buffer::allocation_successful`](crate::Buffer::allocation_successful)
8/// rather than as a failure to shape.
9#[derive(Clone, PartialEq, Eq, Hash, Debug)]
10#[non_exhaustive]
11pub enum ShapeError {
12    /// The buffer already holds the glyphs from an earlier call.
13    ///
14    /// To shape the same contents again, set the content type back to
15    /// [`ContentType::Unicode`](crate::ContentType::Unicode) with
16    /// [`set_content_type`](crate::Buffer::set_content_type); to shape something else,
17    /// clear the buffer and fill it afresh.
18    AlreadyShaped,
19
20    /// The buffer has no direction, so no plan could be built for it.
21    ///
22    /// Call [`guess_segment_properties`](crate::Buffer::guess_segment_properties) to
23    /// infer one from the contents, or set it with
24    /// [`set_direction`](crate::Buffer::set_direction).
25    DirectionUnset,
26
27    /// The buffer's direction is not the one the supplied plan was built for.
28    DirectionMismatch {
29        /// The direction the plan was built for.
30        plan: Direction,
31        /// The direction the buffer carries.
32        buffer: Direction,
33    },
34
35    /// The buffer's script is not the one the supplied plan was built for.
36    ScriptMismatch {
37        /// The script the plan was built for.
38        plan: Script,
39        /// The script the buffer carries.
40        buffer: Script,
41    },
42}
43
44impl core::fmt::Display for ShapeError {
45    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
46        match self {
47            Self::AlreadyShaped => fmt.write_str("buffer already holds shaped glyphs"),
48            Self::DirectionUnset => fmt.write_str("buffer has no direction set"),
49            Self::DirectionMismatch { plan, buffer } => write!(
50                fmt,
51                "buffer direction does not match plan direction: {buffer:?} != {plan:?}"
52            ),
53            Self::ScriptMismatch { plan, buffer } => write!(
54                fmt,
55                "buffer script does not match plan script: {buffer:?} != {plan:?}"
56            ),
57        }
58    }
59}
60
61#[cfg(feature = "std")]
62impl std::error::Error for ShapeError {}