Skip to main content

fory_core/serializer/
string.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::error::Error;
19use crate::resolver::context::ReadContext;
20use crate::resolver::context::WriteContext;
21use crate::resolver::type_resolver::TypeResolver;
22use crate::serializer::util::read_basic_type_info;
23use crate::serializer::{ForyDefault, Serializer};
24use crate::types::TypeId;
25use std::mem;
26
27#[allow(dead_code)]
28enum StrEncoding {
29    Latin1 = 0,
30    Utf16 = 1,
31    Utf8 = 2,
32}
33
34impl Serializer for String {
35    #[inline(always)]
36    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
37        let bitor = (self.len() as i32 as u64) << 2 | StrEncoding::Utf8 as u64;
38        context.writer.write_var_uint36_small(bitor);
39        context.writer.write_utf8_string(self);
40        Ok(())
41    }
42
43    #[inline(always)]
44    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
45        // xlang mode: read encoding header and decode accordingly
46        let bitor = context.reader.read_varuint36small()?;
47        let len = bitor >> 2;
48        let encoding = bitor & 0b11;
49        let s = match encoding {
50            0 => context.reader.read_latin1_string(len as usize),
51            1 => context.reader.read_utf16_string(len as usize),
52            2 => context.reader.read_utf8_string(len as usize),
53            _ => {
54                return Err(Error::encoding_error(format!(
55                    "wrong encoding value: {}",
56                    encoding
57                )))
58            }
59        }?;
60        Ok(s)
61    }
62
63    #[inline(always)]
64    fn fory_reserved_space() -> usize {
65        mem::size_of::<i32>()
66    }
67
68    #[inline(always)]
69    fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
70        Ok(TypeId::STRING)
71    }
72
73    #[inline(always)]
74    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
75        Ok(TypeId::STRING)
76    }
77
78    #[inline(always)]
79    fn fory_static_type_id() -> TypeId
80    where
81        Self: Sized,
82    {
83        TypeId::STRING
84    }
85
86    #[inline(always)]
87    fn as_any(&self) -> &dyn std::any::Any {
88        self
89    }
90
91    #[inline(always)]
92    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
93        context.writer.write_u8(TypeId::STRING as u8);
94        Ok(())
95    }
96
97    #[inline(always)]
98    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
99        read_basic_type_info::<Self>(context)
100    }
101}
102
103impl ForyDefault for String {
104    #[inline(always)]
105    fn fory_default() -> Self {
106        String::new()
107    }
108}