deku_string 0.4.1

Encoding/decoding helpers for Deku, String, Vec in fixed, Pascal, .NET and C-style formats with length guarantee.
Documentation
//! Crate-related implementations for [`crate::StringDeku`].

use crate::{
    InternalValue, StringDeku, serde_shim_implementation, std_shim_implementation,
};
use alloc::string::String;

impl InternalValue for StringDeku {
    type InternalType = String;

    #[inline]
    fn internal_move(self) -> Self::InternalType {
        self.0
    }

    #[inline]
    fn internal_mut(&mut self) -> &mut Self::InternalType {
        &mut self.0
    }

    #[inline]
    fn internal_ref(&self) -> &Self::InternalType {
        &self.0
    }
}

impl StringDeku {
    /// Construct new [`StringDeku`] from [`&str`]-like.
    #[inline]
    #[must_use]
    pub fn new<T>(value: T) -> Self
    where
        T: AsRef<str>,
    {
        Self(value.as_ref().into())
    }
}

serde_shim_implementation! {
    module_name: serde_impl,
    local_type: StringDeku,
    internal_type: alloc::string::String,
    test_input: "from str",
    test_input_encoded: "\"from str\"",
    test_input_encoded_invalid: "123",
}

std_shim_implementation! {
    module_name: std_impl,
    local_type: StringDeku,
    internal_type: alloc::string::String,
    deref_type: str,
    test_input: "from str".into(),
    test_input_other: "other value".into(),
    test_input_less: "aaa".into(),
    test_input_greater: "zzz".into(),
}

#[cfg(test)]
mod new_impl {
    use crate::StringDeku;
    use rstest::rstest;

    #[rstest]
    fn new_impl() {
        let input = "from str";
        let local: StringDeku = StringDeku::new(input);

        assert_eq!(input, local);
    }
}