github-webhook-type-generator 0.5.2

GitHub webhook payload type generator for Rust
Documentation
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use std::collections::HashMap;

use crate::dag::CoDirectedAcyclicGraph;

pub struct RustComment(pub String);

pub enum RustSegment {
    Struct(RustStruct),
    Enum(RustEnum),
    Alias(RustAlias),
}

impl RustSegment {
    pub fn name(&self) -> &str {
        match self {
            RustSegment::Struct(s) => &s.name,
            RustSegment::Enum(e) => &e.name,
            RustSegment::Alias(a) => &a.name,
        }
    }
}

#[derive(Debug, Clone)]
pub struct TypeName {
    pub name: String,
    pub is_borrowed: bool,
}

impl TypeName {
    pub fn new(name: String) -> Self {
        Self {
            name,
            is_borrowed: false,
        }
    }
}

#[derive(Debug, Clone, Default)]
pub enum RustType {
    String {
        is_borrowed: bool,
    },
    Number,
    Boolean,
    Custom(TypeName),
    Array(Box<RustType>),
    Map(Box<Self>, Box<Self>),
    /// `()`
    #[default]
    Unit,
    Unknown,
    UnknownLiteral,
    UnknownIntersection,
}

impl RustType {
    pub fn to_ident(&self) -> &str {
        match self {
            RustType::String { .. } => "String",
            RustType::Number => "Number",
            RustType::Boolean => "Boolean",
            RustType::Custom(c) => &c.name,
            RustType::Array(t) => t.to_ident(),
            RustType::Unit => "Unit",
            RustType::Unknown => "Unknown",
            RustType::UnknownLiteral => "UnknownLiteral",
            RustType::UnknownIntersection => "UnknownIntersection",
            RustType::Map(..) => "Map",
        }
    }

    pub fn is_unknown(&self) -> bool {
        match &self {
            RustType::UnknownLiteral | RustType::UnknownIntersection => true,
            RustType::Array(t) => t.is_unknown(),
            RustType::Map(t1, t2) => t1.is_unknown() || t2.is_unknown(),
            RustType::Unknown
            | RustType::String { .. }
            | RustType::Number
            | RustType::Boolean
            | RustType::Custom(_)
            | RustType::Unit => false,
        }
    }

    pub fn is_borrowed(&self) -> bool {
        match self {
            RustType::String { is_borrowed } => *is_borrowed,
            RustType::Custom(t) => t.is_borrowed,
            RustType::Array(t) => t.is_borrowed(),
            RustType::Map(t1, t2) => t1.is_borrowed() || t2.is_borrowed(),
            RustType::Number
            | RustType::Boolean
            | RustType::Unit
            | RustType::Unknown
            | RustType::UnknownLiteral
            | RustType::UnknownIntersection => false,
        }
    }

    pub fn as_custom(&self) -> Option<&TypeName> {
        if let Self::Custom(t) = self {
            Some(t)
        } else {
            None
        }
    }

    pub fn as_mut_custom(&mut self) -> Option<&mut TypeName> {
        if let Self::Custom(t) = self {
            Some(t)
        } else {
            None
        }
    }

    pub fn get_using(&self) -> Option<&TypeName> {
        if let Self::Array(t) = self {
            t.get_using()
        } else {
            self.as_custom()
        }
    }

    /// Returns `true` if the rust type is [`String`].
    ///
    /// [`String`]: RustType::String
    #[must_use]
    pub fn is_string(&self) -> bool {
        matches!(self, Self::String { .. })
    }
}

pub struct RustStruct {
    pub attr: RustContainerAttrs,
    pub name: String,
    pub comment: Option<RustComment>,
    pub is_borrowed: bool,
    pub member: Vec<RustStructMember>,
}

impl RustStruct {
    pub fn from_members(
        name: String,
        comment: Option<RustComment>,
        members: impl Iterator<Item = RustStructMember>,
    ) -> Self {
        Self {
            attr: RustContainerAttrs::new(),
            name,
            comment,
            is_borrowed: false,
            member: members.collect(),
        }
    }
}

