summaryrefslogtreecommitdiffstats
path: root/src/string.rs
blob: 9c681f6e53dc172c889913346df2a902afed2408 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
//   This Source Code Form is subject to the terms of the Mozilla Public
//   License, v. 2.0. If a copy of the MPL was not distributed with this
//   file, You can obtain one at http://mozilla.org/MPL/2.0/.
//

#[derive(Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct MqttString(String);

impl MqttString {
    pub const MAX_LEN: usize = u16::MAX as usize;
}

impl std::str::FromStr for MqttString {
    type Err = MqttStringError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.len() > Self::MAX_LEN {
            Err(MqttStringError::TooLong(s.len()))
        } else {
            Ok(Self(s.to_string()))
        }
    }
}

impl TryFrom<&str> for MqttString {
    type Error = MqttStringError;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        <MqttString as std::str::FromStr>::from_str(s)
    }
}

impl TryFrom<String> for MqttString {
    type Error = MqttStringError;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        if s.len() > Self::MAX_LEN {
            Err(MqttStringError::TooLong(s.len()))
        } else {
            Ok(Self(s.to_string()))
        }
    }
}

impl AsRef<str> for MqttString {
    fn as_ref(&self) -> &str {
        self.0.as_ref()
    }
}

impl std::fmt::Debug for MqttString {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

#[derive(Debug, thiserror::Error)]
pub enum MqttStringError {
    #[error("String of length {} is too long, max length is {}", .0, MqttString::MAX_LEN)]
    TooLong(usize),
}