use crate::{
metadata::{
diagnostics::DiagnosticCategory,
loader::{LoaderContext, MetadataLoader},
tables::{ModuleRaw, TableId},
},
Result,
};
pub(crate) struct ModuleLoader;
impl MetadataLoader for ModuleLoader {
fn load(&self, context: &LoaderContext) -> Result<()> {
let (Some(tables_header), Some(strings), Some(guids)) =
(context.meta, context.strings, context.guids)
else {
return Err(malformed_error!("No module has been found"));
};
let Some(table) = tables_header.table::<ModuleRaw>() else {
return Err(malformed_error!("No module has been found"));
};
let Some(row) = table.get(1) else {
return Err(malformed_error!("No module has been found"));
};
let token_msg = || format!("module 0x{:08x}", row.token.value());
let Some(owned) = context.handle_result(
row.to_owned(strings, guids),
DiagnosticCategory::Table,
token_msg,
)?
else {
return Err(malformed_error!("No module has been found"));
};
context.handle_error(
context
.module
.set(owned)
.map_err(|_| malformed_error!("Module has already been set")),
DiagnosticCategory::Table,
token_msg,
)
}
fn table_id(&self) -> Option<TableId> {
Some(TableId::Module)
}
fn dependencies(&self) -> &'static [TableId] {
&[]
}
}