use std::cell::Cell;
use crate::{context::Context, pretty::DebugWithContext, Constant, Function, MetadataIndex, Type};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, DebugWithContext)]
pub struct Config(#[in_context(configs)] pub slotmap::DefaultKey);
#[doc(hidden)]
#[derive(Clone, Debug, DebugWithContext)]
pub enum ConfigContent {
V0 {
name: String,
ty: Type,
ptr_ty: Type,
constant: Constant,
opt_metadata: Option<MetadataIndex>,
},
V1 {
name: String,
ty: Type,
ptr_ty: Type,
encoded_bytes: Vec<u8>,
decode_fn: Cell<Function>,
opt_metadata: Option<MetadataIndex>,
},
}
impl Config {
pub fn new(context: &mut Context, content: ConfigContent) -> Self {
Config(context.configs.insert(content))
}
pub fn get_content<'a>(&self, context: &'a Context) -> &'a ConfigContent {
&context.configs[self.0]
}
pub fn get_type(&self, context: &Context) -> Type {
match &context.configs[self.0] {
ConfigContent::V0 { ptr_ty, .. } | ConfigContent::V1 { ptr_ty, .. } => *ptr_ty,
}
}
pub fn get_name<'a>(&self, context: &'a Context) -> &'a str {
match &context.configs[self.0] {
ConfigContent::V0 { name, .. } | ConfigContent::V1 { name, .. } => name,
}
}
}