codama_koroks/
enum_variant_korok.rs1use crate::{FieldKorok, KorokTrait};
2use codama_attributes::{Attributes, NameDirective, TryFromFilter};
3use codama_errors::{combine_errors, CodamaError, CodamaResult, IteratorCombineErrors};
4use codama_nodes::{CamelCaseString, Node};
5
6#[derive(Debug, PartialEq)]
7pub struct EnumVariantKorok<'a> {
8 pub ast: &'a syn::Variant,
9 pub attributes: Attributes<'a>,
10 pub fields: Vec<FieldKorok<'a>>,
11 pub node: Option<Node>,
12}
13
14impl<'a> EnumVariantKorok<'a> {
15 pub fn parse(ast: &'a syn::Variant) -> CodamaResult<Self> {
16 let (attributes, fields) = combine_errors!(
17 Attributes::parse(&ast.attrs, ast.into()).map_err(CodamaError::from),
18 FieldKorok::parse_all(&ast.fields),
19 )?;
20 Ok(Self {
21 ast,
22 attributes,
23 fields,
24 node: None,
25 })
26 }
27
28 pub fn parse_all(
29 variants: &'a syn::punctuated::Punctuated<syn::Variant, syn::Token![,]>,
30 ) -> CodamaResult<Vec<Self>> {
31 variants
32 .iter()
33 .map(Self::parse)
34 .collect_and_combine_errors()
35 }
36
37 pub fn name(&self) -> CamelCaseString {
38 self.attributes
39 .get_last(NameDirective::filter)
40 .map(|n| n.name.clone())
41 .unwrap_or(self.ast.ident.to_string().into())
42 }
43}
44
45impl KorokTrait for EnumVariantKorok<'_> {
46 fn node(&self) -> &Option<Node> {
47 &self.node
48 }
49
50 fn set_node(&mut self, node: Option<Node>) {
51 self.node = node;
52 }
53
54 fn attributes(&self) -> Option<&Attributes<'_>> {
55 Some(&self.attributes)
56 }
57}