use crate::compile::ContextError;
use crate::function_meta::{Associated, ToInstance};
use crate::item::IntoComponent;
use crate::module::ItemMut;
use crate::runtime::{ToConstValue, TypeHash, TypeOf};
use super::Module;
#[must_use = "Must call one of the build functions, like `build` or `build_associated`"]
pub struct ModuleConstantBuilder<'a, N, V> {
pub(super) module: &'a mut Module,
pub(super) name: N,
pub(super) value: V,
}
impl<'a, N, V> ModuleConstantBuilder<'a, N, V>
where
V: TypeHash + TypeOf + ToConstValue,
{
pub fn build(self) -> Result<ItemMut<'a>, ContextError>
where
N: IntoComponent,
{
self.module.insert_constant(self.name, self.value)
}
pub fn build_associated<T>(self) -> Result<ItemMut<'a>, ContextError>
where
T: TypeOf,
N: ToInstance,
{
let name = self.name.to_instance()?;
let associated = Associated::from_type::<T>(name)?;
self.module
.insert_associated_constant(associated, self.value)
}
}