Skip to main content

lisette_syntax/
attributes.rs

1//! Canonical catalog of recognized attributes, shared by the `unknown_attribute`
2//! lint and LSP completions.
3
4use crate::ast::Attribute;
5use crate::ast::AttributeArg;
6use crate::ast::Expression;
7use ecow::EcoString;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10pub enum AttributeTarget {
11    Struct,
12    StructField,
13    Enum,
14    EnumVariant,
15    Function,
16    Method,
17    TypeAlias,
18}
19
20pub struct AttributeInfo {
21    pub name: &'static str,
22    pub detail: &'static str,
23    targets: &'static [AttributeTarget],
24}
25
26impl AttributeInfo {
27    pub fn applies_to(&self, target: AttributeTarget) -> bool {
28        self.targets.contains(&target)
29    }
30}
31
32use AttributeTarget::*;
33
34pub const SERIALIZATION_KEYS: &[&str] = &[
35    "json",
36    "xml",
37    "yaml",
38    "toml",
39    "db",
40    "bson",
41    "mapstructure",
42    "msgpack",
43];
44
45/// In `unknown_attribute` display order. `#[go(...)]` is accepted (see
46/// [`is_known_attribute`]) but deliberately not advertised here.
47pub const ATTRIBUTES: &[AttributeInfo] = &[
48    AttributeInfo {
49        name: "json",
50        detail: "JSON serialization tag",
51        // Enums support `#[json]` (Marshal/UnmarshalJSON); other keys do not.
52        targets: &[Struct, StructField, Enum],
53    },
54    AttributeInfo {
55        name: "xml",
56        detail: "XML serialization tag",
57        targets: &[Struct, StructField],
58    },
59    AttributeInfo {
60        name: "yaml",
61        detail: "YAML serialization tag",
62        targets: &[Struct, StructField],
63    },
64    AttributeInfo {
65        name: "toml",
66        detail: "TOML serialization tag",
67        targets: &[Struct, StructField],
68    },
69    AttributeInfo {
70        name: "db",
71        detail: "database column tag",
72        targets: &[Struct, StructField],
73    },
74    AttributeInfo {
75        name: "bson",
76        detail: "BSON serialization tag",
77        targets: &[Struct, StructField],
78    },
79    AttributeInfo {
80        name: "mapstructure",
81        detail: "mapstructure decoding tag",
82        targets: &[Struct, StructField],
83    },
84    AttributeInfo {
85        name: "msgpack",
86        detail: "MessagePack serialization tag",
87        targets: &[Struct, StructField],
88    },
89    AttributeInfo {
90        name: "tag",
91        detail: "Go struct tag (struct-level defaults or per-field)",
92        targets: &[Struct, StructField],
93    },
94    AttributeInfo {
95        name: "allow",
96        detail: "suppress a lint",
97        targets: &[Function, Method, Struct, Enum, TypeAlias],
98    },
99    AttributeInfo {
100        name: "iterate",
101        detail: "synthesize `variants` for an enum",
102        targets: &[Enum],
103    },
104    AttributeInfo {
105        name: "display",
106        detail: "derive `to_string`",
107        targets: &[Struct, Enum],
108    },
109    AttributeInfo {
110        name: "equality",
111        detail: "derive `equals`",
112        targets: &[Struct, Enum],
113    },
114    AttributeInfo {
115        name: "test",
116        detail: "mark a test function",
117        targets: &[Function],
118    },
119    AttributeInfo {
120        name: "default",
121        detail: "mark the variant that is the enum's zero value",
122        targets: &[EnumVariant],
123    },
124];
125
126/// `go` is accepted but absent from [`ATTRIBUTES`] (the Go-interop rail).
127pub fn is_known_attribute(name: &str) -> bool {
128    name == "go" || ATTRIBUTES.iter().any(|a| a.name == name)
129}
130
131pub fn is_serialization_key(key: &str) -> bool {
132    SERIALIZATION_KEYS.contains(&key)
133}
134
135pub fn struct_attribute_forces_field_export(attribute: &Attribute) -> bool {
136    if attribute.name == "tag" {
137        return matches!(attribute.args.first(), Some(AttributeArg::String(_)));
138    }
139    is_serialization_key(&attribute.name)
140}
141
142pub(crate) fn field_attribute_forces_export(attribute: &Attribute) -> bool {
143    if attribute.name == "tag" {
144        return matches!(
145            attribute.args.first(),
146            Some(AttributeArg::String(_) | AttributeArg::Raw(_))
147        );
148    }
149    is_serialization_key(&attribute.name)
150}
151
152pub fn known_attribute_names() -> Vec<&'static str> {
153    ATTRIBUTES.iter().map(|a| a.name).collect()
154}
155
156pub fn lookup(name: &str) -> Option<&'static AttributeInfo> {
157    ATTRIBUTES.iter().find(|a| a.name == name)
158}
159
160pub fn attributes_for(target: AttributeTarget) -> impl Iterator<Item = &'static AttributeInfo> {
161    ATTRIBUTES.iter().filter(move |a| a.applies_to(target))
162}
163
164pub fn test_attribute(attributes: &[Attribute]) -> Option<&Attribute> {
165    attributes.iter().find(|a| a.name == "test")
166}
167
168pub fn has_test_attribute(attributes: &[Attribute]) -> bool {
169    test_attribute(attributes).is_some()
170}
171
172/// The name and reason of a typedef placeholder marked `#[go(skipped, "<reason>")]`.
173pub fn unsupported_go_symbol(item: &Expression) -> Option<(&EcoString, &str)> {
174    let (Expression::VariableDeclaration {
175        name, attributes, ..
176    }
177    | Expression::TypeAlias {
178        name, attributes, ..
179    }) = item
180    else {
181        return None;
182    };
183    let marker = attributes.iter().find(|attribute| {
184        attribute.name == "go"
185            && attribute
186                .args
187                .iter()
188                .any(|arg| matches!(arg, AttributeArg::Flag(flag) if flag == "skipped"))
189    })?;
190    marker.args.iter().find_map(|arg| match arg {
191        AttributeArg::String(reason) => Some((name, reason.as_str())),
192        _ => None,
193    })
194}