codama_koroks/
struct_korok.rs1use crate::{FieldKorok, KorokTrait};
2use codama_attributes::{Attributes, NameDirective, TryFromFilter};
3use codama_errors::{combine_errors, CodamaError, CodamaResult};
4use codama_nodes::{CamelCaseString, 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: Vec<FieldKorok<'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 FieldKorok::parse_all(&ast.fields),
23 )?;
24 Ok(Self {
25 ast,
26 attributes,
27 fields,
28 node: None,
29 })
30 }
31
32 pub fn name(&self) -> CamelCaseString {
33 self.attributes
34 .get_last(NameDirective::filter)
35 .map(|n| n.name.clone())
36 .unwrap_or(self.ast.ident.to_string().into())
37 }
38}
39
40impl KorokTrait for StructKorok<'_> {
41 fn node(&self) -> &Option<Node> {
42 &self.node
43 }
44
45 fn set_node(&mut self, node: Option<Node>) {
46 self.node = node;
47 }
48
49 fn attributes(&self) -> Option<&Attributes<'_>> {
50 Some(&self.attributes)
51 }
52}