pub type RustContainerAttrs = Attrs<RustStructAttr>;

pub enum RustStructAttr {
    Serde(SerdeContainerAttr),
}

impl RustStructAttr {
    pub fn as_serde(&self) -> Option<&SerdeContainerAttr> {
        let Self::Serde(v) = self;
        Some(v)
    }
}

pub enum SerdeContainerAttr {
    RenameAll(RenameRule),
    Tag(String),
    Untagged,
}

impl SerdeContainerAttr {
    /// Returns `true` if the serde container attr is [`Tag`].
    ///
    /// [`Tag`]: SerdeContainerAttr::Tag
    #[must_use]
    pub fn is_tag(&self) -> bool {
        matches!(self, Self::Tag(..))
    }
}

#[derive(PartialEq)]
pub enum SerdeFieldAttr {
    Rename(String),
    Flatten,
    Borrow,
}

pub enum SerdeVariantAttr {
    Rename(String),
    Borrow,
}

pub enum RenameRule {
    PascalCase,
    SnakeCase,
    ScreamingSnakeCase,
}

impl RenameRule {
    /// Returns `true` if the rename rule is [`PascalCase`].
    ///
    /// [`PascalCase`]: RenameRule::PascalCase
    #[must_use]
    pub fn is_pascal_case(&self) -> bool {
        matches!(self, Self::PascalCase)
    }
    pub fn convert_to_pascal(&self, s: &mut String) {
        match self {
            RenameRule::PascalCase => (),
            RenameRule::SnakeCase | RenameRule::ScreamingSnakeCase => {
                *s = s
                    .split('_')
                    .map(|term| {
                        let mut term = term.to_ascii_lowercase();
                        if let Some(c) = term.chars().next() {
                            let capital_ch = c.to_ascii_uppercase();
                            term.replace_range(..1, &capital_ch.to_string());
                        }
                        term
                    })
                    .collect::<Vec<_>>()
                    .concat();
            }
        }
    }
    pub fn convert_to_snake(&self, s: &mut String) {
        match self {
            RenameRule::PascalCase => {
                *s = s
                    .chars()
                    .enumerate()
                    .fold(String::new(), |mut snake, (i, c)| {
                        if i > 0 && c.is_uppercase() {
                            snake.push('_');
                        }
                        snake.push(c.to_ascii_lowercase());
                        snake
                    });
            }
            _ => unimplemented!(),
        }
    }
}

impl ToString for RenameRule {
    fn to_string(&self) -> String {
        match self {
            RenameRule::PascalCase => "PascalCase",
            RenameRule::SnakeCase => "snake_case",
            RenameRule::ScreamingSnakeCase => "SCREAMING_SNAKE_CASE",
        }
        .to_string()
    }
}

pub struct RustEnum {
    pub attr: RustContainerAttrs,
    pub name: String,
    pub comment: Option<RustComment>,
    pub is_borrowed: bool,
    pub member: Vec<RustEnumMember>,
}

impl RustEnum {
    pub fn from_members(
        name: String,
        comment: Option<RustComment>,
        members: impl Iterator<Item = RustEnumMember>,
    ) -> Self {
        Self {
            attr: RustContainerAttrs::new(),
            name,
            comment,
            is_borrowed: false,
            member: members.collect(),
        }
    }
}

pub struct RustStructMember {
    pub attr: RustFieldAttrs,
    pub name: String,
    pub ty: RustMemberType,
    pub comment: Option<RustComment>,
}

pub type RustFieldAttrs = Attrs<RustFieldAttr>;

#[derive(Default)]
pub struct Attrs<Field>(Vec<Field>);

impl<T> Attrs<T> {
    pub fn add_attr(&mut self, a: T) {
        self.0.push(a)
    }
    pub fn from_attr(a: T) -> Self {
        let mut s = Self::new();
        s.add_attr(a);
        s
    }
    pub fn new() -> Self {
        Self(Vec::new())
    }

