Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ jobs:
- uses: actions-rs/cargo@v1
with:
command: test
args: --release

format:
name: Format
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[workspace]
resolver = "3"

members = [
"schema_analysis",
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "1.85"
channel = "1.90"
2 changes: 1 addition & 1 deletion schema_analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "schema_analysis"
version = "0.5.0"
authors = ["QuartzLibrary"]
license = "MIT OR Apache-2.0"
edition = "2018"
edition = "2024"
description = "Analyze the schema of any self describing format"
homepage = "https://schema-analysis.com/"
repository = "https://github.com/QuartzLibrary/schema_analysis"
Expand Down
2 changes: 1 addition & 1 deletion schema_analysis/src/analysis/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::de::{DeserializeSeed, Error, Visitor};

use crate::Field;

use super::{schema::SchemaVisitor, schema_seed::SchemaVisitorSeed, Context};
use super::{Context, schema::SchemaVisitor, schema_seed::SchemaVisitorSeed};

pub(super) struct InferredField<C> {
_marker: PhantomData<C>,
Expand Down
6 changes: 3 additions & 3 deletions schema_analysis/src/analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,14 @@ let _: Visitor::Value = deserializer.deserialize_str(visitor);
`[...]`

*/
use serde::{de::DeserializeSeed, Deserialize};
use serde::{Deserialize, de::DeserializeSeed};

#[allow(unused_imports)]
use serde::de::Visitor; // For docs above.

use crate::{
context::{Context, DefaultContext},
Coalesce, Schema,
context::{Context, DefaultContext},
};

mod field;
Expand Down Expand Up @@ -235,7 +235,7 @@ where
mod boilerplate {
use std::fmt;

use crate::{context::Context, Schema};
use crate::{Schema, context::Context};

use super::InferredSchema;

Expand Down
4 changes: 2 additions & 2 deletions schema_analysis/src/analysis/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::marker::PhantomData;
use ordermap::OrderMap;
use serde::de::{Error, Visitor};

use crate::{traits::Aggregate, Field, Schema};
use crate::{Field, Schema, traits::Aggregate};

use super::{
field::{InferredField, InferredFieldSeed},
Context,
field::{InferredField, InferredFieldSeed},
};

pub(super) struct SchemaVisitor<C> {
Expand Down
8 changes: 4 additions & 4 deletions schema_analysis/src/analysis/schema_seed.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use serde::de::{Error, Visitor};

use crate::{traits::Aggregate, traits::Coalesce, Schema};
use crate::{Schema, traits::Aggregate, traits::Coalesce};

use super::{
Context,
field::{InferredField, InferredFieldSeed},
schema::SchemaVisitor,
Context,
};

pub(super) struct SchemaVisitorSeed<'s, C: Context> {
Expand Down Expand Up @@ -182,8 +182,8 @@ where
match &mut self.schema {
// The schema matches
Schema::Sequence {
field: ref mut boxed_field,
context: ref mut aggregators,
field: boxed_field,
context: aggregators,
} => {
let field = boxed_field.as_mut();

Expand Down
2 changes: 1 addition & 1 deletion schema_analysis/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub use string::{SemanticExtractor, StringContext, SuspiciousStrings};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

use crate::{traits::Aggregate, Coalesce};
use crate::{Coalesce, traits::Aggregate};

/// Interface describing the custom analysis that will be run on each type
/// alongside the schema shape.
Expand Down
4 changes: 2 additions & 2 deletions schema_analysis/src/context/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

use std::fmt::Debug;

use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde::{Deserialize, Serialize, de::DeserializeOwned};

use crate::{traits::Aggregate, traits::Coalesce};

use super::{
shared::{MinMax, Sampler},
Counter,
shared::{MinMax, Sampler},
};

/// The context for numeric values.
Expand Down
2 changes: 1 addition & 1 deletion schema_analysis/src/context/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};

use crate::{traits::Aggregate, traits::Coalesce};

use super::{shared::MinMax, Counter};
use super::{Counter, shared::MinMax};

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SequenceContext {
Expand Down
2 changes: 1 addition & 1 deletion schema_analysis/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod xml {

use std::mem;

use crate::{context::Context, Field, Schema};
use crate::{Field, Schema, context::Context};

/// A wrapper function that applies all XML cleaning transformations.
///
Expand Down
4 changes: 2 additions & 2 deletions schema_analysis/src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::mem;

use ordermap::{map::Entry, OrderMap};
use ordermap::{OrderMap, map::Entry};
use serde::{Deserialize, Serialize};

use crate::{context::Context, context::DefaultContext, Coalesce, StructuralEq};
use crate::{Coalesce, StructuralEq, context::Context, context::DefaultContext};

/// This enum is the core output of the analysis, it describes the structure of a document.
///
Expand Down
4 changes: 2 additions & 2 deletions schema_analysis/src/targets/json_typegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ let output: String = json_typegen_shared::codegen_from_shape("Root", &Shape::Boo
```
*/

pub use json_typegen_shared::{codegen_from_shape, ErrorKind, JTError, Options, OutputMode, Shape};
pub use json_typegen_shared::{ErrorKind, JTError, Options, OutputMode, Shape, codegen_from_shape};

use crate::{context::Context, Field, Schema};
use crate::{Field, Schema, context::Context};

impl<C: Context> Schema<C> {
/// Convert a [Schema] to a json_typegen [Shape].
Expand Down
43 changes: 19 additions & 24 deletions schema_analysis/src/targets/schemars.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
//! Integration with [schemars](https://github.com/GREsau/schemars)

use std::error::Error;

use schemars::schema as schemars_types;

use crate::{context::Context, Schema};
use crate::{Schema, context::Context};

impl<C: Context> Schema<C> {
/// Convert into a json_schema using the default settings.
pub fn to_json_schema_with_schemars(&self) -> Result<String, impl Error> {
self.to_json_schema_with_schemars_version(&Default::default())
pub fn to_json_schema_with_schemars(&self) -> serde_json::Result<String> {
let default = Default::default();
self.to_json_schema_with_schemars_version(&default)
}

/// Convert into a specific version of json_schema.
pub fn to_json_schema_with_schemars_version(
&self,
version: &JsonSchemaVersion,
) -> Result<String, impl Error> {
let settings: schemars::gen::SchemaSettings = version.to_schemars_settings();
let mut generator: schemars::gen::SchemaGenerator = settings.into();
) -> serde_json::Result<String> {
let settings: schemars::r#gen::SchemaSettings = version.to_schemars_settings();
let mut generator: schemars::r#gen::SchemaGenerator = settings.into();

let root = self.to_schemars_schema(&mut generator);
serde_json::to_string_pretty(&root)
Expand All @@ -27,32 +26,28 @@ impl<C: Context> Schema<C> {
/// Convert using a provided generator (which also holds the settings) to a json schema.
pub fn to_schemars_schema(
&self,
generator: &mut schemars::gen::SchemaGenerator,
generator: &mut schemars::r#gen::SchemaGenerator,
) -> schemars_types::RootSchema {
let inner = helpers::inferred_to_schemars(generator, self);
helpers::wrap_in_root(inner, generator.settings())
}
}

/// The currently supported json schema versions.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub enum JsonSchemaVersion {
/// `schemars::gen::SchemaSettings::draft07`
/// `schemars::r#gen::SchemaSettings::draft07`
Draft07,
/// `schemars::gen::SchemaSettings::draft2019_09`
/// `schemars::r#gen::SchemaSettings::draft2019_09`
#[default]
Draft2019_09,
/// `schemars::gen::SchemaSettings::openapi3`
/// `schemars::r#gen::SchemaSettings::openapi3`
OpenApi3,
}
impl Default for JsonSchemaVersion {
fn default() -> Self {
Self::Draft2019_09
}
}
impl JsonSchemaVersion {
/// Convert the version to full settings.
pub fn to_schemars_settings(&self) -> schemars::gen::SchemaSettings {
use schemars::gen::SchemaSettings;
pub fn to_schemars_settings(&self) -> schemars::r#gen::SchemaSettings {
use schemars::r#gen::SchemaSettings;
match self {
JsonSchemaVersion::Draft07 => SchemaSettings::draft07(),
JsonSchemaVersion::Draft2019_09 => SchemaSettings::draft2019_09(),
Expand All @@ -67,12 +62,12 @@ mod helpers {

use schemars::schema as schemars_types;

use crate::{context::Context, Field, Schema};
use crate::{Field, Schema, context::Context};

/// Wraps a [Schema](schemars_types::Schema) in a [RootSchema](schemars_types::RootSchema).
pub fn wrap_in_root(
inner: schemars_types::Schema,
settings: &schemars::gen::SchemaSettings,
settings: &schemars::r#gen::SchemaSettings,
) -> schemars_types::RootSchema {
schemars_types::RootSchema {
meta_schema: settings.meta_schema.clone(),
Expand All @@ -83,7 +78,7 @@ mod helpers {

/// Converts an inferred [Schema] to a schemars [Schema](schemars_types::Schema).
pub fn inferred_to_schemars<C: Context>(
generator: &mut schemars::gen::SchemaGenerator,
generator: &mut schemars::r#gen::SchemaGenerator,
inferred: &Schema<C>,
) -> schemars_types::Schema {
// Note: we can use the generator even if we don't generate the final root schema
Expand Down Expand Up @@ -166,7 +161,7 @@ mod helpers {

/// Converts a [Field] into a [Schema](schemars_types::Schema).
fn internal_field_to_schemars_schema<C: Context>(
generator: &mut schemars::gen::SchemaGenerator,
generator: &mut schemars::r#gen::SchemaGenerator,
field: &Field<C>,
) -> schemars_types::Schema {
// Note: we can use the generator even if we don't generate the final root schema
Expand Down
2 changes: 1 addition & 1 deletion schema_analysis/tests/source_json.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde_json::{json, Value};
use serde_json::{Value, json};

use schema_analysis::InferredSchema;

Expand Down
2 changes: 1 addition & 1 deletion schema_analysis/tests/source_toml.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use toml::{value::Table, Value};
use toml::{Value, value::Table};

use schema_analysis::InferredSchema;

Expand Down
2 changes: 1 addition & 1 deletion schema_analysis/tests/source_xml.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use schema_analysis::{helpers, InferredSchema};
use schema_analysis::{InferredSchema, helpers};

mod shared;
use shared::FormatTests;
Expand Down
2 changes: 1 addition & 1 deletion schema_analysis/tests/target_json_schema_json_typegen.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(feature = "json_typegen")]

use json_typegen_shared::OutputMode;
use serde_json::{json, Value};
use serde_json::{Value, json};

use schema_analysis::{InferredSchema, Schema};

Expand Down
2 changes: 1 addition & 1 deletion schema_analysis/tests/target_json_schema_schemars.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![cfg(feature = "schemars_integration")]

use serde_json::{json, Value};
use serde_json::{Value, json};

use schema_analysis::{InferredSchema, Schema};

Expand Down
2 changes: 1 addition & 1 deletion website/schema_analysis_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "schema_analysis_wasm"
version = "0.2.2"
authors = ["QuartzLibrary"]
edition = "2018"
edition = "2024"
publish = false

[lib]
Expand Down
4 changes: 2 additions & 2 deletions website/schema_analysis_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{error::Error, fmt, sync::Mutex};
use once_cell::sync::Lazy;
use wasm_bindgen::prelude::wasm_bindgen;

use schema_analysis::{targets::json_typegen::OutputMode, InferredSchema, Schema};
use schema_analysis::{InferredSchema, Schema, targets::json_typegen::OutputMode};

// For some reason Rust Analyzer (clippy) errors with missing-unsafe if log! (or similar)
// is not in a nested function. Adding an unsafe block results in a rustc lint (unused_unsafe).
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn infer(data: Vec<u8>, file_type: DataType) -> Result<(), wasm_bindgen::JsV

mod infer {

use serde::{de::DeserializeSeed, Deserializer};
use serde::{Deserializer, de::DeserializeSeed};

use schema_analysis::InferredSchema;

Expand Down
Loading