1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
//! Schema generator and its settings.
mod naming_strategy;
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use self::naming_strategy::NamingStrategy;
use crate::schema::{RootSchema, Schema, SchemaType};
use crate::type_id::{type_id, TypeId};
use crate::{JsonTypedef, Names};
/// A configurable schema generator. An instance is meant to produce one
/// [`RootSchema`] and be consumed in the process.
///
/// If you want to just use the sane defaults, try [`Generator::default()`].
///
/// Otherwise, you can configure schema generation using the builder.
///
/// # Examples
///
/// Using the default settings:
///
/// ```
/// use jtd_derive::{JsonTypedef, Generator};
///
/// #[derive(JsonTypedef)]
/// struct Foo {
/// x: u32,
/// }
///
/// let root_schema = Generator::default().into_root_schema::<Foo>().unwrap();
/// let json_schema = serde_json::to_value(&root_schema).unwrap();
///
/// assert_eq!(json_schema, serde_json::json!{ {
/// "properties": {
/// "x": { "type": "uint32" }
/// },
/// "additionalProperties": true,
/// } });
/// ```
///
/// Using custom settings:
///
/// ```
/// use jtd_derive::{JsonTypedef, Generator};
///
/// #[derive(JsonTypedef)]
/// struct Foo {
/// x: u32,
/// }
///
/// let root_schema = Generator::builder()
/// .top_level_ref()
/// .naming_short()
/// .build()
/// .into_root_schema::<Foo>()
/// .unwrap();
/// let json_schema = serde_json::to_value(&root_schema).unwrap();
///
/// assert_eq!(json_schema, serde_json::json!{ {
/// "definitions": {
/// "Foo": {
/// "properties": {
/// "x": { "type": "uint32" }
/// },
/// "additionalProperties": true,
/// }
/// },
/// "ref": "Foo",
/// } });
/// ```
#[derive(Default, Debug)]
pub struct Generator {
naming_strategy: NamingStrategy,
/// Types for which at least one ref was created during schema gen.
/// By keeping track of these, we can clean up unused definitions at the end.
refs: HashSet<TypeId>,
definitions: HashMap<TypeId, (Names, DefinitionState)>,
inlining: Inlining,
}
impl Generator {
/// Provide a `Generator` builder, allowing for some customization.
pub fn builder() -> GeneratorBuilder {
GeneratorBuilder::default()
}
/// Generate the root schema for the given type according to the settings.
/// This consumes the generator.
///
/// This will return an error if a naming collision is detected, i.e. two
/// distinct Rust types produce the same identifier.
pub fn into_root_schema<T: JsonTypedef>(mut self) -> Result<RootSchema, GenError> {
let schema = self.sub_schema_impl::<T>(true);
self.clean_up_defs();
fn process_defs(
defs: HashMap<TypeId, (Names, DefinitionState)>,
ns: &mut NamingStrategy,
) -> Result<HashMap<String, Schema>, GenError> {
// This could probably be optimized somehow.
let defs = defs
.into_iter()
.map(|(_, (n, s))| (ns.fun()(&n), (n, s.unwrap())));
let mut map = HashMap::new();
for (key, (names, schema)) in defs {
if let Some((other_names, _)) = map.get(&key) {
return Err(GenError::NameCollision {
id: key,
type1: NamingStrategy::long().fun()(other_names),
type2: NamingStrategy::long().fun()(&names),
});
} else {
map.insert(key, (names, schema));
}
}
Ok(map
.into_iter()
.map(|(key, (_, schema))| (key, schema))
.collect())
}
Ok(RootSchema {
definitions: process_defs(self.definitions, &mut self.naming_strategy)?,
schema,
})
}
/// Generate a [`Schema`] for a given type, adding definitions to the
/// generator as appropriate.
///
/// This is meant to only be called when implementing [`JsonTypedef`] for
/// new types. Most commonly you'll derive that trait. It's unlikely you'll
/// need to call this method explicitly.
pub fn sub_schema<T: JsonTypedef + ?Sized>(&mut self) -> Schema {
self.sub_schema_impl::<T>(false)
}
fn sub_schema_impl<T: JsonTypedef + ?Sized>(&mut self, top_level: bool) -> Schema {
let id = type_id::<T>();
let inlining = match self.inlining {
Inlining::Always => true,
Inlining::Normal => top_level,
Inlining::Never => false,
};
let inlined_schema = match self.definitions.get(&id) {
Some((_, DefinitionState::Finished(schema))) => {
// we had already built a schema for this type.
// no need to do it again.
(!T::referenceable() || (inlining && !self.refs.contains(&id)))
.then_some(schema.clone())
}
Some((_, DefinitionState::Processing)) => {
// we're already in the process of building a schema for this type.
// this means it's recursive and the only way to keep things sane
// is to go by reference
None
}
None => {
// no schema available yet, so we have to build it
if T::referenceable() {
self.definitions
.insert(id, (T::names(), DefinitionState::Processing));
let schema = T::schema(self);
self.definitions
.get_mut(&id)
.unwrap()
.1
.finalize(schema.clone());
(inlining && !self.refs.contains(&id)).then_some(schema)
} else {
Some(T::schema(self))
}
}
};
inlined_schema.unwrap_or_else(|| {
let schema = Schema {
ty: SchemaType::Ref {
r#ref: self.naming_strategy.fun()(&T::names()),
},
..Schema::default()
};
self.refs.insert(id);
schema
})
}
fn clean_up_defs(&mut self) {
let to_remove: Vec<_> = self
.definitions
.keys()
.filter(|names| !self.refs.contains(names))
.cloned()
.collect();
for names in to_remove {
self.definitions.remove(&names);
}
}
}
#[derive(Debug, Clone, Copy)]
enum Inlining {
Always,
Normal,
Never,
}
impl Default for Inlining {
fn default() -> Self {
Inlining::Normal
}
}
/// Builder for [`Generator`]. For example usage, refer to [`Generator`].
#[derive(Default, Debug)]
pub struct GeneratorBuilder {
inlining: Inlining,
naming_strategy: Option<NamingStrategy>,
}
impl GeneratorBuilder {
/// Always try to inline complex types rather than provide them using
/// definitions/refs. The exception is recursive types - these cannot
/// be expressed without a ref.
pub fn prefer_inline(&mut self) -> &mut Self {
self.inlining = Inlining::Always;
self
}
/// Where possible, provide types by ref even for the top-level type.
pub fn top_level_ref(&mut self) -> &mut Self {
self.inlining = Inlining::Never;
self
}
/// A naming strategy that produces the stringified name
/// of the type with type parameters and const parameters in angle brackets.
///
/// E.g. if you have a struct like this in the top-level of `my_crate`:
///
/// ```
/// #[derive(jtd_derive::JsonTypedef)]
/// struct Foo<T, const N: usize> {
/// x: [T; N],
/// }
/// ```
///
/// Then the concrete type `Foo<u32, 5>` will be named `"Foo<uint32, 5>"`
/// in the schema.
///
/// Please note that this representation is prone to name collisions if you
/// use identically named types in different modules or crates.
pub fn naming_short(&mut self) -> &mut Self {
self.naming_strategy = Some(NamingStrategy::short());
self
}
/// Use the `long` naming strategy. This is the default.
///
/// The `long` naming strategy produces the stringified full path
/// of the type with type parameters and const parameters in angle brackets.
///
/// E.g. if you have a struct like this in the top-level of `my_crate`:
///
/// ```
/// #[derive(jtd_derive::JsonTypedef)]
/// struct Foo<T, const N: usize> {
/// x: [T; N],
/// }
/// ```
///
/// Then the concrete type `Foo<u32, 5>` will be named `"my_crate::Foo<uint32, 5>"`
/// in the schema.
///
/// This representation will prevent name collisions under normal circumstances,
/// but it's technically possible some type will manually implement `names()`
/// in a weird way.
pub fn naming_long(&mut self) -> &mut Self {
self.naming_strategy = Some(NamingStrategy::long());
self
}
/// Use a custom naming strategy.
pub fn naming_custom(&mut self, f: impl Fn(&Names) -> String + 'static) -> &mut Self {
self.naming_strategy = Some(NamingStrategy::custom(f));
self
}
/// Finalize the configuration and get a `Generator`.
pub fn build(&mut self) -> Generator {
Generator {
inlining: self.inlining,
naming_strategy: self.naming_strategy.take().unwrap_or_default(),
..Generator::default()
}
}
}
#[derive(Debug, Clone)]
enum DefinitionState {
Finished(Schema),
Processing,
}
impl DefinitionState {
fn unwrap(self) -> Schema {
if let Self::Finished(schema) = self {
schema
} else {
panic!()
}
}
fn finalize(&mut self, schema: Schema) {
match self {
DefinitionState::Finished(_) => panic!("schema already finalized"),
DefinitionState::Processing => *self = DefinitionState::Finished(schema),
}
}
}
impl Default for DefinitionState {
fn default() -> Self {
Self::Processing
}
}
/// Schema generation errors.
#[derive(Debug, Clone, PartialEq, Eq, Hash, thiserror::Error)]
pub enum GenError {
/// A name collision was detected, i.e. two distinct types have the same
/// definition/ref identifiers.
#[error("definition/ref id \"{id}\" is shared by types `{type1}` and `{type2}`")]
NameCollision {
type1: String,
type2: String,
id: String,
},
}