codama_koroks/
field_korok.rs1use crate::KorokTrait;
2use codama_attributes::Attributes;
3use codama_errors::CodamaResult;
4use codama_nodes::{Node, StructFieldTypeNode, TypeNode};
5
6#[derive(Debug, PartialEq)]
7pub struct FieldKorok<'a> {
8 pub ast: &'a syn::Field,
9 pub attributes: Attributes<'a>,
10 pub node: Option<Node>,
11}
12
13impl<'a> FieldKorok<'a> {
14 pub fn parse(ast: &'a syn::Field) -> CodamaResult<Self> {
15 let attributes = Attributes::parse(&ast.attrs, ast.into())?;
16 Ok(Self {
17 ast,
18 attributes,
19 node: None,
20 })
21 }
22
23 pub fn set_type_node(&mut self, node: TypeNode) {
24 self.node = match &self.ast.ident {
25 Some(ident) => Some(StructFieldTypeNode::new(ident.to_string(), node).into()),
26 None => Some(node.into()),
27 }
28 }
29}
30
31impl KorokTrait for FieldKorok<'_> {
32 fn node(&self) -> &Option<Node> {
33 &self.node
34 }
35
36 fn set_node(&mut self, node: Option<Node>) {
37 self.node = node;
38 }
39
40 fn attributes(&self) -> Option<&Attributes> {
41 Some(&self.attributes)
42 }
43}