rusmpp_extra/encoding/ucs2/
errors.rs

1use crate::concatenation::MAX_PARTS;
2
3/// Errors that can occur during UCS2 encoding.
4#[derive(Debug, thiserror::Error, PartialEq, Eq)]
5pub enum Ucs2EncodeError {
6    /// Input contains un-encodable character.
7    #[error("Input contains un-encodable character")]
8    UnencodableCharacter,
9}
10
11/// Errors that can occur during UCS2 concatenation.
12#[derive(Debug, thiserror::Error, PartialEq, Eq)]
13pub enum Ucs2ConcatenateError {
14    /// Encoding error.
15    #[error("Encoding error: {0}")]
16    Encode(
17        #[from]
18        #[source]
19        Ucs2EncodeError,
20    ),
21    /// Part cannot fit even a single character.
22    ///
23    /// This error is returned when `max_message_size - part_header_size == 0`.
24    #[error(
25        "Cannot fit even a single character into a part with the given header and size constraints"
26    )]
27    PartCapacityExceeded,
28    /// A part would end with a leading surrogate, which is not allowed unless allow_split_character=true.
29    ///
30    /// This error might be returned when `max_message_size - part_header_size < 2 && allow_split_character == false`.
31    #[error(
32        "A part would end with a leading surrogate, which is not allowed unless allow_split_character=true"
33    )]
34    InvalidBoundary,
35    #[error("The number of parts exceeds the maximum allowed. actual: {actual}, max: {max}")]
36    /// The number of parts exceeds the maximum allowed.
37    PartsCountExceeded {
38        /// The maximum allowed number of parts.
39        max: usize,
40        /// The actual number of parts.
41        actual: usize,
42    },
43}
44
45impl Ucs2ConcatenateError {
46    pub(crate) const fn parts_count_exceeded(actual: usize) -> Self {
47        Self::PartsCountExceeded {
48            max: MAX_PARTS,
49            actual,
50        }
51    }
52}