fory_core/serializer/
enum_.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::ensure;
19use crate::error::Error;
20use crate::meta::FieldInfo;
21use crate::resolver::context::{ReadContext, WriteContext};
22use crate::serializer::{ForyDefault, Serializer};
23use crate::types::{RefFlag, TypeId};
24use crate::TypeResolver;
25
26#[inline(always)]
27pub fn actual_type_id(type_id: u32, register_by_name: bool, _compatible: bool) -> u32 {
28    if register_by_name {
29        TypeId::NAMED_ENUM as u32
30    } else {
31        (type_id << 8) + TypeId::ENUM as u32
32    }
33}
34
35#[inline(always)]
36pub fn write<T: Serializer>(
37    this: &T,
38    context: &mut WriteContext,
39    write_ref_info: bool,
40    write_type_info: bool,
41) -> Result<(), Error> {
42    if write_ref_info {
43        context.writer.write_i8(RefFlag::NotNullValue as i8);
44    }
45    if write_type_info {
46        T::fory_write_type_info(context)?;
47    }
48    this.fory_write_data(context)
49}
50
51#[inline(always)]
52pub fn write_type_info<T: Serializer>(context: &mut WriteContext) -> Result<(), Error> {
53    let type_id = T::fory_get_type_id(context.get_type_resolver())?;
54    context.writer.write_varuint32(type_id);
55    let is_named_enum = type_id & 0xff == TypeId::NAMED_ENUM as u32;
56    if !is_named_enum {
57        return Ok(());
58    }
59    let rs_type_id = std::any::TypeId::of::<T>();
60    if context.is_share_meta() {
61        let meta_index = context.push_meta(rs_type_id)? as u32;
62        context.writer.write_varuint32(meta_index);
63    } else {
64        let type_info = context.get_type_resolver().get_type_info(&rs_type_id)?;
65        let namespace = type_info.get_namespace();
66        let type_name = type_info.get_type_name();
67        context.write_meta_string_bytes(namespace)?;
68        context.write_meta_string_bytes(type_name)?;
69    }
70    Ok(())
71}
72
73#[inline(always)]
74pub fn read<T: Serializer + ForyDefault>(
75    context: &mut ReadContext,
76    read_ref_info: bool,
77    read_type_info: bool,
78) -> Result<T, Error> {
79    let ref_flag = if read_ref_info {
80        context.reader.read_i8()?
81    } else {
82        RefFlag::NotNullValue as i8
83    };
84    if ref_flag == RefFlag::Null as i8 {
85        Ok(T::fory_default())
86    } else if ref_flag == (RefFlag::NotNullValue as i8) || ref_flag == (RefFlag::RefValue as i8) {
87        if read_type_info {
88            T::fory_read_type_info(context)?;
89        }
90        T::fory_read_data(context)
91    } else if ref_flag == (RefFlag::Ref as i8) {
92        Err(Error::invalid_ref("Invalid ref, enum type is not a ref"))
93    } else {
94        Err(Error::invalid_data(format!(
95            "Unknown ref flag: {}",
96            ref_flag
97        )))
98    }
99}
100
101#[inline(always)]
102pub fn read_type_info<T: Serializer>(context: &mut ReadContext) -> Result<(), Error> {
103    let local_type_id = T::fory_get_type_id(context.get_type_resolver())?;
104    let remote_type_id = context.reader.read_varuint32()?;
105    ensure!(
106        local_type_id == remote_type_id,
107        Error::type_mismatch(local_type_id, remote_type_id)
108    );
109    let is_named_enum = local_type_id & 0xff == TypeId::NAMED_ENUM as u32;
110    if !is_named_enum {
111        return Ok(());
112    }
113    if context.is_share_meta() {
114        let _meta_index = context.reader.read_varuint32()?;
115    } else {
116        let _namespace_msb = context.read_meta_string()?;
117        let _type_name_msb = context.read_meta_string()?;
118    }
119    Ok(())
120}
121
122pub trait NamedEnumVariantMetaTrait: 'static {
123    fn fory_get_sorted_field_names() -> &'static [&'static str] {
124        &[]
125    }
126
127    #[allow(unused_variables)]
128    fn fory_fields_info(type_resolver: &TypeResolver) -> Result<Vec<FieldInfo>, Error> {
129        Ok(Vec::default())
130    }
131}