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, WriteContext};
19use crate::error::Error;
20use crate::serializer::util::read_basic_type_info;
21use crate::serializer::Serializer;
22use crate::type_id::TypeId;
23use std::sync::Arc;
24
25#[allow(dead_code)]
26enum StrEncoding {
27    Latin1 = 0,
28    Utf16 = 1,
29    Utf8 = 2,
30}
31
32impl Serializer for String {
33    type Target = Self;
34
35    #[inline(always)]
36    fn write_data(value: &Self, context: &mut WriteContext) -> Result<(), Error> {
37        let header = (value.len() as i32 as u64) << 2 | StrEncoding::Utf8 as u64;
38        context.writer.write_var_u36_small(header);
39        context.writer.write_utf8_string(value);
40        Ok(())
41    }
42
43    #[inline(always)]
44    fn read_data(context: &mut ReadContext) -> Result<Self, Error> {
45        let header = context.reader.read_var_u36_small()?;
46        let len = (header >> 2) as usize;
47        match header & 0b11 {
48            0 => context.reader.read_latin1_string(len),
49            1 => context.reader.read_utf16_string(len),
50            2 if context.is_check_string_read() => context.reader.read_utf8_string(len),
51            2 => context.reader.read_utf8_string_unchecked(len),
52            encoding => Err(Error::encoding_error(format!(
53                "wrong encoding value: {}",
54                encoding
55            ))),
56        }
57    }
58
59    #[inline(always)]
60    fn default_value(_: &mut ReadContext) -> Result<Self, Error> {
61        Ok(String::new())
62    }
63
64    #[inline(always)]
65    fn read_arc_any(
66        context: &mut ReadContext,
67    ) -> Result<Arc<dyn std::any::Any + Send + Sync>, Error> {
68        Ok(Arc::new(Self::read_data(context)?))
69    }
70
71    #[inline(always)]
72    fn reserved_space() -> usize {
73        std::mem::size_of::<i32>()
74    }
75
76    #[inline(always)]
77    fn static_type_id() -> TypeId {
78        TypeId::STRING
79    }
80
81    #[inline(always)]
82    fn write_type_info(context: &mut WriteContext) -> Result<(), Error> {
83        context.writer.write_u8(TypeId::STRING as u8);
84        Ok(())
85    }
86
87    #[inline(always)]
88    fn read_type_info(context: &mut ReadContext) -> Result<(), Error> {
89        read_basic_type_info::<Self>(context)
90    }
91}