codama_koroks/
file_module_korok.rs

1use crate::{ItemKorok, KorokTrait};
2use codama_attributes::Attributes;
3use codama_errors::{combine_errors, CodamaError, CodamaResult};
4use codama_nodes::Node;
5use codama_stores::FileModuleStore;
6use codama_syn_helpers::extensions::*;
7
8#[derive(Debug, PartialEq)]
9pub struct FileModuleKorok<'a> {
10    pub ast: &'a syn::ItemMod,
11    pub attributes: Attributes<'a>,
12    pub file_attributes: Attributes<'a>,
13    pub items: Vec<ItemKorok<'a>>,
14    pub node: Option<Node>,
15    pub store: &'a FileModuleStore,
16}
17
18impl<'a> FileModuleKorok<'a> {
19    pub fn parse(item: &'a syn::Item, store: &'a FileModuleStore) -> CodamaResult<Self> {
20        let syn::Item::Mod(ast) = item else {
21            return Err(item.error("Expected an module").into());
22        };
23        if ast.content.is_some() {
24            return Err(syn::Error::new_spanned(
25                ast,
26                "Module has content, it should be parsed with ModuleKorok",
27            )
28            .into());
29        }
30
31        let (attributes, file_attributes, items) = combine_errors!(
32            Attributes::parse(&ast.attrs, item.into()).map_err(CodamaError::from),
33            Attributes::parse(&store.file.attrs, (&store.file).into()).map_err(CodamaError::from),
34            ItemKorok::parse_all(&store.file.items, &store.file_modules, &mut 0),
35        )?;
36        Ok(Self {
37            ast,
38            attributes,
39            file_attributes,
40            items,
41            node: None,
42            store,
43        })
44    }
45}
46
47impl KorokTrait for FileModuleKorok<'_> {
48    fn node(&self) -> &Option<Node> {
49        &self.node
50    }
51
52    fn set_node(&mut self, node: Option<Node>) {
53        self.node = node;
54    }
55
56    fn attributes(&self) -> Option<&Attributes> {
57        Some(&self.attributes)
58    }
59}