Skip to main content

xidl_parser/hir/annotation/
mod.rs

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