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
pub mod fmt;
pub mod inflect;
pub mod metadata;

use crate::error::Result;
use metadata::Metadata;
use std::io::Write;
use std::path::PathBuf;

pub trait Target {
    type FileState: Default;

    fn strategy(&self) -> Strategy;
    fn name(&self, kind: NameableKind, name_parts: &[String]) -> String;
    fn expr(&self, state: &mut Self::FileState, metadata: Metadata, expr: Expr) -> String;
    fn item(
        &self,
        out: &mut dyn Write,
        state: &mut Self::FileState,
        item: Item,
    ) -> Result<Option<String>>;
}

#[derive(Debug)]
pub struct Strategy {
    pub file_partitioning: FilePartitioningStrategy,
    pub enum_member_naming: EnumMemberNamingStrategy,
    pub optional_property_handling: OptionalPropertyHandlingStrategy,
    pub booleans_are_nullable: bool,
    pub int8s_are_nullable: bool,
    pub uint8s_are_nullable: bool,
    pub int16s_are_nullable: bool,
    pub uint16s_are_nullable: bool,
    pub int32s_are_nullable: bool,
    pub uint32s_are_nullable: bool,
    pub float32s_are_nullable: bool,
    pub float64s_are_nullable: bool,
    pub strings_are_nullable: bool,
    pub timestamps_are_nullable: bool,
    pub arrays_are_nullable: bool,
    pub dicts_are_nullable: bool,
    pub aliases_are_nullable: bool,
    pub enums_are_nullable: bool,
    pub structs_are_nullable: bool,
    pub discriminators_are_nullable: bool,
}

#[derive(Debug)]
pub enum FilePartitioningStrategy {
    FilePerType(String),
    SingleFile(String),
}

#[derive(Debug)]
pub enum EnumMemberNamingStrategy {
    Modularized,
    Unmodularized,
}

#[derive(Debug)]
pub enum OptionalPropertyHandlingStrategy {
    NativeSupport,
    WrapWithNullable,
}

#[derive(Debug)]
pub enum NameableKind {
    Type,
    Field,
    EnumMember,
}

#[derive(Debug)]
pub enum Expr {
    Empty,
    Boolean,
    Int8,
    Uint8,
    Int16,
    Uint16,
    Int32,
    Uint32,
    Float32,
    Float64,
    String,
    Timestamp,
    ArrayOf(String),
    DictOf(String),
    NullableOf(String),
}

#[derive(Debug)]
pub enum Item {
    Auxiliary {
        out_dir: PathBuf,
    },
    Preamble,
    Alias {
        metadata: Metadata,
        name: String,
        type_: String,
    },
    Enum {
        metadata: Metadata,
        name: String,
        members: Vec<EnumMember>,
    },
    Struct {
        metadata: Metadata,
        name: String,
        has_additional: bool,
        fields: Vec<Field>,
    },
    Discriminator {
        metadata: Metadata,
        name: String,
        tag_field_name: String,
        tag_json_name: String,
        variants: Vec<DiscriminatorVariantInfo>,
    },
    DiscriminatorVariant {
        metadata: Metadata,
        name: String,
        parent_name: String,
        tag_field_name: String,
        tag_json_name: String,
        tag_value: String,
        has_additional: bool,
        fields: Vec<Field>,
    },
}

#[derive(Debug)]
pub struct EnumMember {
    pub name: String,
    pub json_value: String,
}

#[derive(Debug)]
pub struct Field {
    pub metadata: Metadata,
    pub name: String,
    pub json_name: String,
    pub optional: bool,
    pub type_: String,
}

#[derive(Debug)]
pub struct DiscriminatorVariantInfo {
    pub type_name: String,
    pub field_name: String,
    pub tag_value: String,
}