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

@ -155,7 +155,7 @@ impl ValidationAttrs {
if !ignore_errors {
errors.error_spanned_by(
meta,
"unknown item in schemars length attribute".to_string(),
"unknown item in schemars length attribute",
);
}
}
@ -184,7 +184,7 @@ impl ValidationAttrs {
if !ignore_errors {
errors.error_spanned_by(
meta,
"unknown item in schemars range attribute".to_string(),
"unknown item in schemars range attribute",
);
}
}
@ -252,8 +252,7 @@ impl ValidationAttrs {
if !ignore_errors {
errors.error_spanned_by(
meta,
"unknown item in schemars regex attribute"
.to_string(),
"unknown item in schemars regex attribute",
);
}
}
@ -294,8 +293,7 @@ impl ValidationAttrs {
if !ignore_errors {
errors.error_spanned_by(
meta,
"unknown item in schemars contains attribute"
.to_string(),
"unknown item in schemars contains attribute",
);
}
}
@ -390,7 +388,7 @@ impl ValidationAttrs {
if let Some(format) = &self.format {
let f = format.schema_str();
result.push(quote! {
schema.ensure_object().insert("format".to_owned(), #f.into());
schema.ensure_object().insert("format".into(), #f.into());
})
};

View file

@ -60,11 +60,11 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
<#ty as schemars::JsonSchema>::always_inline_schema()
}
fn schema_name() -> std::borrow::Cow<'static, str> {
fn schema_name() -> schemars::_alloc::borrow::Cow<'static, str> {
<#ty as schemars::JsonSchema>::schema_name()
}
fn schema_id() -> std::borrow::Cow<'static, str> {
fn schema_id() -> schemars::_alloc::borrow::Cow<'static, str> {
<#ty as schemars::JsonSchema>::schema_id()
}
@ -104,11 +104,11 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
{
(
quote! {
std::borrow::Cow::Borrowed(#schema_base_name)
schemars::_alloc::borrow::Cow::Borrowed(#schema_base_name)
},
quote! {
std::borrow::Cow::Borrowed(std::concat!(
std::module_path!(),
schemars::_alloc::borrow::Cow::Borrowed(::core::concat!(
::core::module_path!(),
"::",
#schema_base_name
))
@ -121,15 +121,15 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
}
(
quote! {
std::borrow::Cow::Owned(
format!(#schema_name_fmt #(,#type_params=#type_params::schema_name())* #(,#const_params=#const_params)*)
schemars::_alloc::borrow::Cow::Owned(
schemars::_alloc::format!(#schema_name_fmt #(,#type_params=#type_params::schema_name())* #(,#const_params=#const_params)*)
)
},
quote! {
std::borrow::Cow::Owned(
format!(
std::concat!(
std::module_path!(),
schemars::_alloc::borrow::Cow::Owned(
schemars::_alloc::format!(
::core::concat!(
::core::module_path!(),
"::",
#schema_name_fmt
)
@ -145,15 +145,15 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
schema_name_fmt.push_str(&"_and_{}".repeat(params.len() - 1));
(
quote! {
std::borrow::Cow::Owned(
format!(#schema_name_fmt #(,#type_params::schema_name())* #(,#const_params)*)
schemars::_alloc::borrow::Cow::Owned(
schemars::_alloc::format!(#schema_name_fmt #(,#type_params::schema_name())* #(,#const_params)*)
)
},
quote! {
std::borrow::Cow::Owned(
format!(
std::concat!(
std::module_path!(),
schemars::_alloc::borrow::Cow::Owned(
schemars::_alloc::format!(
::core::concat!(
::core::module_path!(),
"::",
#schema_name_fmt
)
@ -178,11 +178,11 @@ fn derive_json_schema(mut input: syn::DeriveInput, repr: bool) -> syn::Result<To
#[automatically_derived]
#[allow(unused_braces)]
impl #impl_generics schemars::JsonSchema for #type_name #ty_generics #where_clause {
fn schema_name() -> std::borrow::Cow<'static, str> {
fn schema_name() -> schemars::_alloc::borrow::Cow<'static, str> {
#schema_name
}
fn schema_id() -> std::borrow::Cow<'static, str> {
fn schema_id() -> schemars::_alloc::borrow::Cow<'static, str> {
#schema_id
}

View file

@ -51,11 +51,11 @@ pub fn expr_for_repr(cont: &Container) -> Result<TokenStream, syn::Error> {
let mut schema_expr = quote!({
let mut map = schemars::_serde_json::Map::new();
map.insert("type".to_owned(), "integer".into());
map.insert("type".into(), "integer".into());
map.insert(
"enum".to_owned(),
"enum".into(),
schemars::_serde_json::Value::Array({
let mut enum_values = Vec::new();
let mut enum_values = schemars::_alloc::vec::Vec::new();
#(enum_values.push((#enum_ident::#variant_idents as #repr_type).into());)*
enum_values
}),
@ -114,14 +114,14 @@ fn type_for_schema(with_attr: &WithAttr) -> (syn::Type, Option<TokenStream>) {
true
}
fn schema_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(#fn_name)
fn schema_name() -> schemars::_alloc::borrow::Cow<'static, str> {
schemars::_alloc::borrow::Cow::Borrowed(#fn_name)
}
fn schema_id() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(std::concat!(
fn schema_id() -> schemars::_alloc::borrow::Cow<'static, str> {
schemars::_alloc::borrow::Cow::Borrowed(::core::concat!(
"_SchemarsSchemaWithFunction/",
std::module_path!(),
::core::module_path!(),
"/",
#fn_name
))
@ -171,11 +171,11 @@ fn expr_for_external_tagged_enum<'a>(
let unit_names = unit_variants.iter().map(|v| v.name());
let unit_schema = quote!({
let mut map = schemars::_serde_json::Map::new();
map.insert("type".to_owned(), "string".into());
map.insert("type".into(), "string".into());
map.insert(
"enum".to_owned(),
"enum".into(),
schemars::_serde_json::Value::Array({
let mut enum_values = Vec::new();
let mut enum_values = schemars::_alloc::vec::Vec::new();
#(enum_values.push((#unit_names).into());)*
enum_values
}),
@ -347,9 +347,9 @@ fn variant_subschemas(unique: bool, schemas: Vec<TokenStream>) -> TokenStream {
quote!({
let mut map = schemars::_serde_json::Map::new();
map.insert(
#keyword.to_owned(),
#keyword.into(),
schemars::_serde_json::Value::Array({
let mut enum_values = Vec::new();
let mut enum_values = schemars::_alloc::vec::Vec::new();
#(enum_values.push(#schemas.to_value());)*
enum_values
}),
@ -556,7 +556,7 @@ fn field_default_expr(field: &Field, container_has_default: bool) -> Option<Toke
impl serde::Serialize for _SchemarsDefaultSerialize<#ty>
{
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
fn serialize<S>(&self, serializer: S) -> ::core::result::Result<S::Ok, S::Error>
where
S: serde::Serializer
{