Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #11

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# `autosar-data`

[![Github Actions](https://github.com/DanielT/autosar-data/workflows/Test/badge.svg)](https://github.com/DanielT/autosar-data/actions)
[![codecov](https://codecov.io/gh/DanielT/autosar-data/branch/main/graph/badge.svg?token=RGKUUJTWZ5)](https://codecov.io/gh/DanielT/autosar-data)

# `autosar-data`

This crate provides functionality to read, modify and write Autosar 4 arxml files,
both separately and in projects consisting of multiple files.

Expand All @@ -21,25 +21,25 @@ both separately and in projects consisting of multiple files.
```rust
use autosar_data::*;

/* load a multi-file project */
let project = AutosarProject::new();
let (file_1, warnings_1) = project.load_arxml_file("some_file.arxml", false)?;
let (file_2, warnings_2) = project.load_arxml_file("other_file.arxml", false)?;
/* load a multi-file data model */
let model = AutosarModel::new();
let (file_1, warnings_1) = model.load_arxml_file("some_file.arxml", false)?;
let (file_2, warnings_2) = model.load_arxml_file("other_file.arxml", false)?;

/* load a buffer */
let (file_3, _) = project.load_named_arxml_buffer(buffer, "filename.arxml", true)?;
let (file_3, _) = model.load_named_arxml_buffer(buffer, "filename.arxml", true)?;

/* write all files of the project */
project.write()?;
/* write all files of the model */
model.write()?;

/* alternatively: */
for file in project.files() {
for file in model.files() {
let file_data = file.serialize();
// do something with file_data
}

/* iterate over all elements in all files */
for (depth, element) in project.elements_dfs() {
for (depth, element) in model.elements_dfs() {
if element.is_identifiable() {
/* the element is identifiable using an Autosar path */
println!("{depth}: {}, {}", element.element_name(), element.path()?);
Expand All @@ -49,7 +49,7 @@ for (depth, element) in project.elements_dfs() {
}

/* get an element by its Autosar path */
let pdu_element = project.get_element_by_path("/Package/Mid/PduName").unwrap();
let pdu_element = model.get_element_by_path("/Package/Mid/PduName").unwrap();

/* work with the content of elements */
if let Some(length) = pdu_element
Expand Down
6 changes: 3 additions & 3 deletions autosar-data-specification/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ struct ElementSpec {
character_data: Option<u16>,
mode: ContentMode,
ordered: bool,
splitable: u32,
splittable: u32,
}

impl AutosarVersion {
Expand Down Expand Up @@ -446,14 +446,14 @@ impl ElementType {
/// This function returns a bitfield that indicates in which versions (if any) the ElementType is marked as splittable.
/// A splittable element may be split across multiple arxml files
pub fn splittable(&self) -> u32 {
DATATYPES[self.0].splitable
DATATYPES[self.0].splittable
}

/// Is the current ElementType splittable in the given version
///
/// A splittable element may be split across multiple arxml files
pub fn splittable_in(&self, version: AutosarVersion) -> bool {
(DATATYPES[self.0].splitable & (version as u32)) != 0
(DATATYPES[self.0].splittable & (version as u32)) != 0
}

/// ElementType::ROOT is the root ElementType of the Autosar arxml document, i.e. this is the ElementType of the AUTOSAR element
Expand Down
9,120 changes: 4,560 additions & 4,560 deletions autosar-data-specification/src/specification.rs

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions autosar-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ both separately and in projects consisting of multiple files.
```rust
use autosar_data::*;

/* load a multi-file project */
let project = AutosarProject::new();
let (file_1, warnings_1) = project.load_arxml_file("some_file.arxml", false)?;
let (file_2, warnings_2) = project.load_arxml_file("other_file.arxml", false)?;
/* load a multi-file data model */
let model = AutosarModel::new();
let (file_1, warnings_1) = model.load_arxml_file("some_file.arxml", false)?;
let (file_2, warnings_2) = model.load_arxml_file("other_file.arxml", false)?;

/* load a buffer */
let (file_3, _) = project.load_named_arxml_buffer(buffer, "filename.arxml", true)?;
let (file_3, _) = model.load_named_arxml_buffer(buffer, "filename.arxml", true)?;

/* write all files of the project */
project.write()?;
/* write all files of the model */
model.write()?;

/* alternatively: */
for file in project.files() {
for file in model.files() {
let file_data = file.serialize();
// do something with file_data
}

/* iterate over all elements in all files */
for (depth, element) in project.elements_dfs() {
for (depth, element) in model.elements_dfs() {
if element.is_identifiable() {
/* the element is identifiable using an Autosar path */
println!("{depth}: {}, {}", element.element_name(), element.path()?);
Expand All @@ -50,7 +50,7 @@ for (depth, element) in project.elements_dfs() {
}

/* get an element by its Autosar path */
let pdu_element = project.get_element_by_path("/Package/Mid/PduName").unwrap();
let pdu_element = model.get_element_by_path("/Package/Mid/PduName").unwrap();

/* work with the content of elements */
if let Some(length) = pdu_element
Expand Down
12 changes: 6 additions & 6 deletions autosar-data/examples/businfo/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;

use autosar_data::{AutosarProject, CharacterData, Element, ElementName, EnumItem};
use autosar_data::{AutosarModel, CharacterData, Element, ElementName, EnumItem};
use rustc_hash::FxHashMap;

enum TimeRangeTolerance {
Expand All @@ -21,24 +21,24 @@ fn main() {
return;
}

let project = AutosarProject::new();
let model = AutosarModel::new();

for filename in &args[1..] {
if let Err(err) = project.load_arxml_file(filename, false) {
if let Err(err) = model.load_arxml_file(filename, false) {
println!("parsing failed: {err}");
return;
}
}

extract_bus_info(project);
extract_bus_info(model);
}

// the starting point for the info we want is a CLUSTER, which is found by iterating over all identifiable elements
// <AUTOSAR> ...
// <AR-PACKAGE> ...
// <CAN_CLUSTER> -or- <FLEXRAY-CLUSTER> -or- <J1939-CLUSTER>
fn extract_bus_info(project: AutosarProject) {
for (_, weak_element) in project.identifiable_elements() {
fn extract_bus_info(model: AutosarModel) {
for (_, weak_element) in model.identifiable_elements() {
let element = weak_element.upgrade().unwrap();
match element.element_name() {
ElementName::CanCluster => {
Expand Down
10 changes: 5 additions & 5 deletions autosar-data/examples/demo/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
io::Read,
};

use autosar_data::AutosarProject;
use autosar_data::AutosarModel;
use autosar_data_specification::*;

fn main() {
Expand All @@ -16,7 +16,7 @@ fn main() {
return;
}

let project = AutosarProject::new();
let model = AutosarModel::new();
for arg in args.iter().skip(1) {
let filename = OsString::from(arg);
let buffer = match load_file_data(&filename) {
Expand All @@ -28,7 +28,7 @@ fn main() {
};

let now = std::time::Instant::now();
let result = project.load_named_arxml_buffer(&buffer, &filename, false);
let result = model.load_named_arxml_buffer(&buffer, &filename, false);
match result {
Ok((_, warnings)) => {
println!("parsing succeeded in {}ms", now.elapsed().as_micros() as f64 / 1000.0);
Expand All @@ -43,10 +43,10 @@ fn main() {
println!("loaded arxml file: {}", arg);
}

for (_, elem) in project.elements_dfs() {
for (_, elem) in model.elements_dfs() {
if elem.is_reference() && elem.element_name() != ElementName::DefinitionRef {
let target_path = elem.character_data().and_then(|cdata| cdata.string_value()).unwrap();
if project.get_element_by_path(&target_path).is_none() {
if model.get_element_by_path(&target_path).is_none() {
println!("Invalid reference from {} to {target_path}", elem.element_name());
}
}
Expand Down
6 changes: 3 additions & 3 deletions autosar-data/examples/generate_files/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ fn main() {

println!("Generating {filename} for \'{}\'", version.describe());

let project = AutosarProject::new();
let arxml_file = project.create_file(&filename, version).unwrap();
let autosar_element = project.root_element();
let model = AutosarModel::new();
let arxml_file = model.create_file(&filename, version).unwrap();
let autosar_element = model.root_element();

let mut counter = 1;
create_sub_elements(&autosar_element, &mut counter, &mut completed, version);
Expand Down
10 changes: 5 additions & 5 deletions autosar-data/examples/sort/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ fn main() {
}
let filename = &args[1];

// create a project and load the file
let project = AutosarProject::new();
let arxmlfile = match project.load_arxml_file(filename, false) {
// create the data model and load the file
let model = AutosarModel::new();
let arxmlfile = match model.load_arxml_file(filename, false) {
Ok((arxmlfile, warnings)) => {
for warn_msg in warnings {
println!("Warning: {warn_msg}");
Expand All @@ -26,12 +26,12 @@ fn main() {
}
};

project.sort();
model.sort();

// write the sorted file
let filename_prefix = filename.strip_suffix(".arxml").unwrap_or(filename);
let new_filename = format!("{filename_prefix}_sorted.arxml");
arxmlfile.set_filename(&new_filename);
project.write().unwrap();
model.write().unwrap();
println!("Sorted output has been written to \"{new_filename}\"");
}
Loading