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