    pub fn as_inner(&self) -> &Vec<T> {
        &self.0
    }

    pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F) {
        self.0.retain(f)
    }
}

#[derive(PartialEq)]
pub enum RustFieldAttr {
    Serde(SerdeFieldAttr),
}

pub struct RustMemberType {
    pub ty: RustType,
    pub is_optional: bool,
}

impl RustMemberType {
    pub fn is_unknown(&self) -> bool {
        self.ty.is_unknown()
    }
}

pub struct RustEnumMember {
    pub attr: RustVariantAttrs,
    pub kind: RustEnumMemberKind,
}

impl From<RustEnumMemberKind> for RustEnumMember {
    fn from(value: RustEnumMemberKind) -> Self {
        Self {
            attr: RustVariantAttrs::new(),
            kind: value,
        }
    }
}

pub enum RustEnumMemberKind {
    Nullary(String),
    /// has the same ident. this is unary
    Unary(RustType),
    UnaryNamed {
        variant_name: String,
        type_name: RustType,
    },
}

impl RustEnumMemberKind {
    pub fn name_unary(&mut self, variant_name: String) {
        match self {
            RustEnumMemberKind::Unary(u) => {
                *self = Self::UnaryNamed {
                    variant_name,
                    type_name: u.clone(),
                }
            }
            _ => unreachable!("do not call with this"),
        }
    }

    /// Returns `true` if the rust enum member is [`Nullary`].
    ///
    /// [`Nullary`]: RustEnumMember::Nullary
    #[must_use]
    pub fn is_nullary(&self) -> bool {
        matches!(self, Self::Nullary(..))
    }

    pub fn as_unary(&self) -> Option<&RustType> {
        if let Self::Unary(v) = self {
            Some(v)
        } else {
            None
        }
    }

    pub fn as_type(&self) -> Option<&RustType> {
        match self {
            RustEnumMemberKind::Nullary(..) => None,
            RustEnumMemberKind::Unary(t) => Some(t),
            RustEnumMemberKind::UnaryNamed { type_name, .. } => Some(type_name),
        }
    }

    pub fn as_type_mut(&mut self) -> Option<&mut RustType> {
        match self {
            RustEnumMemberKind::Nullary(..) => None,
            RustEnumMemberKind::Unary(t) => Some(t),
            RustEnumMemberKind::UnaryNamed { type_name, .. } => Some(type_name),
        }
    }
}

pub type RustVariantAttrs = Attrs<RustVariantAttr>;

pub enum RustVariantAttr {
    Serde(SerdeVariantAttr),
}

pub struct RustAlias {
    pub name: String,
    pub is_borrowed: bool,
    pub comment: Option<RustComment>,
    pub ty: RustType,
}

pub type LiteralKeyMap = HashMap<String, HashMap<String, String>>;

pub fn type_deps(segments: &[RustSegment]) -> CoDirectedAcyclicGraph<usize> {
    let index_map: HashMap<_, _> = segments
        .iter()
        .enumerate()
        .map(|(i, s)| (s.name(), i))
        .collect();
    let mut type_deps = CoDirectedAcyclicGraph::new();
    for (i, segment) in segments.iter().enumerate() {
        let children: Vec<_> = match segment {
            RustSegment::Struct(s) => s
                .member
                .iter()
                .flat_map(|m| m.ty.ty.get_using())
                .map(|t| t.name.as_str())
                .collect(),
            RustSegment::Enum(e) => e
                .member
                .iter()
                .flat_map(|m| m.kind.as_type())
                .flat_map(|m| m.as_custom())
                .map(|tn| tn.name.as_str())
                .collect(),
            RustSegment::Alias(a) => {
                a.ty.get_using()
                    .map(|t| t.name.as_str())
                    .into_iter()
                    .collect()
            }
        };
        for child in children {
            if let Some(to) = index_map.get(child) {
                type_deps.add_edge(i, *to);
            }
        }
    }
    type_deps
}