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    #[inline]
70    fn fory_read_data_as_send_sync_any(
71        context: &mut ReadContext,
72    ) -> Result<Box<dyn std::any::Any + Send + Sync>, Error>
73    where
74        Self: Sized + ForyDefault,
75    {
76        Ok(crate::serializer::box_send_sync(Self::fory_read_data(
77            context,
78        )?))
79    }
80
81    #[inline(always)]
82    fn fory_reserved_space() -> usize {
83        mem::size_of::<i32>()
84    }
85
86    #[inline(always)]
87    fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
88        Ok(TypeId::STRING)
89    }
90
91    #[inline(always)]
92    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
93        Ok(TypeId::STRING)
94    }
95
96    #[inline(always)]
97    fn fory_static_type_id() -> TypeId
98    where
99        Self: Sized,
100    {
101        TypeId::STRING
102    }
103
104    #[inline(always)]
105    fn as_any(&self) -> &dyn std::any::Any {
106        self
107    }
108
109    #[inline(always)]
110    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
111        context.writer.write_u8(TypeId::STRING as u8);
112        Ok(())
113    }
114
115    #[inline(always)]
116    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
117        read_basic_type_info::<Self>(context)
118    }
119}
120
121impl ForyDefault for String {
122    #[inline(always)]
123    fn fory_default() -> Self {
124        String::new()
125    }
126}