-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathlib.rs
1179 lines (958 loc) · 34.9 KB
/
lib.rs
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
use std::fmt::{Debug, Display, Formatter};
use anyhow::anyhow;
use derive_more::From;
use indexmap::IndexMap;
// Re-export crates whose types we expose publicly
pub use ::once_cell;
pub use ::indexmap;
pub use ::anyhow;
// Child modules
pub mod builtins;
pub mod transform;
use once_cell::sync::Lazy;
use serde::de::SeqAccess;
use serde::ser::SerializeSeq;
use serde::{Deserialize, Serialize};
#[allow(clippy::trivially_copy_pass_by_ref)] // needs to match signature for use in serde attribute
#[inline]
const fn is_false(v: &bool) -> bool {
!(*v)
}
// String type where efficient cloning brings significant improvements
// ArcStr is a compact reference counted string that makes cloning a very cheap operation.
// This is particularly important for `TypeName` that is cloned a lot, e.g. for `IndexedModel` keys
// See https://swatinem.de/blog/optimized-strings/ for a general discussion
//
// TODO: interning at deserialization time to reuse identical values (links from values to types)
type FastString = arcstr::ArcStr;
pub trait Documented {
fn doc_url(&self) -> Option<&str>;
fn doc_id(&self) -> Option<&str>;
fn description(&self) -> Option<&str>;
}
pub trait ExternalDocument {
fn ext_doc_id(&self) -> Option<&str>;
fn ext_doc_url(&self) -> Option<&str>;
fn ext_doc_description(&self) -> Option<&str>;
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TypeName {
// Order is important for Ord implementation
pub namespace: FastString,
pub name: FastString,
}
impl TypeName {
pub fn new(namespace: &str, name: &str) -> TypeName {
TypeName {
namespace: namespace.into(),
name: name.into(),
}
}
pub fn is_builtin(&self) -> bool {
self.namespace == "_builtins"
}
}
/// Creates a constant `TypeName` from static strings
#[macro_export]
macro_rules! type_name {
($namespace:expr,$name:expr) => {
TypeName {
namespace: arcstr::literal!($namespace),
name: arcstr::literal!($name),
}
};
}
impl Display for TypeName {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.namespace, self.name)
}
}
//-----------------------------------------------------------------------------
/// Type of a value. Used both for property types and nested type definitions.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd, From)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum ValueOf {
InstanceOf(InstanceOf),
ArrayOf(ArrayOf),
UnionOf(UnionOf),
DictionaryOf(DictionaryOf),
UserDefinedValue(UserDefinedValue),
LiteralValue(LiteralValue),
}
impl From<TypeName> for ValueOf {
fn from(name: TypeName) -> Self {
ValueOf::InstanceOf(InstanceOf::new(name))
}
}
impl From<&TypeName> for ValueOf {
fn from(name: &TypeName) -> Self {
ValueOf::InstanceOf(InstanceOf::new(name.clone()))
}
}
impl From<&Lazy<TypeName>> for ValueOf {
fn from(name: &Lazy<TypeName>) -> Self {
ValueOf::InstanceOf(InstanceOf::new((*name).clone()))
}
}
//-----------------------------------------------------------------------------
/// A single value
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
pub struct InstanceOf {
#[serde(rename = "type")]
pub typ: TypeName,
/// generic parameters: either concrete types or open parameters from the enclosing type
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub generics: Vec<ValueOf>,
}
impl InstanceOf {
pub fn new(name: TypeName) -> InstanceOf {
InstanceOf {
typ: name,
generics: Vec::default(),
}
}
}
/// An array
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
pub struct ArrayOf {
pub value: Box<ValueOf>,
}
/// One of several possible types which don't necessarily have a common superclass
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
pub struct UnionOf {
pub items: Vec<ValueOf>,
}
/// A dictionary (or map). The key is a string or a number (or a union thereof), possibly through an alias.
///
/// If `singleKey` is true, then this dictionary can only have a single key. This is a common pattern in ES APIs,
/// used to associate a value to a field name or some other identifier.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
#[serde(rename_all = "camelCase")]
pub struct DictionaryOf {
pub key: Box<ValueOf>,
pub value: Box<ValueOf>,
pub single_key: bool,
}
/// A user defined value. To be used when bubbling a generic parameter up to the top-level class is
/// inconvenient or impossible (e.g. for lists of user-defined values of possibly different types).
///
/// Clients will allow providing a serializer/deserializer when reading/writing properties of this type,
/// and should also accept raw json.
///
/// Think twice before using this as it defeats the purpose of a strongly typed API, and deserialization
/// will also require to buffer raw JSON data which may have performance implications.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
#[serde(rename_all = "camelCase")]
pub struct UserDefinedValue {}
/// A literal value. This is used for tagged unions, where each type member of a union has a 'type'
/// attribute that defines its kind. This metamodel heavily uses this approach with its 'kind' attributes.
///
/// It may later be used to set a property to a constant value, which is why it accepts not only strings but also
/// other primitive types.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
#[serde(rename_all = "camelCase")]
pub struct LiteralValue {
pub value: LiteralValueValue,
}
impl Display for LiteralValue {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.value, f)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
#[serde(untagged)]
pub enum LiteralValueValue {
String(String),
Number(f64),
Boolean(bool),
}
impl Display for LiteralValueValue {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
LiteralValueValue::String(x) => Display::fmt(x, f),
LiteralValueValue::Number(x) => Display::fmt(x, f),
LiteralValueValue::Boolean(x) => Display::fmt(x, f),
}
}
}
//--------------------------------------------------------------------------------------------
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Stability {
Stable,
Beta,
Experimental,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Visibility {
Public,
FeatureFlag,
Private,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Deprecation {
pub version: String,
pub description: String,
}
/// An API flavor
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq, clap::ValueEnum)]
#[serde(rename_all = "snake_case")]
pub enum Flavor {
Stack,
Serverless,
}
/// The availability of an item in a API flavor
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Availability {
pub since: Option<String>,
pub stability: Option<Stability>,
pub visibility: Option<Visibility>,
}
/// The availability of an
pub type Availabilities = IndexMap<Flavor, Availability>;
pub trait AvailabilityFilter: Fn(&Option<Availabilities>) -> bool {}
impl Flavor {
/// Predicate that indicates if a flavor is available in a given set of availabilities
pub fn available(&self, availabilities: &Option<Availabilities>) -> bool {
if let Some(ref avail) = availabilities {
avail.contains_key(self)
} else {
// No restriction
true
}
}
/// Gets the visibility for a given set of availabilities. If the result is `None`,
/// this flavor isn't available.
pub fn visibility(&self, availabilities: &Option<Availabilities>) -> Option<Visibility> {
if let Some(ref availabilities) = availabilities {
// Some availabilities defined
if let Some(availability) = availabilities.get(self) {
// This one exists. Public by default
availability.visibility.clone().or(Some(Visibility::Public))
} else {
// Not available
None
}
} else {
// No restriction: available and public
Some(Visibility::Public)
}
}
}
/// An interface or request interface property.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Property {
pub name: String,
#[serde(rename = "type")]
pub typ: ValueOf,
pub required: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub doc_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub doc_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ext_doc_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ext_doc_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ext_doc_description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub server_default: Option<ServerDefault>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecation: Option<Deprecation>,
#[serde(skip_serializing_if = "Option::is_none")]
pub availability: Option<Availabilities>,
/// If specified takes precedence over `name` when generating code. `name` is always the value
/// to be sent over the wire
#[serde(skip_serializing_if = "Option::is_none")]
pub codegen_name: Option<String>,
/// An optional set of aliases for `name`
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub aliases: Vec<String>,
/// If the enclosing class is a variants container, is this a property of the container and not a variant?
#[serde(default, skip_serializing_if = "is_false")]
pub container_property: bool,
/// If this property has a quirk that needs special attention, give a short explanation about it
#[serde(skip_serializing_if = "Option::is_none")]
pub es_quirk: Option<String>,
}
impl Documented for Property {
fn doc_url(&self) -> Option<&str> {
self.doc_url.as_deref()
}
fn doc_id(&self) -> Option<&str> {
self.doc_id.as_deref()
}
fn description(&self) -> Option<&str> {
self.description.as_deref()
}
}
impl ExternalDocument for Property {
fn ext_doc_url(&self) -> Option<&str> {
self.ext_doc_url.as_deref()
}
fn ext_doc_id(&self) -> Option<&str> {
self.ext_doc_id.as_deref()
}
fn ext_doc_description(&self) -> Option<&str> {
self.ext_doc_description.as_deref()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ServerDefault {
Boolean(bool),
String(String),
Number(f64),
StringArray(Vec<String>),
NumberArray(Vec<String>),
}
//--------------------------------------------------------------------------------------------
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum Variants {
ExternalTag(ExternalTag),
InternalTag(InternalTag),
Untagged(Untagged),
Container(Container),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ExternalTag {
#[serde(default)]
pub non_exhaustive: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InternalTag {
#[serde(default)]
pub non_exhaustive: bool,
/// Name of the property that holds the variant tag
pub tag: String,
/// Default value for the variant tag if it's missing
#[serde(skip_serializing_if = "Option::is_none")]
pub default_tag: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Untagged {
#[serde(default)]
pub non_exhaustive: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Container {
#[serde(default)]
pub non_exhaustive: bool,
}
/// Inherits clause (aka extends or implements) for an interface or request
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Inherits {
#[serde(rename = "type")]
pub typ: TypeName,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub generics: Vec<ValueOf>,
}
#[derive(Debug, Clone, Serialize, Deserialize, From)]
#[serde(rename_all = "snake_case", tag = "kind")]
#[allow(clippy::large_enum_variant)]
pub enum TypeDefinition {
Interface(Interface),
Request(Request),
Response(Response),
Enum(Enum),
TypeAlias(TypeAlias),
}
impl TypeDefinition {
pub fn name(&self) -> &TypeName {
&self.base().name
}
pub fn base(&self) -> &BaseType {
use TypeDefinition::*;
match self {
Interface(x) => &x.base,
Request(x) => &x.base,
Response(x) => &x.base,
Enum(x) => &x.base,
TypeAlias(x) => &x.base,
}
}
pub fn base_mut(&mut self) -> &mut BaseType {
use TypeDefinition::*;
match self {
Interface(x) => &mut x.base,
Request(x) => &mut x.base,
Response(x) => &mut x.base,
Enum(x) => &mut x.base,
TypeAlias(x) => &mut x.base,
}
}
}
/// Common attributes for all type definitions
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BaseType {
pub name: TypeName,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// Link to public documentation
#[serde(skip_serializing_if = "Option::is_none")]
pub doc_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub doc_id: Option<String>,
/// Link to public documentation
#[serde(skip_serializing_if = "Option::is_none")]
pub ext_doc_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ext_doc_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ext_doc_description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecation: Option<Deprecation>,
/// If this endpoint has a quirk that needs special attention, give a short explanation about it
#[serde(skip_serializing_if = "Option::is_none")]
pub es_quirk: Option<String>,
/// Variant name for externally tagged variants
#[serde(skip_serializing_if = "Option::is_none")]
pub variant_name: Option<String>,
/// Additional identifiers for use by code generators. Usage depends on the actual type:
/// - on unions (modeled as alias(union_of)), these are identifiers for the union members
/// - for additional properties, this is the name of the dict that holds these properties
/// - for additional property, this is the name of the key and value fields that hold the additional property
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub codegen_names: Vec<String>,
/// Location of an item. The path is relative to the "specification" directory, e.g "_types/common.ts#L1-L2"
#[serde(skip_serializing_if = "Option::is_none")]
pub spec_location: Option<String>,
}
impl BaseType {
pub fn new(name: TypeName) -> BaseType {
BaseType {
name,
codegen_names: Default::default(),
doc_id: None,
doc_url: None,
deprecation: None,
es_quirk: None,
description: None,
variant_name: None,
spec_location: None,
ext_doc_id: None,
ext_doc_url: None,
ext_doc_description: None,
}
}
}
impl Documented for BaseType {
fn doc_url(&self) -> Option<&str> {
self.doc_url.as_deref()
}
fn doc_id(&self) -> Option<&str> {
self.doc_id.as_deref()
}
fn description(&self) -> Option<&str> {
self.description.as_deref()
}
}
impl ExternalDocument for BaseType {
fn ext_doc_url(&self) -> Option<&str> {
self.ext_doc_url.as_deref()
}
fn ext_doc_id(&self) -> Option<&str> {
self.ext_doc_id.as_deref()
}
fn ext_doc_description(&self) -> Option<&str> {
self.ext_doc_description.as_deref()
}
}
trait WithBaseType {
fn base(&self) -> &BaseType;
}
impl<T: WithBaseType> Documented for T {
fn doc_url(&self) -> Option<&str> {
self.base().doc_url()
}
fn doc_id(&self) -> Option<&str> {
self.base().doc_id()
}
fn description(&self) -> Option<&str> {
self.base().description()
}
}
impl<T: WithBaseType> ExternalDocument for T {
fn ext_doc_url(&self) -> Option<&str> {
self.base().ext_doc_url()
}
fn ext_doc_id(&self) -> Option<&str> {
self.base().ext_doc_id()
}
fn ext_doc_description(&self) -> Option<&str> {
self.base().ext_doc_description()
}
}
/// An interface type
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Interface {
#[serde(flatten)]
pub base: BaseType,
/// Open generic parameters. The name is that of the parameter, the namespace is an arbitrary value that allows
/// this fully qualified type name to be used when this open generic parameter is used in property's type.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub generics: Vec<TypeName>,
#[serde(skip_serializing_if = "Option::is_none")]
pub inherits: Option<Inherits>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub implements: Vec<Inherits>,
/// Behaviors directly implemented by this interface
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub behaviors: Vec<Inherits>,
/// Behaviors attached to this interface, coming from the interface itself (see `behaviors`)
/// or from inherits and implements ancestors
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub attached_behaviors: Vec<String>,
pub properties: Vec<Property>,
/// The property that can be used as a shortcut for the entire data structure in the JSON.
#[serde(skip_serializing_if = "Option::is_none")]
pub shortcut_property: Option<String>,
// Identify containers
#[serde(skip_serializing_if = "Option::is_none")]
pub variants: Option<Container>,
}
impl WithBaseType for Interface {
fn base(&self) -> &BaseType {
&self.base
}
}
/// A request type
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
// Note: does not extend Interface as properties are split across path, query and body
pub struct Request {
#[serde(flatten)]
pub base: BaseType,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub generics: Vec<TypeName>,
/// The parent defines additional body properties that are added to the body, that has to be a PropertyBody
pub inherits: Option<Inherits>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub implements: Vec<Inherits>,
/// URL path properties
pub path: Vec<Property>,
/// Query string properties
pub query: Vec<Property>,
/// Body type. Most often a list of properties (that can extend those of the inherited class, see above), except
/// for a few specific cases that use other types such as bulk (array) or create (generic parameter). Or NoBody
/// for requests that don't have a body.
pub body: Body,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub behaviors: Vec<Inherits>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub attached_behaviors: Vec<String>,
}
impl WithBaseType for Request {
fn base(&self) -> &BaseType {
&self.base
}
}
/// A response type
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Response {
#[serde(flatten)]
pub base: BaseType,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub generics: Vec<TypeName>,
pub body: Body,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub behaviors: Vec<Inherits>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub attached_behaviors: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub exceptions: Vec<ResponseException>,
}
impl WithBaseType for Response {
fn base(&self) -> &BaseType {
&self.base
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResponseException {
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
pub body: Body,
pub status_codes: Vec<usize>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum Body {
Value(ValueBody),
Properties(PropertiesBody),
NoBody(NoBody),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ValueBody {
pub value: ValueOf,
#[serde(skip_serializing_if = "Option::is_none")]
pub codegen_name: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PropertiesBody {
pub properties: Vec<Property>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NoBody {}
/// An enumeration member.
///
/// When enumeration members can become ambiguous when translated to an identifier, the `name` property will be a good
/// identifier name, and `stringValue` will be the string value to use on the wire.
/// See DateMathTimeUnit for an example of this, which have members for "m" (minute) and "M" (month).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct EnumMember {
/// The identifier to use for this enum
pub name: String,
/// An optional set of aliases for `name`
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub aliases: Vec<String>,
/// If specified takes precedence over `name` when generating code. `name` is always the value
/// to be sent over the wire
#[serde(skip_serializing_if = "Option::is_none")]
pub codegen_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecation: Option<Deprecation>,
#[serde(skip_serializing_if = "Option::is_none")]
pub since: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub availability: Option<Availabilities>,
}
impl From<&str> for EnumMember {
fn from(name: &str) -> Self {
EnumMember {
name: name.to_string(),
..Default::default()
}
}
}
/// An enumeration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Enum {
#[serde(flatten)]
pub base: BaseType,
/// If the enum is open, it means that other than the specified values it can accept an arbitrary value.
/// If this property is not present, it means that the enum is not open (in other words, is closed).
#[serde(default)]
pub is_open: bool,
pub members: Vec<EnumMember>,
}
/// An alias for an existing type.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TypeAlias {
#[serde(flatten)]
pub base: BaseType,
#[serde(rename = "type")]
pub typ: ValueOf,
/// generic parameters: either concrete types or open parameters from the enclosing type
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub generics: Vec<TypeName>,
/// Only applicable to `union_of` aliases: identify typed_key unions (external) and variant inventories (internal)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub variants: Option<TypeAliasVariants>,
}
impl TypeAlias {
pub fn new(name: TypeName, typ: ValueOf) -> TypeAlias {
TypeAlias {
base: BaseType::new(name),
typ,
variants: Default::default(),
generics: Default::default(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "kind")]
pub enum TypeAliasVariants {
ExternalTag(ExternalTag),
InternalTag(InternalTag),
Untagged(Untagged),
}
//------------------------------------------------------------------------------------------------------------
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Endpoint {
pub name: String,
pub description: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub doc_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub doc_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ext_doc_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ext_doc_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ext_doc_description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecation: Option<Deprecation>,
#[serde(skip_serializing_if = "Option::is_none")]
pub availability: Option<Availabilities>,
#[serde(skip_serializing_if = "Option::is_none")]
pub doc_tag: Option<String>,
/// If missing, there is not yet a request definition for this endpoint.
#[serde(skip_serializing_if = "Option::is_none")]
pub request: Option<TypeName>,
pub request_body_required: bool, // Not sure this is useful
/// If missing, there is not yet a response definition for this endpoint.
#[serde(skip_serializing_if = "Option::is_none")]
pub response: Option<TypeName>,
pub urls: Vec<UrlTemplate>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub request_media_type: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub response_media_type: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub privileges: Option<Privileges>,
}
impl Documented for Endpoint {
fn doc_url(&self) -> Option<&str> {
self.doc_url.as_deref()
}
fn doc_id(&self) -> Option<&str> {
self.doc_id.as_deref()
}
fn description(&self) -> Option<&str> {
Some(self.description.as_str())
}
}
impl ExternalDocument for Endpoint {
fn ext_doc_url(&self) -> Option<&str> {
self.ext_doc_url.as_deref()
}
fn ext_doc_id(&self) -> Option<&str> {
self.ext_doc_id.as_deref()
}
fn ext_doc_description(&self) -> Option<&str> {
self.ext_doc_description.as_deref()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Privileges {
#[serde(default)]
pub index: Vec<String>,
#[serde(default)]
pub cluster: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UrlTemplate {
pub path: String,
pub methods: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub deprecation: Option<Deprecation>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ModelInfo {
pub title: String,
pub license: License,
pub version: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct License {
pub name: String,
pub url: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct Model {
#[serde(rename = "_info", skip_serializing_if = "Option::is_none")]
pub info: Option<ModelInfo>,
pub endpoints: Vec<Endpoint>,
pub types: Vec<TypeDefinition>,
}
impl Model {
pub fn from_reader(r: impl std::io::Read) -> Result<Self, serde_json::error::Error> {
serde_json::from_reader(r)
}
}