Skip to main content

xidl_parser/hir/annotation/
mod.rs

1use super::*;
2use serde::{Deserialize, Serialize};
3use std::str::FromStr;
4
5mod convert;
6mod location;
7pub(super) mod params;
8#[cfg(test)]
9mod tests;
10
11pub use location::{HttpParameterLocation, VersionParameter};
12
13pub use convert::{
14    annotation_id_value, annotation_name, annotation_params, effective_wire_name,
15    expand_annotations, field_rename, is_skipped, normalize_annotation_params, rename_all,
16};
17#[cfg(test)]
18use convert::{apply_rename_rule, render_annotation_const_expr};
19#[derive(Debug, Serialize, Deserialize, Clone)]
20pub enum Annotation {
21    Id {
22        value: String,
23    },
24    Key {
25        value: Option<String>,
26    },
27    AutoId {
28        value: Option<String>,
29    },
30    Optional {
31        value: Option<String>,
32    },
33    Position {
34        value: String,
35    },
36    Value {
37        value: String,
38    },
39    Extensibility {
40        kind: String,
41    },
42    Final,
43    Appendable,
44    Mutable,
45    MustUnderstand {
46        value: Option<String>,
47    },
48    Default {
49        value: String,
50    },
51    Range {
52        min: String,
53        max: String,
54    },
55    Min {
56        value: String,
57    },
58    Max {
59        value: String,
60    },
61    Unit {
62        value: String,
63    },
64    BitBound {
65        value: String,
66    },
67    External {
68        value: Option<String>,
69    },
70    Nested {
71        value: Option<String>,
72    },
73    Verbatim {
74        language: Option<String>,
75        placement: Option<String>,
76        text: String,
77    },
78    Service {
79        platform: Option<String>,
80    },
81    Oneway {
82        value: Option<String>,
83    },
84    Ami {
85        value: Option<String>,
86    },
87    HashId {
88        value: Option<String>,
89    },
90    DefaultNested {
91        value: Option<String>,
92    },
93    IgnoreLiteralNames {
94        value: Option<String>,
95    },
96    TryConstruct {
97        value: Option<String>,
98    },
99    NonSerialized {
100        value: Option<String>,
101    },
102    DataRepresentation {
103        kinds: Vec<String>,
104    },
105    Topic {
106        name: Option<String>,
107        platform: Option<String>,
108    },
109    Choice,
110    Empty,
111    DdsService,
112    DdsRequestTopic {
113        name: String,
114    },
115    DdsReplyTopic {
116        name: String,
117    },
118    Builtin {
119        name: String,
120        params: Option<AnnotationParams>,
121    },
122    ScopedName {
123        name: ScopedName,
124        params: Option<AnnotationParams>,
125    },
126    DefaultLiteral,
127    Rename {
128        name: String,
129    },
130    RenameAll {
131        rule: RenameRule,
132    },
133    Skip,
134    /// The `@version` marker that designates an enum as the API version timeline.
135    Version,
136    /// A version timeline with an explicit request parameter location and name.
137    VersionParameter(VersionParameter),
138    /// The `@added(Version::member)` lifecycle annotation: the declaration exists
139    /// from that version onward.
140    Added {
141        version: ScopedName,
142    },
143    /// The `@removed(Version::member)` lifecycle annotation: the declaration is
144    /// removed at that version.
145    Removed {
146        version: ScopedName,
147    },
148}
149
150#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
151pub enum RenameRule {
152    None,
153    LowerCase,
154    UpperCase,
155    PascalCase,
156    CamelCase,
157    SnakeCase,
158    ScreamingSnakeCase,
159    KebabCase,
160    ScreamingKebabCase,
161}
162
163impl RenameRule {
164    pub fn as_str(&self) -> &'static str {
165        match self {
166            Self::None => "None",
167            Self::LowerCase => "lowercase",
168            Self::UpperCase => "UPPERCASE",
169            Self::PascalCase => "PascalCase",
170            Self::CamelCase => "camelCase",
171            Self::SnakeCase => "snake_case",
172            Self::ScreamingSnakeCase => "SCREAMING_SNAKE_CASE",
173            Self::KebabCase => "kebab-case",
174            Self::ScreamingKebabCase => "SCREAMING-KEBAB-CASE",
175        }
176    }
177}
178impl FromStr for RenameRule {
179    type Err = ();
180
181    fn from_str(s: &str) -> Result<Self, Self::Err> {
182        match s {
183            "lowercase" => Ok(Self::LowerCase),
184            "UPPERCASE" => Ok(Self::UpperCase),
185            "PascalCase" => Ok(Self::PascalCase),
186            "camelCase" => Ok(Self::CamelCase),
187            "snake_case" => Ok(Self::SnakeCase),
188            "SCREAMING_SNAKE_CASE" | "SCREAMINGSNAKECASE" => Ok(Self::ScreamingSnakeCase),
189            "kebab-case" => Ok(Self::KebabCase),
190            "SCREAMING-KEBAB-CASE" => Ok(Self::ScreamingKebabCase),
191            _ => Ok(Self::None),
192        }
193    }
194}
195
196#[derive(Debug, Serialize, Deserialize, Clone)]
197pub enum AnnotationParams {
198    ConstExpr(ConstExpr),
199    Positional(Vec<ConstExpr>),
200    Params(Vec<AnnotationParam>),
201    Raw(String),
202}
203
204#[derive(Debug, Serialize, Deserialize, Clone)]
205pub struct AnnotationParam {
206    pub ident: String,
207    pub value: Option<ConstExpr>,
208}