borsh_derive/internals/attributes/
mod.rs1use syn::{Attribute, Path};
2
3pub mod field;
4pub mod item;
5pub mod parsing;
6
7#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
10pub struct Symbol(pub &'static str, pub &'static str);
11
12pub const BORSH: Symbol = Symbol("borsh", "borsh(...)");
14pub const BOUND: Symbol = Symbol("bound", "bound(...)");
16pub const USE_DISCRIMINANT: Symbol = Symbol("use_discriminant", "use_discriminant = ...");
18pub const SERIALIZE: Symbol = Symbol("serialize", "serialize = ...");
20pub const DESERIALIZE: Symbol = Symbol("deserialize", "deserialize = ...");
22pub const SKIP: Symbol = Symbol("skip", "skip");
24pub const INIT: Symbol = Symbol("init", "init = ...");
26pub const SERIALIZE_WITH: Symbol = Symbol("serialize_with", "serialize_with = ...");
28pub const DESERIALIZE_WITH: Symbol = Symbol("deserialize_with", "deserialize_with = ...");
30pub const CRATE: Symbol = Symbol("crate", "crate = ...");
32
33#[cfg(feature = "schema")]
34pub mod schema_keys {
35 use super::Symbol;
36
37 pub const SCHEMA: Symbol = Symbol("schema", "schema(...)");
39 pub const PARAMS: Symbol = Symbol("params", "params = ...");
41 pub const WITH_FUNCS: Symbol = Symbol("with_funcs", "with_funcs(...)");
44 pub const DECLARATION: Symbol = Symbol("declaration", "declaration = ...");
46 pub const DEFINITIONS: Symbol = Symbol("definitions", "definitions = ...");
48}
49
50#[derive(Clone, Copy)]
51pub enum BoundType {
52 Serialize,
53 Deserialize,
54}
55impl PartialEq<Symbol> for Path {
56 fn eq(&self, word: &Symbol) -> bool {
57 self.is_ident(word.0)
58 }
59}
60
61impl<'a> PartialEq<Symbol> for &'a Path {
62 fn eq(&self, word: &Symbol) -> bool {
63 self.is_ident(word.0)
64 }
65}
66
67fn get_one_attribute(attrs: &[Attribute]) -> syn::Result<Option<&Attribute>> {
68 let count = attrs.iter().filter(|attr| attr.path() == BORSH).count();
69 let borsh = attrs.iter().find(|attr| attr.path() == BORSH);
70 if count > 1 {
71 return Err(syn::Error::new_spanned(
72 borsh.unwrap(),
73 format!("multiple `{}` attributes not allowed", BORSH.0),
74 ));
75 }
76 Ok(borsh)
77}