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::context::ReadContext;
19use crate::context::WriteContext;
20use crate::error::Error;
21use crate::resolver::TypeResolver;
22use crate::serializer::util::read_basic_type_info;
23use crate::serializer::{ForyDefault, Serializer};
24use crate::type_id::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_u36_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_var_u36_small()?;
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 => {
53                let len = len as usize;
54                if context.is_check_string_read() {
55                    context.reader.read_utf8_string(len)
56                } else {
57                    context.reader.read_utf8_string_unchecked(len)
58                }
59            }
60            _ => {
61                return Err(Error::encoding_error(format!(
62                    "wrong encoding value: {}",
63                    encoding
64                )))
65            }
66        }?;
67        Ok(s)
68    }
69
70    #[inline(always)]
71    fn fory_reserved_space() -> usize {
72        mem::size_of::<i32>()
73    }
74
75    #[inline(always)]
76    fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
77        Ok(TypeId::STRING)
78    }
79
80    #[inline(always)]
81    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
82        Ok(TypeId::STRING)
83    }
84
85    #[inline(always)]
86    fn fory_static_type_id() -> TypeId
87    where
88        Self: Sized,
89    {
90        TypeId::STRING
91    }
92
93    #[inline(always)]
94    fn as_any(&self) -> &dyn std::any::Any {
95        self
96    }
97
98    #[inline(always)]
99    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
100        context.writer.write_u8(TypeId::STRING as u8);
101        Ok(())
102    }
103
104    #[inline(always)]
105    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
106        read_basic_type_info::<Self>(context)
107    }
108}
109
110impl ForyDefault for String {
111    #[inline(always)]
112    fn fory_default() -> Self {
113        String::new()
114    }
115}