rune/module/
enum_mut.rs

1use core::marker::PhantomData;
2
3use crate::compile::{ContextError, Docs};
4use crate::runtime::TypeOf;
5
6use super::{Enum, VariantMut};
7
8/// Access enum metadata mutably.
9pub struct EnumMut<'a, T>
10where
11    T: ?Sized + TypeOf,
12{
13    pub(super) docs: &'a mut Docs,
14    pub(super) enum_: &'a mut Enum,
15    pub(super) _marker: PhantomData<T>,
16}
17
18impl<T> EnumMut<'_, T>
19where
20    T: ?Sized + TypeOf,
21{
22    /// Set documentation for an inserted type.
23    ///
24    /// This completely replaces any existing documentation.
25    pub fn docs(self, docs: impl IntoIterator<Item: AsRef<str>>) -> Result<Self, ContextError> {
26        self.docs.set_docs(docs)?;
27        Ok(self)
28    }
29
30    /// Set static documentation.
31    ///
32    /// This completely replaces any existing documentation.
33    pub fn static_docs(self, docs: &'static [&'static str]) -> Result<Self, ContextError> {
34        self.docs.set_docs(docs)?;
35        Ok(self)
36    }
37
38    /// Get the given variant mutably.
39    pub fn variant_mut(&mut self, index: usize) -> Result<VariantMut<'_, T>, ContextError> {
40        let Some(variant) = self.enum_.variants.get_mut(index) else {
41            return Err(ContextError::MissingVariant {
42                index,
43                type_info: T::type_info(),
44            });
45        };
46
47        Ok(VariantMut {
48            name: variant.name,
49            docs: &mut variant.docs,
50            fields: &mut variant.fields,
51            constructor: &mut variant.constructor,
52            _marker: PhantomData,
53        })
54    }
55}