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<I>(self, docs: I) -> Result<Self, ContextError>
26    where
27        I: IntoIterator,
28        I::Item: AsRef<str>,
29    {
30        self.docs.set_docs(docs)?;
31        Ok(self)
32    }
33
34    /// Set static documentation.
35    ///
36    /// This completely replaces any existing documentation.
37    pub fn static_docs(self, docs: &'static [&'static str]) -> Result<Self, ContextError> {
38        self.docs.set_docs(docs)?;
39        Ok(self)
40    }
41
42    /// Get the given variant mutably.
43    pub fn variant_mut(&mut self, index: usize) -> Result<VariantMut<'_, T>, ContextError> {
44        let Some(variant) = self.enum_.variants.get_mut(index) else {
45            return Err(ContextError::MissingVariant {
46                index,
47                type_info: T::type_info(),
48            });
49        };
50
51        Ok(VariantMut {
52            name: variant.name,
53            docs: &mut variant.docs,
54            fields: &mut variant.fields,
55            constructor: &mut variant.constructor,
56            _marker: PhantomData,
57        })
58    }
59}