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