schemars/json_schema_impls/
ffi.rs

1use crate::SchemaGenerator;
2use crate::_alloc_prelude::*;
3use crate::{json_schema, JsonSchema, Schema};
4use alloc::borrow::Cow;
5use serde_json::json;
6use std::ffi::{CStr, CString, OsStr, OsString};
7
8impl JsonSchema for OsString {
9    fn schema_name() -> Cow<'static, str> {
10        "OsString".into()
11    }
12
13    fn schema_id() -> Cow<'static, str> {
14        "std::ffi::OsString".into()
15    }
16
17    fn json_schema(generator: &mut SchemaGenerator) -> Schema {
18        json_schema!({
19            "oneOf": [
20                {
21                    "type": "object",
22                    "properties": {
23                        "Unix": <Vec<u8>>::json_schema(generator)
24                    },
25                    "required": ["Unix"]
26                },
27                {
28                    "type": "object",
29                    "properties": {
30                        "Windows": <Vec<u16>>::json_schema(generator)
31                    },
32                    "required": ["Windows"]
33                },
34            ]
35        })
36    }
37}
38
39forward_impl!(OsStr => OsString);
40
41impl JsonSchema for CString {
42    fn schema_name() -> Cow<'static, str> {
43        "CString".into()
44    }
45
46    fn schema_id() -> Cow<'static, str> {
47        "std::ffi::CString".into()
48    }
49
50    fn json_schema(generator: &mut SchemaGenerator) -> Schema {
51        let ty = if generator.contract().is_deserialize() {
52            json!(["array", "string"])
53        } else {
54            json!("array")
55        };
56
57        json_schema!({
58            "type": ty,
59            "items": {
60                "type": "integer",
61                "minimum": 1,
62                "maximum": 255
63            },
64        })
65    }
66}
67
68forward_impl!(CStr => CString);