orchestra-toolkit 0.6.1

Client to interract with Orchestra system, uses HGTP protocol
Documentation
/* Copyright 2024-2025 LEDR Technologies Inc.
* This file is part of the Orchestra library, which helps developer use our Orchestra technology which is based on AvesTerra, owned and developped by Georgetown University, under license agreement with LEDR Technologies Inc.
*
* The Orchestra library is a free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
*
* The Orchestra library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with the Orchestra library. If not, see <https://www.gnu.org/licenses/>.
*
* If you have any questions, feedback or issues about the Orchestra library, you can contact us at support@ledr.io.
*/

use ascii::AsciiString;
use serde::{Deserialize, Serialize};
use std::fmt::Display;
use std::ops::Deref;
use thiserror::Error;

/// String that is at most 255 bytes long.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String", expecting = "a string")]
pub struct String255(String);

#[derive(Error, Debug)]
#[error("String is too long. Maximum size is 255 bytes but is {0}.")]
pub struct String255TooLongError(pub usize);

impl String255 {
    pub const NULL: Self = Self(String::new());

    /// Will panic if the value fails to get created.
    /// Use this for constants, otherwise use `try_from`.
    pub fn unchecked(from: &str) -> Self {
        Self::try_from(from.to_string()).expect("ShortString::unchecked failed")
    }

    pub fn from_str_truncate(s: &str) -> Self {
        if s.len() <= 255 {
            Self::unchecked(s)
        } else {
            Self::unchecked(&s[..255])
        }
    }

    // For consistency with other types
    pub fn is_null(&self) -> bool {
        self.0.is_empty()
    }
}

impl TryFrom<String> for String255 {
    type Error = String255TooLongError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        if value.len() > 255 {
            Err(String255TooLongError(value.len()))
        } else {
            Ok(Self(value))
        }
    }
}

impl TryFrom<AsciiString> for String255 {
    type Error = String255TooLongError;

    fn try_from(value: AsciiString) -> Result<Self, Self::Error> {
        value.to_string().try_into()
    }
}

impl Deref for String255 {
    type Target = String;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

// Needed for Serde
impl From<String255> for String {
    fn from(val: String255) -> Self {
        val.0
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_truncate_short_string() {
        let expected = String255::unchecked("Hello");
        let actual = String255::from_str_truncate("Hello");
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_truncate_len_254() {
        let str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        assert_eq!(str.len(), 254);
        let expected = String255::unchecked(str);

        let actual = String255::from_str_truncate(str);
        assert_eq!(expected, actual);
    }

    #[test]
    fn test_truncate_len_255() {
        let str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        assert_eq!(str.len(), 255);
        let expected = String255::unchecked(str);

        let actual = String255::from_str_truncate(str);
        assert_eq!(expected, actual);
    }
    #[test]
    fn test_truncate_len_256() {
        let str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        assert_eq!(str.len(), 256);
        let expected = String255::unchecked("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        assert_eq!(expected.len(), 255);

        let actual = String255::from_str_truncate(str);
        assert_eq!(expected, actual);
    }
}