Skip to content

Commit

Permalink
add crate features, allowing various APIs to be disabled
Browse files Browse the repository at this point in the history
- check: check the plausibility of the data in the a2l file
- cleanup: remove unused items in the file
- ifdata_cleanup: remove incorrect IF_DATA
- merge: merge the content of two MODULEs
- sort: sort the content of the a2l file
  • Loading branch information
DanielT committed Dec 31, 2024
1 parent 7ec0e59 commit 763f5af
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
8 changes: 8 additions & 0 deletions a2lfile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ keywords = ["a2l", "file", "file-format", "ASAM-MCD-2MC"]
categories = ["parser-implementations"]
repository = "https://github.com/DanielT/a2lfile"

[features]
default = ["check", "cleanup", "ifdata_cleanup", "merge", "sort"]
check = []
cleanup = []
ifdata_cleanup = []
merge = []
sort = []

[dependencies.a2lmacros]
version = "2.2.0"

Expand Down
17 changes: 10 additions & 7 deletions a2lfile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ The program [a2ltool](https://github.com/DanielT/a2ltool) is based on this libra

## Documentation

Add this to your `Cargo.toml`:

```toml
[dependencies]
a2lfile = "2.2"
```

A simple program based on the `a2lfile` library might look like this:

```rust
Expand Down Expand Up @@ -90,6 +83,16 @@ fn main() {

```

## Crate Features

All features are active by default. If "default-features = false" is set in Cargo.toml, then the following features can be enabled separately:

- `check`: perform a consistency check on the data
- `cleanup`: remove unused `GROUP`s, `RECORD_LAYOUT`s, `COMPU_METHOD`s, `COMPU_(V)TAB`s and `UNIT`s
- `ifdata_cleanup`: remove any `IF_DATA` blocks that could not be parsed using either the specification provided during load or the specification in the A2ML block in the file
- `merge`: merge two a2l files on the `MODULE` level
- `sort`: sort the data in the a2l file

## License

Licensed under either of
Expand Down
1 change: 1 addition & 0 deletions a2lfile/src/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum CharacteristicWrapper<'a> {
TypedefCharacteristic(&'a TypedefCharacteristic),
}

#[must_use]
// check the cross references between various elements
pub fn check(a2l_file: &A2lFile) -> Vec<A2lError> {
let mut results = Vec::new();
Expand Down
6 changes: 5 additions & 1 deletion a2lfile/src/ifdata.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::a2ml::{A2mlTaggedTypeSpec, A2mlTypeSpec, GenericIfData, GenericIfDataTaggedItem};
use crate::parser::{ParseContext, ParserError, ParserState};
use crate::specification::{A2lFile, IfData};
use crate::tokenizer::{A2lToken, A2lTokenType};
use std::collections::HashMap;

#[cfg(feature = "ifdata_cleanup")]
use crate::specification::{A2lFile, IfData};

// parse_ifdata()
// entry point for ifdata parsing.
// Three attmpts to parse the data will be made:
Expand Down Expand Up @@ -479,6 +481,7 @@ fn parse_unknown_taggedstruct(
Ok(GenericIfData::TaggedStruct(tsitems))
}

#[cfg(feature = "ifdata_cleanup")]
pub(crate) fn remove_unknown_ifdata(a2l_file: &mut A2lFile) {
for module in &mut a2l_file.project.module {
remove_unknown_ifdata_from_list(&mut module.if_data);
Expand Down Expand Up @@ -527,6 +530,7 @@ pub(crate) fn remove_unknown_ifdata(a2l_file: &mut A2lFile) {
}
}

#[cfg(feature = "ifdata_cleanup")]
fn remove_unknown_ifdata_from_list(ifdata_list: &mut Vec<IfData>) {
let mut new_ifdata_list = Vec::new();
std::mem::swap(ifdata_list, &mut new_ifdata_list);
Expand Down
21 changes: 21 additions & 0 deletions a2lfile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
//! a2lfile is a library that allows you to read, modify and write a2l files.
//!
//! It is fast, preserves the formatting of the input, and has support for files using standard version 1.71.
//!
//! # Features
//!
//! - `check`: perform a consistency check on the data
//! - `cleanup`: remove unused `GROUP`s, `RECORD_LAYOUTs`, `COMPU_METHOD`s, `COMPU_(V)TAB`s and `UNIT`s
//! - `ifdata_cleanup`: remove any `IF_DATA` blocks that could not be parsed using either the specification provided during load or the specification in the A2ML block in the file
//! - `merge`: merge two a2l files on the `MODULE` level
//! - `sort`: sort the data in the a2l file
mod a2ml;
#[cfg(feature = "check")]
mod checker;
#[cfg(feature = "cleanup")]
mod cleanup;
mod ifdata;
mod loader;
#[cfg(feature = "merge")]
mod merge;
mod namemap;
mod parser;
#[cfg(feature = "sort")]
mod sort;
mod specification;
mod tokenizer;
Expand Down Expand Up @@ -368,6 +380,7 @@ impl A2lFile {
Ok(())
}

#[cfg(feature = "merge")]
/// Merge another a2l file on the MODULE level.
///
/// The input file and the merge file must each contain exactly one MODULE.
Expand Down Expand Up @@ -395,27 +408,33 @@ impl A2lFile {
}
}

#[cfg(feature = "check")]
/// perform a consistency check on the data.
#[must_use]
pub fn check(&self) -> Vec<A2lError> {
checker::check(self)
}

#[cfg(feature = "sort")]
/// sort the data in the a2l file.
/// This changes the order in which the blocks will be written to an output file
pub fn sort(&mut self) {
sort::sort(self);
}

#[cfg(feature = "sort")]
/// sort newly added or merged blocks into sensible locations between the existing blocks
pub fn sort_new_items(&mut self) {
sort::sort_new_items(self);
}

#[cfg(feature = "cleanup")]
/// cleanup: remove unused GROUPs, `RECORD_LAYOUTs`, `COMPU_METHODs`, COMPU_(V)TABs and UNITs
pub fn cleanup(&mut self) {
cleanup::cleanup(self);
}

#[cfg(feature = "ifdata_cleanup")]
/// cleanup `IF_DATA`: remove any `IF_DATA` blocks that could not be parsed using either the
/// specification provided during load or the specification in the A2ML block in the file
pub fn ifdata_cleanup(&mut self) {
Expand All @@ -430,6 +449,7 @@ impl Module {
ModuleNameMap::build(self)
}

#[cfg(feature = "merge")]
/// merge another module with this module
///
/// Any elements in other that are not present in this module will be moved over. The other module will typically be empty at the end of the merge.
Expand Down Expand Up @@ -653,6 +673,7 @@ mod tests {
std::fs::remove_file(path).unwrap();
}

#[cfg(feature = "merge")]
#[test]
fn merge() {
// version is copied if none exists
Expand Down
7 changes: 7 additions & 0 deletions a2lfile/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ ASAP2_VERSION 1 61
assert_eq!(TEST_A2L, text);
}

#[cfg(all(
feature = "merge",
feature = "sort",
feature = "check",
feature = "cleanup"
))]
#[test]
fn full_test() {
// work in a tempdir
Expand Down Expand Up @@ -1252,6 +1258,7 @@ ASAP2_VERSION 1 61
/end PROJECT
"#;

#[cfg(all(feature = "check", feature = "sort"))]
#[test]
fn specification_test() {
let mut log_msgs = Vec::new();
Expand Down

0 comments on commit 763f5af

Please sign in to comment.