torn-api-codegen 0.8.1

Contains the v2 torn API model descriptions and codegen for the bindings
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
use std::fmt::Write;

use heck::{ToSnakeCase, ToUpperCamelCase};
use indexmap::IndexMap;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};
use syn::Ident;

use crate::openapi::{
    parameter::OpenApiParameter,
    path::{OpenApiPath, OpenApiPathParameter, OpenApiResponseBody},
};

use super::{
    parameter::{Parameter, ParameterLocation, ParameterType},
    union::Union,
    ResolvedSchema, WarningReporter,
};

#[derive(Debug, Clone)]
pub enum PathSegment {
    Constant(String),
    Parameter { name: String },
}

pub struct PrettySegments<'a>(pub &'a [PathSegment]);

impl std::fmt::Display for PrettySegments<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for segment in self.0 {
            match segment {
                PathSegment::Constant(c) => write!(f, "/{c}")?,
                PathSegment::Parameter { name } => write!(f, "/{{{name}}}")?,
            }
        }

        Ok(())
    }
}

#[derive(Debug, Clone)]
pub enum PathParameter {
    Inline(Parameter),
    Component(Parameter),
}

#[derive(Debug, Clone)]
pub enum PathResponse {
    Component { name: String },
    // TODO: needs to be implemented
    ArbitraryUnion(Union),
}

#[derive(Debug, Clone)]
pub struct Path {
    pub segments: Vec<PathSegment>,
    pub name: String,
    pub summary: Option<String>,
    pub description: Option<String>,
    pub parameters: Vec<PathParameter>,
    pub response: PathResponse,
}

impl Path {
    pub fn from_schema(
        path: &str,
        schema: &OpenApiPath,
        parameters: &IndexMap<&str, OpenApiParameter>,
        warnings: WarningReporter,
    ) -> Option<Self> {
        let mut segments = Vec::new();
        for segment in path.strip_prefix('/')?.split('/') {
            if segment.starts_with('{') && segment.ends_with('}') {
                segments.push(PathSegment::Parameter {
                    name: segment[1..(segment.len() - 1)].to_owned(),
                });
            } else {
                segments.push(PathSegment::Constant(segment.to_owned()));
            }
        }

        let summary = schema.get.summary.as_deref().map(ToOwned::to_owned);
        let description = schema.get.description.as_deref().map(ToOwned::to_owned);

        let mut params = Vec::with_capacity(schema.get.parameters.len());
        for parameter in &schema.get.parameters {
            match &parameter {
                OpenApiPathParameter::Link { ref_path } => {
                    let name = ref_path
                        .strip_prefix("#/components/parameters/")?
                        .to_owned();
                    let param = parameters.get(&name.as_str())?;
                    params.push(PathParameter::Component(Parameter::from_schema(
                        &name,
                        param,
                        warnings.child(&name),
                    )?));
                }
                OpenApiPathParameter::Inline(schema) => {
                    let name = schema.name.to_upper_camel_case();
                    let parameter = Parameter::from_schema(&name, schema, warnings.clone())?;
                    params.push(PathParameter::Inline(parameter));
                }
            };
        }

        let mut suffixes = vec![];
        let mut name = String::new();

        for seg in &segments {
            match seg {
                PathSegment::Constant(val) => {
                    name.push_str(&val.to_upper_camel_case());
                }
                PathSegment::Parameter { name } => {
                    suffixes.push(format!("For{}", name.to_upper_camel_case()));
                }
            }
        }

        for suffix in suffixes {
            name.push_str(&suffix);
        }

        let response = match &schema.get.response_content {
            OpenApiResponseBody::Schema(link) => PathResponse::Component {
                name: link
                    .ref_path
                    .strip_prefix("#/components/schemas/")?
                    .to_owned(),
            },
            OpenApiResponseBody::Union { any_of: _ } => {
                PathResponse::ArbitraryUnion(Union::from_schema(
                    "Response",
                    &schema.get.response_content,
                    warnings.child("response"),
                )?)
            }
        };

        Some(Self {
            segments,
            name,
            summary,
            description,
            parameters: params,
            response,
        })
    }

