Skip to main content

serde_shape/impls/
bound.rs

1// Copyright 2026 FastLabs Developers
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use alloc::vec;
16use core::ops::Bound;
17
18use crate::DefaultShape;
19use crate::DeserializeContainerAttributes;
20use crate::DeserializeDefinitionKind;
21use crate::DeserializeEnumShape;
22use crate::DeserializeFieldShape;
23use crate::DeserializeShape;
24use crate::DeserializeShapeContext;
25use crate::DeserializeVariantContent;
26use crate::DeserializeVariantShape;
27use crate::FieldMember;
28use crate::FieldWireShape;
29use crate::FieldsStyle;
30use crate::SerializeContainerAttributes;
31use crate::SerializeDefinitionKind;
32use crate::SerializeEnumShape;
33use crate::SerializeFieldShape;
34use crate::SerializeShape;
35use crate::SerializeShapeContext;
36use crate::SerializeVariantContent;
37use crate::SerializeVariantShape;
38use crate::ShapeRef;
39use crate::Tagging;
40use crate::TypeName;
41
42impl<T> SerializeShape for Bound<T>
43where
44    T: SerializeShape,
45{
46    fn serialize_shape_in(context: &mut SerializeShapeContext) -> ShapeRef {
47        context.define_named_type(TypeName::of::<Self>("Bound"), |context| {
48            SerializeDefinitionKind::Enum(SerializeEnumShape {
49                repr: Tagging::External,
50                variants: vec![
51                    serialize_bound_variant("Unbounded", None),
52                    serialize_bound_variant("Included", Some(T::serialize_shape_in(context))),
53                    serialize_bound_variant("Excluded", Some(T::serialize_shape_in(context))),
54                ],
55                attributes: SerializeContainerAttributes::default(),
56            })
57        })
58    }
59}
60
61impl<T> DeserializeShape for Bound<T>
62where
63    T: DeserializeShape,
64{
65    fn deserialize_shape_in(context: &mut DeserializeShapeContext) -> ShapeRef {
66        context.define_named_type(TypeName::of::<Self>("Bound"), |context| {
67            DeserializeDefinitionKind::Enum(DeserializeEnumShape {
68                repr: Tagging::External,
69                variants: vec![
70                    deserialize_bound_variant("Unbounded", None),
71                    deserialize_bound_variant("Included", Some(T::deserialize_shape_in(context))),
72                    deserialize_bound_variant("Excluded", Some(T::deserialize_shape_in(context))),
73                ],
74                attributes: DeserializeContainerAttributes::default(),
75            })
76        })
77    }
78}
79
80fn serialize_bound_variant(
81    name: &'static str,
82    value_shape: Option<ShapeRef>,
83) -> SerializeVariantShape {
84    let (style, fields) = match value_shape {
85        Some(value_shape) => (
86            FieldsStyle::Newtype,
87            vec![SerializeFieldShape {
88                member: FieldMember::Unnamed(0),
89                name: "0",
90                description: None,
91                wire_shape: FieldWireShape::Value(value_shape),
92                skip_if: None,
93            }],
94        ),
95        None => (FieldsStyle::Unit, vec![]),
96    };
97
98    SerializeVariantShape {
99        rust_name: name,
100        name,
101        description: None,
102        style,
103        content: SerializeVariantContent::Fields(fields),
104        untagged: false,
105    }
106}
107
108fn deserialize_bound_variant(
109    name: &'static str,
110    value_shape: Option<ShapeRef>,
111) -> DeserializeVariantShape {
112    let (style, fields) = match value_shape {
113        Some(value_shape) => (
114            FieldsStyle::Newtype,
115            vec![DeserializeFieldShape {
116                member: FieldMember::Unnamed(0),
117                name: "0",
118                aliases: vec!["0"],
119                description: None,
120                wire_shape: FieldWireShape::Value(value_shape),
121                default: DefaultShape::None,
122            }],
123        ),
124        None => (FieldsStyle::Unit, vec![]),
125    };
126
127    DeserializeVariantShape {
128        rust_name: name,
129        name,
130        aliases: vec![name],
131        description: None,
132        style,
133        content: DeserializeVariantContent::Fields(fields),
134        other: false,
135        untagged: false,
136    }
137}