interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / ptr_meta_derive / src / attributes.rs

72 lines · 1900 bytes Raw Blame History
1use quote::ToTokens;
2use syn::{
3 meta::ParseNestedMeta, parse_quote, AttrStyle, Attribute, Error, Path,
4 Token,
5};
6
7fn try_set_attribute<T: ToTokens>(
8 attribute: &mut Option<T>,
9 value: T,
10 name: &'static str,
11) -> Result<(), Error> {
12 if attribute.is_none() {
13 *attribute = Some(value);
14 Ok(())
15 } else {
16 Err(Error::new_spanned(
17 value,
18 format!("{name} already specified"),
19 ))
20 }
21}
22
23#[derive(Default)]
24pub struct Attributes {
25 crate_path: Option<Path>,
26}
27
28impl Attributes {
29 pub fn parse_meta(
30 &mut self,
31 meta: ParseNestedMeta<'_>,
32 ) -> Result<(), Error> {
33 if meta.path.is_ident("crate") {
34 if meta.input.parse::<Token![=]>().is_ok() {
35 let path = meta.input.parse::<Path>()?;
36 try_set_attribute(&mut self.crate_path, path, "crate")
37 } else if meta.input.is_empty() || meta.input.peek(Token![,]) {
38 try_set_attribute(
39 &mut self.crate_path,
40 parse_quote! { crate },
41 "crate",
42 )
43 } else {
44 Err(meta.error("expected `crate` or `crate = ...`"))
45 }
46 } else {
47 Err(meta.error("unrecognized ptr_meta argument"))
48 }
49 }
50
51 pub fn parse(attrs: &[Attribute]) -> Result<Self, Error> {
52 let mut result = Self::default();
53
54 for attr in attrs.iter() {
55 if !matches!(attr.style, AttrStyle::Outer) {
56 continue;
57 }
58
59 if attr.path().is_ident("ptr_meta") {
60 attr.parse_nested_meta(|nested| result.parse_meta(nested))?;
61 }
62 }
63
64 Ok(result)
65 }
66
67 pub fn crate_path(&self) -> Path {
68 self.crate_path
69 .clone()
70 .unwrap_or_else(|| parse_quote! { ::ptr_meta })
71 }
72}