Add no_std support via disabling the new default feature std (#319)

This commit is contained in:
Graham Esau 2024-08-17 19:46:11 +01:00 committed by GitHub
parent 3c9e49d161
commit 89a34e7a63
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
40 changed files with 343 additions and 164 deletions

View file

@ -0,0 +1,70 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "MyStruct",
"type": "object",
"properties": {
"my_int": {
"type": "integer",
"format": "int32"
},
"my_bool": {
"type": "boolean"
},
"my_nullable_enum": {
"anyOf": [
{
"$ref": "#/$defs/MyEnum"
},
{
"type": "null"
}
]
}
},
"required": [
"my_int",
"my_bool"
],
"$defs": {
"MyEnum": {
"oneOf": [
{
"type": "object",
"properties": {
"StringNewType": {
"type": "string"
}
},
"required": [
"StringNewType"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"StructVariant": {
"type": "object",
"properties": {
"floats": {
"type": "array",
"items": {
"type": "number",
"format": "float"
}
}
},
"required": [
"floats"
]
}
},
"required": [
"StructVariant"
],
"additionalProperties": false
}
]
}
}
}

25
schemars/tests/no_std.rs Normal file
View file

@ -0,0 +1,25 @@
#![no_std]
mod util;
use schemars::JsonSchema;
use util::*;
extern crate alloc as test_alloc;
#[derive(JsonSchema)]
pub struct MyStruct {
pub my_int: i32,
pub my_bool: bool,
pub my_nullable_enum: Option<MyEnum>,
}
#[derive(JsonSchema)]
pub enum MyEnum {
StringNewType(test_alloc::string::String),
StructVariant { floats: test_alloc::vec::Vec<f32> },
}
#[test]
fn no_std() -> TestResult {
test_default_generated_schema::<MyStruct>("no_std")
}

View file

@ -2,7 +2,7 @@ mod util;
use schemars::JsonSchema;
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet};
use util::*;
#[allow(dead_code)]
@ -39,7 +39,7 @@ struct MyStruct<'a, T: Serialize> {
s: Str<'a>,
// #[schemars(with = "HashMap::<_, HashSet<_>>")]
// map: BTreeMap<String, BTreeSet<String>>,
#[schemars(with = "HashMap::<String, HashSet<String>>")]
#[schemars(with = "BTreeMap::<String, BTreeSet<String>>")]
fake_map: (),
}

View file

@ -1,7 +1,11 @@
use pretty_assertions::assert_eq;
use schemars::{gen::SchemaSettings, schema_for, JsonSchema, Schema};
use std::error::Error;
use std::format;
use std::fs;
use std::prelude::rust_2021::*;
extern crate std;
pub type TestResult = Result<(), Box<dyn Error>>;

View file

@ -1,6 +1,6 @@
mod util;
use schemars::JsonSchema;
use std::collections::HashMap;
use std::collections::BTreeMap;
use util::*;
// In real code, this would typically be a Regex, potentially created in a `lazy_static!`.
@ -39,7 +39,7 @@ pub struct Struct {
#[validate(length(equal = 2))]
pair: Vec<i32>,
#[validate(contains = "map_key")]
map_contains: HashMap<String, ()>,
map_contains: BTreeMap<String, ()>,
#[validate(required)]
required_option: Option<bool>,
#[validate(required)]
@ -91,7 +91,7 @@ pub struct Struct2 {
#[schemars(length(equal = 2))]
pair: Vec<i32>,
#[schemars(contains = "map_key")]
map_contains: HashMap<String, ()>,
map_contains: BTreeMap<String, ()>,
#[schemars(required)]
required_option: Option<bool>,
#[schemars(required)]