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