quick_type_schema/
langs.rs

1use super::*;
2
3#[derive(Debug, Clone, Hash, PartialEq, Eq)]
4pub enum Language {
5    Typescript(TypescriptOptions),
6    JsonSchema(JsonSchemaOptions),
7    CSharp(CSharpOptions),
8    Crystal(CrystalOptions),
9    Dart(DartOptions),
10    Elm(ElmOptions),
11    Go(GoOptions),
12    Haskell(HaskellOptions),
13    Python(PythonOptions),
14    Ruby(RubyOptions),
15    Rust(RustOptions),
16    Smithy(SmithyOptions),
17    Swift(SwiftOptions),
18}
19
20#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
21pub struct TypescriptOptions {
22    /// Interfaces only
23    pub just_types: bool,
24    /// Transform property names to be JavaScripty
25    pub nice_property_names: bool,
26    /// Explicitly name unions
27    pub explicit_unions: bool,
28    /// Use union type instead of enum
29    pub prefer_unions: bool,
30    /// Use types instead of interfaces
31    pub prefer_types: bool,
32    /// Use string instead of enum for string enums with single value
33    pub prefer_const_values: bool,
34    /// Use readonly type members
35    pub readonly: bool,
36}
37
38#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
39pub struct JsonSchemaOptions {}
40
41#[derive(Debug, Clone, Hash, PartialEq, Eq)]
42pub struct CSharpOptions {
43    /// Serialization framework
44    pub framework: CSharpFramework,
45    /// Use T[] or List<T>
46    pub array_type: ArrayOrList,
47    /// Property density
48    pub density: Density,
49    /// Generated namespace
50    pub namespace: String,
51    /// C# version
52    pub csharp_version: CSharpVersion,
53    /// Generate virtual properties
54    pub r#virtual: bool,
55    /// Type to use for "any"
56    pub any_type: CSharpAnyType,
57    /// Type to use for numbers
58    pub number_type: CSharpNumberType,
59    /// Output features
60    pub features: CSharpFeatures,
61    /// Base class
62    pub base_class: CSharpBaseClass,
63    /// Fail if required properties are missing
64    pub check_required: bool,
65    /// Keep original field name generate
66    pub keep_property_name: bool,
67}
68
69#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
70pub struct CrystalOptions {}
71
72#[derive(Debug, Clone, Hash, PartialEq, Eq)]
73pub struct DartOptions {
74    /// Null Safety
75    pub null_safety: bool,
76    /// Types only
77    pub just_types: bool,
78    /// Put encoder & decoder in Class
79    pub coders_in_class: bool,
80    /// Use method names fromMap() & toMap()
81    pub from_map: bool,
82    /// Make all properties required
83    pub required_props: bool,
84    /// Generate CopyWith method
85    pub copy_with: bool,
86    /// Generate class definitions with @freezed compatibility
87    pub use_freezed: bool,
88    /// Generate annotations for Hive type adapters
89    pub use_hive: bool,
90    /// Generate annotations for json_serializable
91    pub use_json_annotation: bool,
92    /// Use this name in `part` directive
93    pub part_name: String,
94}
95
96#[derive(Debug, Clone, Hash, PartialEq, Eq)]
97pub struct ElmOptions {
98    /// Plain types only
99    pub just_types: bool,
100    /// Use Array or List
101    pub array_type: ArrayOrList,
102    /// Generated module name
103    pub module: String,
104}
105
106#[derive(Debug, Clone, Hash, PartialEq, Eq)]
107pub struct GoOptions {
108    /// Plain types only
109    pub just_types: bool,
110    /// Plain types with package only
111    pub just_types_and_package: bool,
112    /// Generated package name
113    pub package: String,
114    // Exposing this attribute breaks codegen.
115    // multi_file_output: bool,
116    /// list of tags which should be generated for fields
117    pub field_tags: String,
118    /// If set, all non-required objects will be tagged with ",omitempty"
119    pub omit_empty: bool,
120}
121
122#[derive(Debug, Clone, Hash, PartialEq, Eq)]
123pub struct HaskellOptions {
124    /// Plain types only
125    pub just_types: bool,
126    /// Use Array or List
127    pub array_type: ArrayOrList,
128    /// Generated module name
129    pub module: String,
130}
131
132#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
133pub struct PythonOptions {
134    /// Python Version
135    pub python_version: PythonVersion,
136    /// Classes only
137    pub just_types: bool,
138    /// Transform property names to be Pythonic
139    pub nice_property_names: bool,
140}
141
142#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
143pub struct RubyOptions {
144    /// Plain types only
145    pub just_types: bool,
146    /// Type strictness
147    pub strictness: RubyStrictness,
148    /// Specify a wrapping Namespace
149    pub namespace: String,
150}
151
152#[derive(Debug, Clone, Hash, PartialEq, Eq)]
153pub struct RustOptions {
154    /// Density
155    pub density: Density,
156    /// Field visibility
157    pub visibility: RustVisibility,
158    /// Derive Debug impl
159    pub derive_debug: bool,
160    /// Derive Clone impl
161    pub derive_clone: bool,
162    /// Derive PartialEq impl
163    pub derive_partial_eq: bool,
164    /// Skip serializing empty Option fields
165    pub skip_serializing_none: bool,
166    /// Edition 2018
167    pub edition_2018: bool,
168    /// Leading Comments
169    pub leading_comments: bool,
170}
171
172#[derive(Debug, Clone, Hash, PartialEq, Eq)]
173pub struct SmithyOptions {
174    /// Serialization framework
175    pub framework: SmithyFramework,
176    /// Package
177    pub package: String,
178}
179
180#[derive(Debug, Clone, Hash, PartialEq, Eq)]
181pub struct SwiftOptions {
182    /// Plain types only
183    pub just_types: bool,
184    /// Generate initializers and mutators
185    pub convience_initializers: bool,
186    /// Explicit CodingKey values in Codable types
187    pub explicit_coding_keys: bool,
188    /// CodingKeys implements protocols
189    pub coding_keys_protocol: String,
190    /// Alamofire extensions
191    pub alamofire: bool,
192    /// Prefix for type names
193    pub type_prefix: String,
194    /// Prefix for type names
195    pub struct_or_class: StructOrClass,
196    /// Use var instead of let for object properties
197    pub mutable_properties: bool,
198    /// Acronym naming style
199    pub acronym_style: AcronymStyle,
200    /// Code density
201    pub density: Density,
202    /// Support Linux
203    pub support_linux: bool,
204    /// Objects inherit from NSObject and @objcMembers is added to classes
205    pub objective_c_support: bool,
206    /// If no matching case is found enum value is set to null
207    pub optional_enums: bool,
208    /// Renders output in a Swift 5 compatible mode
209    pub swift_5_support: bool,
210    /// Mark generated models as Sendable
211    pub sendable: bool,
212    // Exposing this attribute breaks codegen.
213    // multi_file_output: bool,
214    /// Access level
215    pub access_level: SwiftAccessLevel,
216    /// Make types implement protocol   
217    pub protocol: SwiftProtocol,
218}
219
220#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
221pub enum ArrayOrList {
222    #[default]
223    Array,
224    List,
225}
226
227impl ArrayOrList {
228    fn as_str(&self) -> &'static str {
229        match self {
230            ArrayOrList::Array => "array",
231            ArrayOrList::List => "list",
232        }
233    }
234}
235
236#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
237pub enum StructOrClass {
238    #[default]
239    Struct,
240    Class,
241}
242
243impl StructOrClass {
244    fn as_str(&self) -> &'static str {
245        match self {
246            StructOrClass::Struct => "struct",
247            StructOrClass::Class => "class",
248        }
249    }
250}
251
252#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
253pub enum Density {
254    #[default]
255    Normal,
256    Dense,
257}
258
259impl Density {
260    fn as_str(&self) -> &'static str {
261        match self {
262            Density::Normal => "normal",
263            Density::Dense => "dense",
264        }
265    }
266}
267
268#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
269pub enum AcronymStyle {
270    #[default]
271    Original,
272    Pascal,
273    Camel,
274    LowerCase,
275}
276
277impl AcronymStyle {
278    fn as_str(&self) -> &'static str {
279        match self {
280            AcronymStyle::Original => "original",
281            AcronymStyle::Pascal => "pascal",
282            AcronymStyle::Camel => "camel",
283            AcronymStyle::LowerCase => "lowercase",
284        }
285    }
286}
287
288#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
289pub enum CSharpFramework {
290    #[default]
291    NewtonSoft,
292    SystemTextJson,
293}
294
295#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
296pub enum CSharpNumberType {
297    #[default]
298    Double,
299    Decimal,
300}
301
302#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
303pub enum CSharpAnyType {
304    #[default]
305    Object,
306    Dynamic,
307}
308
309#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
310pub enum CSharpVersion {
311    V5,
312    #[default]
313    V6,
314}
315
316#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
317pub enum CSharpFeatures {
318    #[default]
319    Complete,
320    AttributesOnly,
321    JustTypesAndNamespace,
322    JustTypes,
323}
324
325#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
326pub enum CSharpBaseClass {
327    EntityData,
328    #[default]
329    Object,
330}
331
332#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
333pub enum PythonVersion {
334    V3_5,
335    #[default]
336    V3_6,
337    V3_7,
338}
339
340#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
341pub enum RubyStrictness {
342    #[default]
343    Strict,
344    Coercible,
345    None,
346}
347
348#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
349pub enum RustVisibility {
350    #[default]
351    Private,
352    Crate,
353    Public,
354}
355
356#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
357pub enum SmithyFramework {
358    #[default]
359    JustTypes,
360}
361
362#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
363pub enum SwiftAccessLevel {
364    #[default]
365    Internal,
366    Public,
367}
368
369#[derive(Debug, Clone, Hash, PartialEq, Eq, Default)]
370pub enum SwiftProtocol {
371    #[default]
372    None,
373    Equatable,
374    Hashable,
375}
376
377impl Language {
378    pub fn get_args(&self) -> Vec<String> {
379        match self {
380            Language::Typescript(opt) => CliBuilder::new(self)
381                .opt_bool("--just-types", opt.just_types)
382                .opt_bool("--nice-property-names", opt.nice_property_names)
383                .opt_bool("--explicit-unions", opt.explicit_unions)
384                .opt_bool("--prefer-unions", opt.prefer_unions)
385                .opt_bool("--prefer-types", opt.prefer_types)
386                .opt_bool("--prefer-const-values", opt.prefer_const_values)
387                .opt_bool("--readonly", opt.readonly)
388                .build(),
389            Language::JsonSchema(_) => CliBuilder::new(self).build(),
390            Language::CSharp(opt) => CliBuilder::new(self)
391                .opt_enum("--framework", &opt.framework, || match opt.framework {
392                    CSharpFramework::NewtonSoft => "NetwonSoft",
393                    CSharpFramework::SystemTextJson => "SystemTextJson",
394                })
395                .opt_enum("--array-type", &opt.array_type, || opt.array_type.as_str())
396                .opt_enum("--density", &opt.density, || opt.density.as_str())
397                .opt_string("--namespace", &opt.namespace)
398                .opt_enum("--csharp-version", &opt.csharp_version, || {
399                    match opt.csharp_version {
400                        CSharpVersion::V5 => "5",
401                        CSharpVersion::V6 => "6",
402                    }
403                })
404                .opt_bool("--virtual", opt.r#virtual)
405                .opt_enum("--any-type", &opt.any_type, || match opt.any_type {
406                    CSharpAnyType::Object => "object",
407                    CSharpAnyType::Dynamic => "dynamic",
408                })
409                .opt_enum("--number-type", &opt.number_type, || {
410                    match opt.number_type {
411                        CSharpNumberType::Double => "double",
412                        CSharpNumberType::Decimal => "decimal",
413                    }
414                })
415                .opt_enum("--features", &opt.features, || match opt.features {
416                    CSharpFeatures::Complete => "complete",
417                    CSharpFeatures::AttributesOnly => "attributes-only",
418                    CSharpFeatures::JustTypesAndNamespace => "just-types-and-namespace",
419                    CSharpFeatures::JustTypes => "just-types",
420                })
421                .opt_enum("--base-class", &opt.base_class, || match opt.base_class {
422                    CSharpBaseClass::EntityData => "EntityData",
423                    CSharpBaseClass::Object => "Object",
424                })
425                .opt_bool("--check-required", opt.check_required)
426                .opt_bool("--keep-property-name", opt.keep_property_name)
427                .build(),
428            Language::Crystal(_) => CliBuilder::new(self).build(),
429            Language::Dart(opt) => CliBuilder::new(self)
430                .opt_bool("--null-safety", opt.null_safety)
431                .opt_bool("--just-types", opt.just_types)
432                .opt_bool("--coders-in-class", opt.coders_in_class)
433                .opt_bool("--from-map", opt.from_map)
434                .opt_bool("--required-props", opt.required_props)
435                .opt_bool("--copy-with", opt.copy_with)
436                .opt_bool("--use-freezed", opt.use_freezed)
437                .opt_bool("--use-hive", opt.use_hive)
438                .opt_bool("--use-json-annotation", opt.use_json_annotation)
439                .opt_string("--part-name", &opt.part_name)
440                .build(),
441            Language::Elm(opt) => CliBuilder::new(self)
442                .opt_bool("--just-types", opt.just_types)
443                .opt_enum("--array-type", &opt.array_type, || opt.array_type.as_str())
444                .opt_string("--module", &opt.module)
445                .build(),
446            Language::Go(opt) => CliBuilder::new(self)
447                .opt_bool("--just-types", opt.just_types)
448                .opt_bool("--just-types-and-package", opt.just_types_and_package)
449                .opt_string("--package", &opt.package)
450                .opt_string("--field-tags", &opt.field_tags)
451                .opt_bool("--omit-empty", opt.omit_empty)
452                .build(),
453            Language::Haskell(opt) => CliBuilder::new(self)
454                .opt_bool("--just-types", opt.just_types)
455                .opt_enum("--array-type", &opt.array_type, || opt.array_type.as_str())
456                .opt_string("--module", &opt.module)
457                .build(),
458            Language::Python(opt) => CliBuilder::new(self)
459                .opt_enum("--python-version", &opt.python_version, || {
460                    match opt.python_version {
461                        PythonVersion::V3_5 => "3.5",
462                        PythonVersion::V3_6 => "3.6",
463                        PythonVersion::V3_7 => "3.7",
464                    }
465                })
466                .opt_bool("--just-types", opt.just_types)
467                .opt_bool("--nice-property-names", opt.nice_property_names)
468                .build(),
469            Language::Ruby(opt) => CliBuilder::new(self)
470                .opt_bool("--just-types", opt.just_types)
471                .opt_enum("--strictness", &opt.strictness, || match opt.strictness {
472                    RubyStrictness::Strict => "strict",
473                    RubyStrictness::Coercible => "coercible",
474                    RubyStrictness::None => "none",
475                })
476                .opt_string("--namespace", &opt.namespace)
477                .build(),
478            Language::Rust(opt) => CliBuilder::new(self)
479                .opt_enum("--density", &opt.density, || opt.density.as_str())
480                .opt_enum("--visibility", &opt.visibility, || match opt.visibility {
481                    RustVisibility::Private => "private",
482                    RustVisibility::Crate => "crate",
483                    RustVisibility::Public => "public",
484                })
485                .opt_bool("--derive-debug", opt.derive_debug)
486                .opt_bool("--derive-clone", opt.derive_clone)
487                .opt_bool("--derive-partial-eq", opt.derive_partial_eq)
488                .opt_bool("--skip-seriaziling-none", opt.skip_serializing_none)
489                .opt_bool("--edition-2018", opt.edition_2018)
490                .opt_bool("--leading-comments", opt.leading_comments)
491                .build(),
492            Language::Smithy(opt) => CliBuilder::new(self)
493                .opt_enum("--framework", &opt.framework, || match opt.framework {
494                    SmithyFramework::JustTypes => "just-types",
495                })
496                .opt_string("--package", &opt.package)
497                .build(),
498            Language::Swift(opt) => CliBuilder::new(self)
499                .opt_bool("--just-types", opt.just_types)
500                .opt_bool("--initializers", opt.convience_initializers)
501                .opt_bool("--coding-keys", opt.explicit_coding_keys)
502                .opt_string("--coding-keys-protocol", &opt.coding_keys_protocol)
503                .opt_bool("--alamofire", opt.alamofire)
504                .opt_string("--type-prefix", &opt.type_prefix)
505                .opt_enum("--struct-or-class", &opt.struct_or_class, || {
506                    opt.struct_or_class.as_str()
507                })
508                .opt_bool("--mutable-properties", opt.mutable_properties)
509                .opt_enum("--acronym-style", &opt.acronym_style, || {
510                    opt.acronym_style.as_str()
511                })
512                .opt_enum("--density", &opt.density, || opt.density.as_str())
513                .opt_bool("--support-linux", opt.support_linux)
514                .opt_bool("--objective-c-support", opt.objective_c_support)
515                .opt_bool("--optional-enums", opt.optional_enums)
516                .opt_bool("--swift-5-support", opt.swift_5_support)
517                .opt_bool("--sendable", opt.sendable)
518                .opt_enum("--access-level", &opt.access_level, || {
519                    match opt.access_level {
520                        SwiftAccessLevel::Internal => "internal",
521                        SwiftAccessLevel::Public => "public",
522                    }
523                })
524                .opt_enum("--protocol", &opt.protocol, || match opt.protocol {
525                    SwiftProtocol::None => "none",
526                    SwiftProtocol::Equatable => "equatable",
527                    SwiftProtocol::Hashable => "hashable",
528                })
529                .build(),
530        }
531    }
532
533    pub fn name(&self) -> &str {
534        match self {
535            Language::Typescript(_) => "typescript",
536            Language::JsonSchema(_) => "schema",
537            Language::CSharp(_) => "csharp",
538            Language::Crystal(_) => "crystal",
539            Language::Dart(_) => "dart",
540            Language::Elm(_) => "elm",
541            Language::Go(_) => "go",
542            Language::Haskell(_) => "haskell",
543            Language::Python(_) => "python",
544            Language::Ruby(_) => "ruby",
545            Language::Rust(_) => "rust",
546            Language::Smithy(_) => "smithy",
547            Language::Swift(_) => "swift",
548        }
549    }
550}
551
552impl Default for CSharpOptions {
553    fn default() -> Self {
554        Self {
555            framework: CSharpFramework::default(),
556            array_type: ArrayOrList::default(),
557            density: Density::default(),
558            namespace: "QuickType".to_owned(),
559            csharp_version: CSharpVersion::default(),
560            r#virtual: false,
561            any_type: CSharpAnyType::default(),
562            number_type: CSharpNumberType::default(),
563            features: CSharpFeatures::default(),
564            base_class: CSharpBaseClass::default(),
565            check_required: false,
566            keep_property_name: false,
567        }
568    }
569}
570
571impl Default for DartOptions {
572    fn default() -> Self {
573        Self {
574            null_safety: true,
575            just_types: false,
576            coders_in_class: false,
577            from_map: false,
578            required_props: false,
579            copy_with: false,
580            use_freezed: false,
581            use_hive: false,
582            use_json_annotation: false,
583            part_name: "".to_owned(),
584        }
585    }
586}
587
588impl Default for ElmOptions {
589    fn default() -> Self {
590        Self {
591            just_types: false,
592            array_type: ArrayOrList::default(),
593            module: "QuickType".to_owned(),
594        }
595    }
596}
597
598impl Default for GoOptions {
599    fn default() -> Self {
600        Self {
601            just_types: false,
602            just_types_and_package: false,
603            package: "main".to_owned(),
604            field_tags: "json".to_owned(),
605            omit_empty: false,
606        }
607    }
608}
609
610impl Default for HaskellOptions {
611    fn default() -> Self {
612        Self {
613            just_types: false,
614            array_type: ArrayOrList::default(),
615            module: "QuickType".to_owned(),
616        }
617    }
618}
619
620impl Default for RustOptions {
621    fn default() -> Self {
622        Self {
623            density: Density::default(),
624            visibility: RustVisibility::default(),
625            derive_debug: false,
626            derive_clone: false,
627            derive_partial_eq: false,
628            skip_serializing_none: false,
629            edition_2018: true,
630            leading_comments: true,
631        }
632    }
633}
634
635impl Default for SmithyOptions {
636    fn default() -> Self {
637        Self {
638            framework: SmithyFramework::default(),
639            package: "quicktype".to_owned(),
640        }
641    }
642}
643
644impl Default for SwiftOptions {
645    fn default() -> Self {
646        Self {
647            just_types: false,
648            convience_initializers: true,
649            explicit_coding_keys: true,
650            coding_keys_protocol: "".to_owned(),
651            alamofire: false,
652            type_prefix: "".to_owned(),
653            struct_or_class: StructOrClass::default(),
654            mutable_properties: false,
655            acronym_style: AcronymStyle::Pascal,
656            density: Density::default(),
657            support_linux: false,
658            objective_c_support: false,
659            optional_enums: false,
660            swift_5_support: false,
661            sendable: false,
662            access_level: SwiftAccessLevel::default(),
663            protocol: SwiftProtocol::default(),
664        }
665    }
666}