Skip to main content

serde_shape/impls/
os_string.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::boxed::Box;
16use alloc::vec;
17use std::ffi::OsStr;
18use std::ffi::OsString;
19
20use crate::DefaultShape;
21use crate::DeserializeContainerAttributes;
22use crate::DeserializeDefinitionKind;
23use crate::DeserializeEnumShape;
24use crate::DeserializeFieldShape;
25use crate::DeserializeShape;
26use crate::DeserializeShapeContext;
27use crate::DeserializeVariantContent;
28use crate::DeserializeVariantShape;
29use crate::FieldMember;
30use crate::FieldWireShape;
31use crate::FieldsStyle;
32use crate::SerializeContainerAttributes;
33use crate::SerializeDefinitionKind;
34use crate::SerializeEnumShape;
35use crate::SerializeFieldShape;
36use crate::SerializeShape;
37use crate::SerializeShapeContext;
38use crate::SerializeVariantContent;
39use crate::SerializeVariantShape;
40use crate::ShapeRef;
41use crate::Tagging;
42use crate::TypeName;
43
44impl SerializeShape for OsStr {
45    fn serialize_shape_in(context: &mut SerializeShapeContext) -> ShapeRef {
46        serialize_os_string(context, TypeName::of::<Self>("OsString"))
47    }
48}
49
50impl SerializeShape for OsString {
51    fn serialize_shape_in(context: &mut SerializeShapeContext) -> ShapeRef {
52        serialize_os_string(context, TypeName::of::<Self>("OsString"))
53    }
54}
55
56impl DeserializeShape for OsString {
57    fn deserialize_shape_in(context: &mut DeserializeShapeContext) -> ShapeRef {
58        deserialize_os_string(context, TypeName::of::<Self>("OsString"))
59    }
60}
61
62impl DeserializeShape for Box<OsStr> {
63    fn deserialize_shape_in(context: &mut DeserializeShapeContext) -> ShapeRef {
64        deserialize_os_string(context, TypeName::of::<Self>("OsString"))
65    }
66}
67
68fn serialize_os_string(context: &mut SerializeShapeContext, type_name: TypeName) -> ShapeRef {
69    context.define_named_type(type_name, |_| {
70        SerializeDefinitionKind::Enum(SerializeEnumShape {
71            repr: Tagging::External,
72            variants: vec![SerializeVariantShape {
73                rust_name: platform_variant_name(),
74                name: platform_variant_name(),
75                description: None,
76                style: FieldsStyle::Newtype,
77                content: SerializeVariantContent::Fields(vec![SerializeFieldShape {
78                    member: FieldMember::Unnamed(0),
79                    name: "0",
80                    description: None,
81                    wire_shape: FieldWireShape::Value(platform_value_shape()),
82                    skip_if: None,
83                }]),
84                untagged: false,
85            }],
86            attributes: SerializeContainerAttributes::default(),
87        })
88    })
89}
90
91fn deserialize_os_string(context: &mut DeserializeShapeContext, type_name: TypeName) -> ShapeRef {
92    context.define_named_type(type_name, |_| {
93        DeserializeDefinitionKind::Enum(DeserializeEnumShape {
94            repr: Tagging::External,
95            variants: vec![DeserializeVariantShape {
96                rust_name: platform_variant_name(),
97                name: platform_variant_name(),
98                aliases: vec![platform_variant_name()],
99                description: None,
100                style: FieldsStyle::Newtype,
101                content: DeserializeVariantContent::Fields(vec![DeserializeFieldShape {
102                    member: FieldMember::Unnamed(0),
103                    name: "0",
104                    aliases: vec!["0"],
105                    description: None,
106                    wire_shape: FieldWireShape::Value(platform_value_shape()),
107                    default: DefaultShape::None,
108                }]),
109                other: false,
110                untagged: false,
111            }],
112            attributes: DeserializeContainerAttributes::default(),
113        })
114    })
115}
116
117#[cfg(unix)]
118fn platform_variant_name() -> &'static str {
119    "Unix"
120}
121
122#[cfg(windows)]
123fn platform_variant_name() -> &'static str {
124    "Windows"
125}
126
127#[cfg(unix)]
128fn platform_value_shape() -> ShapeRef {
129    ShapeRef::Seq(Box::new(ShapeRef::U8))
130}
131
132#[cfg(windows)]
133fn platform_value_shape() -> ShapeRef {
134    ShapeRef::Seq(Box::new(ShapeRef::U16))
135}