tribles/value/schemas/
shortstring.rs

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use crate::id::Id;
use crate::id_hex;
use crate::value::{FromValue, ToValue, TryFromValue, TryToValue, Value, ValueSchema};

use indxvec::Printing;
use std::str::Utf8Error;

/// An error that occurs when converting a string to a short string.
/// This error occurs when the string is too long or contains an interior NUL byte.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FromStrError {
    TooLong,
    InteriorNul,
}

/// A value schema for a short string.
/// A short string is a UTF-8 encoded string with a maximum length of 32 bytes (inclusive)
/// The string is null-terminated.
/// If the string is shorter than 32 bytes, the remaining bytes are zero.
/// If the string is exactly 32 bytes, then there is no zero terminator.
pub struct ShortString;

impl ValueSchema for ShortString {
    const VALUE_SCHEMA_ID: Id = id_hex!("2D848DB0AF112DB226A6BF1A3640D019");
}

impl<'a> TryFromValue<'a, ShortString> for &'a str {
    type Error = Utf8Error;

    fn try_from_value(v: &'a Value<ShortString>) -> Result<&'a str, Self::Error> {
        std::str::from_utf8(
            &v.bytes[0..v
                .bytes
                .iter()
                .position(|&b| b == 0)
                .unwrap_or(v.bytes.len())],
        )
    }
}

impl<'a> TryFromValue<'a, ShortString> for String {
    type Error = Utf8Error;

    fn try_from_value(v: &Value<ShortString>) -> Result<Self, Self::Error> {
        let s: &str = v.try_from_value()?;
        Ok(s.to_string())
    }
}

impl<'a> FromValue<'a, ShortString> for &'a str {
    fn from_value(v: &'a Value<ShortString>) -> Self {
        v.try_from_value().unwrap()
    }
}

impl<'a> FromValue<'a, ShortString> for String {
    fn from_value(v: &'a Value<ShortString>) -> Self {
        v.try_from_value().unwrap()
    }
}

impl TryToValue<ShortString> for &str {
    type Error = FromStrError;

    fn try_to_value(self) -> Result<Value<ShortString>, Self::Error> {
        let bytes = self.as_bytes();
        if bytes.len() > 32 {
            return Err(FromStrError::TooLong);
        }
        if bytes.iter().any(|&b| b == 0) {
            return Err(FromStrError::InteriorNul);
        }

        let mut data: [u8; 32] = [0; 32];
        data[..bytes.len()].copy_from_slice(bytes);

        Ok(Value::new(data))
    }
}

impl TryToValue<ShortString> for String {
    type Error = FromStrError;

    fn try_to_value(self) -> Result<Value<ShortString>, Self::Error> {
        (&self[..]).try_to_value()
    }
}

impl ToValue<ShortString> for &str {
    fn to_value(self) -> Value<ShortString> {
        self.try_to_value().unwrap()
    }
}

impl ToValue<ShortString> for String {
    fn to_value(self) -> Value<ShortString> {
        self.try_to_value().unwrap()
    }
}

impl ToValue<ShortString> for &String {
    fn to_value(self) -> Value<ShortString> {
        self.to_str().try_to_value().unwrap()
    }
}