codama_koroks/
struct_korok.rs

1use crate::{FieldsKorok, KorokTrait};
2use codama_attributes::Attributes;
3use codama_errors::{combine_errors, CodamaError, CodamaResult};
4use codama_nodes::Node;
5use codama_syn_helpers::extensions::*;
6
7#[derive(Debug, PartialEq)]
8pub struct StructKorok<'a> {
9    pub ast: &'a syn::ItemStruct,
10    pub attributes: Attributes<'a>,
11    pub fields: FieldsKorok<'a>,
12    pub node: Option<Node>,
13}
14
15impl<'a> StructKorok<'a> {
16    pub fn parse(item: &'a syn::Item) -> CodamaResult<Self> {
17        let syn::Item::Struct(ast) = item else {
18            return Err(item.error("Expected a struct").into());
19        };
20        let (attributes, fields) = combine_errors!(
21            Attributes::parse(&ast.attrs, item.into()).map_err(CodamaError::from),
22            FieldsKorok::parse(&ast.fields),
23        )?;
24        Ok(Self {
25            ast,
26            attributes,
27            fields,
28            node: None,
29        })
30    }
31}
32
33impl KorokTrait for StructKorok<'_> {
34    fn node(&self) -> &Option<Node> {
35        &self.node
36    }
37
38    fn set_node(&mut self, node: Option<Node>) {
39        self.node = node;
40    }
41
42    fn attributes(&self) -> Option<&Attributes> {
43        Some(&self.attributes)
44    }
45}