    pub fn codegen_request(
        &self,
        resolved: &ResolvedSchema,
        warnings: WarningReporter,
    ) -> Option<TokenStream> {
        let name = if self.segments.len() == 1 {
            let Some(PathSegment::Constant(first)) = self.segments.first() else {
                return None;
            };
            format_ident!("{}Request", first.to_upper_camel_case())
        } else {
            format_ident!("{}Request", self.name)
        };

        let mut ns = PathNamespace {
            path: self,
            ident: None,
            elements: Vec::new(),
        };

        let mut fields = Vec::with_capacity(self.parameters.len());
        let mut convert_field = Vec::with_capacity(self.parameters.len());
        let mut start_fields = Vec::new();
        let mut discriminant = Vec::new();
        let mut discriminant_val = Vec::new();
        let mut fmt_val = Vec::new();

        for param in &self.parameters {
            let (is_inline, param) = match &param {
                PathParameter::Inline(param) => (true, param),
                PathParameter::Component(param) => (false, param),
            };

            let (ty, builder_param) = match &param.r#type {
                ParameterType::I32 { .. } | ParameterType::Enum { .. } => {
                    let ty_name = format_ident!("{}", param.name);

                    if is_inline {
                        ns.push_element(param.codegen(resolved)?);
                        let path = ns.get_ident();

                        (
                            quote! {
                                crate::request::models::#path::#ty_name
                            },
                            Some(quote! { #[cfg_attr(feature = "builder", builder(into))] }),
                        )
                    } else {
                        (
                            quote! {
                                crate::parameters::#ty_name
                            },
                            Some(quote! { #[cfg_attr(feature = "builder", builder(into))]}),
                        )
                    }
                }
                ParameterType::String => (quote! { String }, None),
                ParameterType::Boolean => (quote! { bool }, None),
                ParameterType::Schema { type_name } => {
                    let ty_name = format_ident!("{}", type_name);

                    (
                        quote! {
                            crate::models::#ty_name
                        },
                        None,
                    )
                }
                ParameterType::Array { .. } => {
                    ns.push_element(param.codegen(resolved)?);
                    let ty_name = param.r#type.codegen_type_name(&param.name);
                    let path = ns.get_ident();
                    (
                        quote! {
                            crate::request::models::#path::#ty_name
                        },
                        Some(quote! { #[cfg_attr(feature = "builder", builder(into))] }),
                    )
                }
            };

            let name = format_ident!("{}", param.name.to_snake_case());
            let query_val = &param.value;

            if param.location == ParameterLocation::Path {
                if self.segments.iter().any(|s| {
                    if let PathSegment::Parameter { name } = s {
                        name == &param.value
                    } else {
                        false
                    }
                }) {
                    discriminant.push(ty.clone());
                    discriminant_val.push(quote! { self.#name });
                    let path_name = format_ident!("{}", param.value);
                    start_fields.push(quote! {
                        #[cfg_attr(feature = "builder", builder(start_fn))]
                        #builder_param
                        pub #name: #ty
                    });
                    fmt_val.push(quote! {
                        #path_name=self.#name
                    });
                } else {
                    warnings.push(format!(
                        "Provided path parameter is not present in the url: {}",
                        param.value
                    ));
                }
            } else {
                let ty = if param.required {
                    convert_field.push(quote! {
                        parameters.push((#query_val, self.#name.to_string()));
                    });
                    ty
                } else {
                    convert_field.push(quote! {
                        if let Some(value) = &self.#name {
                            parameters.push((#query_val, value.to_string()));
                        }
                    });
                    quote! { Option<#ty>}
                };

                fields.push(quote! {
                    #builder_param
                    pub #name: #ty
                });
            }
        }

        let response_ty = match &self.response {
            PathResponse::Component { name } => {
                let name = format_ident!("{name}");
                quote! {
                    crate::models::#name
                }
            }
            PathResponse::ArbitraryUnion(union) => {
                let path = ns.get_ident();
                let ty_name = format_ident!("{}", union.name);

                quote! {
                    crate::request::models::#path::#ty_name
                }
            }
        };

        let mut path_fmt_str = String::new();
        for seg in &self.segments {
            match seg {
                PathSegment::Constant(val) => _ = write!(path_fmt_str, "/{val}"),
                PathSegment::Parameter { name } => _ = write!(path_fmt_str, "/{{{name}}}"),
            }
        }

        if let PathResponse::ArbitraryUnion(union) = &self.response {
            ns.push_element(union.codegen()?);
        }

        let ns = ns.codegen();

        start_fields.extend(fields);

        Some(quote! {
            #ns

            #[cfg_attr(feature = "builder", derive(bon::Builder))]
            #[derive(Debug, Clone)]
            #[cfg_attr(feature = "builder", builder(state_mod(vis = "pub(crate)"), on(String, into)))]
            pub struct #name {
                #(#start_fields),*
            }

            impl crate::request::IntoRequest for #name {
                #[allow(unused_parens)]
                type Discriminant = (#(#discriminant),*);
                type Response = #response_ty;
                fn into_request(self) -> (Self::Discriminant, crate::request::ApiRequest) {
                    let path = format!(#path_fmt_str, #(#fmt_val),*);
                    let mut parameters = Vec::new();
                    #(#convert_field)*

                    #[allow(unused_parens)]
                    (
                        (#(#discriminant_val),*),
                        crate::request::ApiRequest {
                            path,
                            parameters,
                        }
                    )
                }
            }
        })
    }

    pub fn codegen_scope_call(&self) -> Option<TokenStream> {
        let mut extra_args = Vec::new();
        let mut disc = Vec::new();

        let snake_name = self.name.to_snake_case();

        let request_name = format_ident!("{}Request", self.name);
        let builder_name = format_ident!("{}RequestBuilder", self.name);
        let builder_mod_name = format_ident!("{}_request_builder", snake_name);
        let request_mod_name = format_ident!("{snake_name}");

        let request_path = quote! { crate::request::models::#request_name };
        let builder_path = quote! { crate::request::models::#builder_name };
        let builder_mod_path = quote! { crate::request::models::#builder_mod_name };

        let tail = snake_name
            .split_once('_')
            .map_or_else(|| "for_selections".to_owned(), |(_, tail)| tail.to_owned());

        let fn_name = format_ident!("{tail}");

        for param in &self.parameters {
            let (param, is_inline) = match param {
                PathParameter::Inline(param) => (param, true),
                PathParameter::Component(param) => (param, false),
            };

            if param.location == ParameterLocation::Path
                && self.segments.iter().any(|s| {
                    if let PathSegment::Parameter { name } = s {
                        name == &param.value
                    } else {
                        false
                    }
                })
            {
                let ty = match &param.r#type {
                    ParameterType::I32 { .. } | ParameterType::Enum { .. } => {
                        let ty_name = format_ident!("{}", param.name);

                        if is_inline {
                            quote! {
                                crate::request::models::#request_mod_name::#ty_name
                            }
                        } else {
                            quote! {
                                crate::parameters::#ty_name
                            }
                        }
                    }
                    ParameterType::String => quote! { String },
                    ParameterType::Boolean => quote! { bool },
                    ParameterType::Schema { type_name } => {
                        let ty_name = format_ident!("{}", type_name);

                        quote! {
                            crate::models::#ty_name
                        }
                    }
                    ParameterType::Array { .. } => {
                        let ty_name = param.r#type.codegen_type_name(&param.name);

                        quote! {
                            crate::request::models::#request_mod_name::#ty_name
                        }
                    }
                };

                let arg_name = format_ident!("{}", param.value.to_snake_case());

                extra_args.push(quote! { #arg_name: #ty, });
                disc.push(arg_name);
            }
        }

        let response_ty = match &self.response {
            PathResponse::Component { name } => {
                let name = format_ident!("{name}");
                quote! {
                    crate::models::#name
                }
            }
            PathResponse::ArbitraryUnion(union) => {
                let name = format_ident!("{}", union.name);
                quote! {
                    crate::request::models::#request_mod_name::#name
                }
            }
        };

        let doc = match (&self.summary, &self.description) {
            (Some(summary), Some(description)) => {
                Some(format!("{summary}\n\n# Description\n{description}"))
            }
            (Some(summary), None) => Some(summary.clone()),
            (None, Some(description)) => Some(format!("# Description\n{description}")),
            (None, None) => None,
        };

        let doc = doc.map(|d| {
            quote! {
                #[doc = #d]
            }
        });

        Some(quote! {
            #doc
            pub async fn #fn_name<S>(
                self,
                #(#extra_args)*
                builder: impl FnOnce(
                    #builder_path<#builder_mod_path::Empty>
                ) -> #builder_path<S>,
            ) -> Result<#response_ty, E::Error>
            where
                S: #builder_mod_path::IsComplete,
            {
                let r = builder(#request_path::builder(#(#disc),*)).build();

                self.0.fetch(r).await
            }
        })
    }

    pub fn codegen_bulk_scope_call(&self) -> Option<TokenStream> {
        let mut disc = Vec::new();
        let mut disc_ty = Vec::new();

        let snake_name = self.name.to_snake_case();

        let request_name = format_ident!("{}Request", self.name);
        let builder_name = format_ident!("{}RequestBuilder", self.name);
        let builder_mod_name = format_ident!("{}_request_builder", snake_name);
        let request_mod_name = format_ident!("{snake_name}");

        let request_path = quote! { crate::request::models::#request_name };
        let builder_path = quote! { crate::request::models::#builder_name };
        let builder_mod_path = quote! { crate::request::models::#builder_mod_name };

        let tail = snake_name
            .split_once('_')
            .map_or_else(|| "for_selections".to_owned(), |(_, tail)| tail.to_owned());

        let fn_name = format_ident!("{tail}");

        for param in &self.parameters {
            let (param, is_inline) = match param {
                PathParameter::Inline(param) => (param, true),
                PathParameter::Component(param) => (param, false),
            };
            if param.location == ParameterLocation::Path
                && self.segments.iter().any(|s| {
                    if let PathSegment::Parameter { name } = s {
                        name == &param.value
                    } else {
                        false
                    }
                })
            {
                let ty = match &param.r#type {
                    ParameterType::I32 { .. } | ParameterType::Enum { .. } => {
                        let ty_name = format_ident!("{}", param.name);

                        if is_inline {
                            quote! {
                                crate::request::models::#request_mod_name::#ty_name
                            }
                        } else {
                            quote! {
                                crate::parameters::#ty_name
                            }
                        }
                    }
                    ParameterType::String => quote! { String },
                    ParameterType::Boolean => quote! { bool },
                    ParameterType::Schema { type_name } => {
                        let ty_name = format_ident!("{}", type_name);

                        quote! {
                            crate::models::#ty_name
                        }
                    }
                    ParameterType::Array { .. } => {
                        let name = param.r#type.codegen_type_name(&param.name);
                        quote! {
                            crate::request::models::#request_mod_name::#name
                        }
                    }
                };

                let arg_name = format_ident!("{}", param.value.to_snake_case());

                disc_ty.push(ty);
                disc.push(arg_name);
            }
        }

        if disc.is_empty() {
            return None;
        }

        let response_ty = match &self.response {
            PathResponse::Component { name } => {
                let name = format_ident!("{name}");
                quote! {
                    crate::models::#name
                }
            }
            PathResponse::ArbitraryUnion(union) => {
                let name = format_ident!("{}", union.name);
                quote! {
                    crate::request::models::#request_mod_name::#name
                }
            }
        };

        let disc = if disc.len() > 1 {
            quote! { (#(#disc),*) }
        } else {
            quote! { #(#disc),* }
        };

        let disc_ty = if disc_ty.len() > 1 {
            quote! { (#(#disc_ty),*) }
        } else {
            quote! { #(#disc_ty),* }
        };

        let doc = match (&self.summary, &self.description) {
            (Some(summary), Some(description)) => {
                Some(format!("{summary}\n\n# Description\n{description}"))
            }
            (Some(summary), None) => Some(summary.clone()),
            (None, Some(description)) => Some(format!("# Description\n{description}")),
            (None, None) => None,
        };

        let doc = doc.map(|d| {
            quote! {
                #[doc = #d]
            }
        });

        Some(quote! {
            #doc
            pub fn #fn_name<S, I, B>(
                self,
                ids: I,
                builder: B
            ) -> impl futures::Stream<Item = (#disc_ty, Result<#response_ty, E::Error>)>
            where
                I: IntoIterator<Item = #disc_ty>,
                S: #builder_mod_path::IsComplete,
                B: Fn(
                    #builder_path<#builder_mod_path::Empty>
                ) -> #builder_path<S>,
            {
                let requests = ids.into_iter()
                    .map(move |#disc| builder(#request_path::builder(#disc)).build());

                let executor = self.executor;
                executor.fetch_many(requests)
            }
        })
    }
}

pub struct PathNamespace<'r> {
    path: &'r Path,
    ident: Option<Ident>,
    elements: Vec<TokenStream>,
}

impl PathNamespace<'_> {
    pub fn get_ident(&mut self) -> Ident {
        self.ident
            .get_or_insert_with(|| {
                let name = self.path.name.to_snake_case();
                format_ident!("{name}")
            })
            .clone()
    }

    pub fn push_element(&mut self, el: TokenStream) {
        self.elements.push(el);
    }

    pub fn codegen(mut self) -> Option<TokenStream> {
        if self.elements.is_empty() {
            None
        } else {
            let ident = self.get_ident();
            let elements = self.elements;
            Some(quote! {
                pub mod #ident {
                    #(#elements)*
                }
            })
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use crate::openapi::schema::test::get_schema;

    #[test]
    fn resolve_paths() {
        let schema = get_schema();

        let mut paths = 0;
        let mut unresolved = vec![];

        for (name, desc) in &schema.paths {
            paths += 1;
            if Path::from_schema(
                name,
                desc,
                &schema.components.parameters,
                WarningReporter::new(),
            )
            .is_none()
            {
                unresolved.push(name);
            }
        }

        if !unresolved.is_empty() {
            panic!(
                "Failed to resolve {}/{} paths. Could not resolve [{}]",
                unresolved.len(),
                paths,
                unresolved
                    .into_iter()
                    .map(|u| format!("`{u}`"))
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        }
    }

    #[test]
    fn codegen_paths() {
        let schema = get_schema();
        let resolved = ResolvedSchema::from_open_api(&schema);
        let reporter = WarningReporter::new();

        let mut paths = 0;
        let mut unresolved = vec![];

        for (name, desc) in &schema.paths {
            paths += 1;
            let Some(path) =
                Path::from_schema(name, desc, &schema.components.parameters, reporter.clone())
            else {
                unresolved.push(name);
                continue;
            };

            if path.codegen_scope_call().is_none()
                || path.codegen_request(&resolved, reporter.clone()).is_none()
            {
                unresolved.push(name);
            }
        }

        if !unresolved.is_empty() {
            panic!(
                "Failed to codegen {}/{} paths. Could not resolve [{}]",
                unresolved.len(),
                paths,
                unresolved
                    .into_iter()
                    .map(|u| format!("`{u}`"))
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        }
    }
}