Skip to content

Commit

Permalink
Make the ModuleNameMap public and add it to the API for the Module class
Browse files Browse the repository at this point in the history
Fixes issue #19
  • Loading branch information
DanielT committed Sep 28, 2023
1 parent f570c9a commit f9967bb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ mod writer;
use std::convert::AsRef;
use std::fmt::Write;
use std::path::Path;
pub use namemap::ModuleNameMap;
// used internally
use parser::{ParseContext, ParserState};
use tokenizer::{A2lToken, A2lTokenType};
Expand Down Expand Up @@ -317,6 +318,14 @@ impl A2lFile {
}
}


impl Module {
pub fn build_namemap(&self) -> ModuleNameMap {
let mut log_msgs = vec![];
ModuleNameMap::build(&self, &mut log_msgs)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
34 changes: 18 additions & 16 deletions src/namemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ macro_rules! check_and_insert_multi {
}
}

/// ModuleNameMap collects references to all items with a [Module] into HashMaps, making it possible to access them all by name.
///
/// There are several name spaces per module, each stored in a different HashMap in the ModuleNameMap:
/// - object: `CHARACTERISTIC`, `MEASUREMENT`, `AXIS_PTS`, `BLOB`, `INSTANCE`
/// - compu_method: `COMPU_METHOD`
/// - compu_tab: `COMPU_VTAB`, `COMPU_VTAB_RANGE`, `COMPU_TAB`
/// - frame: `FRAME`
/// - function: `FUNCTION`
/// - group: `GROUP`
/// - memory_segment: `MEMORY_SEGMENT`
/// - record_layout: `RECORD_LAYOUT`
/// - transformer: `TRANSFORMER`
/// - typedef: `TYPEDEF_AXIS`, `TYPEDEF_BLOB`, `TYPEDEF_CHARACTERISTIC`, `TYPEDEF_INSTANCE`, `TYPEDEF_MEASUREMENT`
/// - unit: `UNIT`
/// - variant: `VARIANT`
///
/// While the ModuleNameMap is holding these references, the borrow checker will prevent any of these items from being modified.
#[derive(Debug, PartialEq)]
pub struct ModuleNameMap<'a> {
pub compu_method: HashMap<String, &'a CompuMethod>,
Expand All @@ -73,22 +90,7 @@ pub struct ModuleNameMap<'a> {
}

impl<'a> ModuleNameMap<'a> {
/*
There are several name spaces per module, officialy starting with 1.7, but this matches informal practice from previous versions.
The following name spaces are defined:
- CHARACTERISTIC, MEASUREMENT, AXIS_PTS, BLOB, INSTANCE
- COMPU_METHOD
- COMPU_VTAB, COMPU_VTAB_RANGE, COMPU_TAB
- FRAME
- FUNCTION
- GROUP
- MEMORY_SEGMENT
- RECORD_LAYOUT
- TRANSFORMER
- TYPEDEF_*
- UNIT
- VARIANT
*/
/// build a new ModuleNameMap for the given [Module]
pub fn build(module: &'a Module, log_msgs: &mut Vec<String>) -> Self {
Self {
compu_method: build_namemap_compu_method(module, log_msgs),
Expand Down

0 comments on commit f9967bb

Please sign in to comment.