Skip to main content

serde_shape/impls/
range.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::Range;
17use core::ops::RangeFrom;
18use core::ops::RangeInclusive;
19use core::ops::RangeTo;
20
21use crate::DefaultShape;
22use crate::DeserializeContainerAttributes;
23use crate::DeserializeDefinitionKind;
24use crate::DeserializeFieldShape;
25use crate::DeserializeShape;
26use crate::DeserializeShapeContext;
27use crate::DeserializeStructShape;
28use crate::FieldMember;
29use crate::FieldWireShape;
30use crate::FieldsStyle;
31use crate::SerializeContainerAttributes;
32use crate::SerializeDefinitionKind;
33use crate::SerializeFieldShape;
34use crate::SerializeShape;
35use crate::SerializeShapeContext;
36use crate::SerializeStructShape;
37use crate::ShapeRef;
38use crate::TypeName;
39
40macro_rules! range_shape {
41    ($($range:ident { $($field:ident),+ $(,)? })+) => {
42        $(
43            impl<T> SerializeShape for $range<T>
44            where
45                T: SerializeShape,
46            {
47                fn serialize_shape_in(context: &mut SerializeShapeContext) -> ShapeRef {
48                    context.define_named_type(
49                        TypeName::of::<Self>(stringify!($range)),
50                        |context| {
51                            SerializeDefinitionKind::Struct(SerializeStructShape {
52                                style: FieldsStyle::Struct,
53                                fields: vec![
54                                    $(SerializeFieldShape {
55                                        member: FieldMember::Named(stringify!($field)),
56                                        name: stringify!($field),
57                                        description: None,
58                                        wire_shape: FieldWireShape::Value(
59                                            T::serialize_shape_in(context),
60                                        ),
61                                        skip_if: None,
62                                    }),+
63                                ],
64                                attributes: SerializeContainerAttributes::default(),
65                            })
66                        },
67                    )
68                }
69            }
70
71            impl<T> DeserializeShape for $range<T>
72            where
73                T: DeserializeShape,
74            {
75                fn deserialize_shape_in(context: &mut DeserializeShapeContext) -> ShapeRef {
76                    context.define_named_type(
77                        TypeName::of::<Self>(stringify!($range)),
78                        |context| {
79                            DeserializeDefinitionKind::Struct(DeserializeStructShape {
80                                style: FieldsStyle::Struct,
81                                fields: vec![
82                                    $(DeserializeFieldShape {
83                                        member: FieldMember::Named(stringify!($field)),
84                                        name: stringify!($field),
85                                        aliases: vec![stringify!($field)],
86                                        description: None,
87                                        wire_shape: FieldWireShape::Value(
88                                            T::deserialize_shape_in(context),
89                                        ),
90                                        default: DefaultShape::None,
91                                    }),+
92                                ],
93                                attributes: DeserializeContainerAttributes {
94                                    deny_unknown_fields: true,
95                                    ..DeserializeContainerAttributes::default()
96                                },
97                            })
98                        },
99                    )
100                }
101            }
102        )+
103    };
104}
105
106range_shape! {
107    Range { start, end }
108    RangeFrom { start }
109    RangeInclusive { start, end }
110    RangeTo { end }
111}