json_schema_rs/code_gen/settings.rs
1//! Code generation settings (model naming, dedupe mode, etc.).
2
3/// How to choose the generated struct/type name when both `title` and property key are available.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
5pub enum ModelNameSource {
6 /// Use `title` first, then property key, then `"Root"` for the root schema. (Current behavior.)
7 #[default]
8 TitleFirst,
9 /// Use property key first, then `title`, then `"Root"` for the root schema.
10 PropertyKeyFirst,
11}
12
13/// Whether and how to deduplicate structurally identical object schemas across and within schemas.
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
15pub enum DedupeMode {
16 /// No deduping. Identical Rust models are generated duplicately.
17 Disabled,
18 /// Only pivotal/functional data is considered (type_, properties, required, title, constraints).
19 /// Excludes non-functional fields such as description. Comparison is deep.
20 Functional,
21 /// Everything is considered, including non-functional fields like description. Comparison is deep.
22 #[default]
23 Full,
24}
25
26/// Language-agnostic code generation settings.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct CodeGenSettings {
29 /// Which source to prefer for struct/type names: title or property key.
30 pub model_name_source: ModelNameSource,
31 /// Whether and how to deduplicate identical object schemas (Disabled, Functional, Full).
32 pub dedupe_mode: DedupeMode,
33}
34
35impl Default for CodeGenSettings {
36 fn default() -> Self {
37 Self {
38 model_name_source: ModelNameSource::TitleFirst,
39 dedupe_mode: DedupeMode::Full,
40 }
41 }
42}
43
44/// Builder for [`CodeGenSettings`].
45#[derive(Debug, Clone, Default)]
46pub struct CodeGenSettingsBuilder {
47 model_name_source: Option<ModelNameSource>,
48 dedupe_mode: Option<DedupeMode>,
49}
50
51impl CodeGenSettingsBuilder {
52 /// Set the model name source (title first vs property key first).
53 #[must_use]
54 pub fn model_name_source(mut self, value: ModelNameSource) -> Self {
55 self.model_name_source = Some(value);
56 self
57 }
58
59 /// Set the dedupe mode (Disabled, Functional, or Full).
60 #[must_use]
61 pub fn dedupe_mode(mut self, value: DedupeMode) -> Self {
62 self.dedupe_mode = Some(value);
63 self
64 }
65
66 /// Build the settings. Any option not set uses its per-option default.
67 #[must_use]
68 pub fn build(self) -> CodeGenSettings {
69 CodeGenSettings {
70 model_name_source: self.model_name_source.unwrap_or_default(),
71 dedupe_mode: self.dedupe_mode.unwrap_or_default(),
72 }
73 }
74}
75
76impl CodeGenSettings {
77 /// Start a builder with all options unset (per-option defaults will be used on [`build`](CodeGenSettingsBuilder::build)).
78 #[must_use]
79 pub fn builder() -> CodeGenSettingsBuilder {
80 CodeGenSettingsBuilder::default()
81 }
82}