Skip to main content

tpack_core/native/
text.rs

1use alloc::{borrow::Cow, string::String};
2
3use crate::{Result, Schema, TpackValue};
4
5use super::helpers::{bytes_schema, deserialize_via_from_value, string_schema, type_mismatch};
6use super::{FromTpackValue, TpackDeserialize, TpackSerialize};
7
8impl TpackSerialize for String {
9    fn schema() -> Schema {
10        string_schema()
11    }
12
13    fn to_value(&self) -> TpackValue<'_> {
14        TpackValue::String(Cow::Borrowed(self.as_str()))
15    }
16}
17
18impl<'de> TpackDeserialize<'de> for String {
19    fn schema() -> Schema {
20        <Self as TpackSerialize>::schema()
21    }
22
23    fn from_value(value: TpackValue<'de>) -> Result<Self> {
24        deserialize_via_from_value(value)
25    }
26}
27
28impl<'de> FromTpackValue<'de> for String {
29    fn from_value(value: TpackValue<'de>) -> Result<Self> {
30        match value {
31            TpackValue::String(value) => Ok(value.into_owned()),
32            _ => Err(type_mismatch("String")),
33        }
34    }
35}
36
37impl TpackSerialize for &str {
38    fn schema() -> Schema {
39        string_schema()
40    }
41
42    fn to_value(&self) -> TpackValue<'_> {
43        TpackValue::String(Cow::Borrowed(*self))
44    }
45}
46
47impl TpackSerialize for &[u8] {
48    fn schema() -> Schema {
49        bytes_schema()
50    }
51
52    fn to_value(&self) -> TpackValue<'_> {
53        TpackValue::Bytes(Cow::Borrowed(*self))
54    }
55}