autosar_data/
lib.rs

1//! Crate autosar-data
2//!
3//! This crate provides functionality to read, modify and write Autosar arxml files,
4//! both separately and in projects consisting of multiple files.
5//!
6//! Features:
7//!
8//!  - read and write arxml files
9//!  - fully validate all data when it is loaded
10//!  - non-strict mode so that invalid but structurally sound data can be loaded
11//!  - various element operations to modify and create sub-elements, data and attributes
12//!  - support for Autosar paths and cross references
13//!  - all operations are thread safe, e.g. it is possible to load mutliple files on separate threads
14//!
15//! # Examples
16//!
17//! ```no_run
18//! use autosar_data::*;
19//! # fn main() -> Result<(), AutosarDataError> {
20//! /* load a multi-file data model */
21//! let model = AutosarModel::new();
22//! let (file_1, warnings_1) = model.load_file("some_file.arxml", false)?;
23//! let (file_2, warnings_2) = model.load_file("other_file.arxml", false)?;
24//! /* load a buffer */
25//! # let buffer = b"";
26//! let (file_3, _) = model.load_buffer(buffer, "filename.arxml", true)?;
27//!
28//! /* write all files of the model */
29//! model.write()?;
30//!
31//! /* alternatively: */
32//! for file in model.files() {
33//!     let file_data = file.serialize();
34//!     // do something with file_data
35//! }
36//!
37//! /* iterate over all elements in all files */
38//! for (depth, element) in model.elements_dfs() {
39//!     if element.is_identifiable() {
40//!         /* the element is identifiable using an Autosar path */
41//!         println!("{depth}: {}, {}", element.element_name(), element.path()?);
42//!     } else {
43//!         println!("{depth}: {}", element.element_name());
44//!     }
45//! }
46//!
47//! /* get an element by its Autosar path */
48//! let pdu_element = model.get_element_by_path("/Package/Mid/PduName").unwrap();
49//!
50//! /* work with the content of elements */
51//! if let Some(length) = pdu_element
52//!     .get_sub_element(ElementName::Length)
53//!     .and_then(|elem| elem.character_data())
54//!     .and_then(|cdata| cdata.string_value())
55//! {
56//!     println!("Pdu Length: {length}");
57//! }
58//!
59//! /* modify the attributes of an element */
60//! pdu_element.set_attribute_string(AttributeName::Uuid, "12ab34cd-1234-1234-1234-12ab34cd56ef");
61//! pdu_element.remove_attribute(AttributeName::Uuid);
62//!
63//! # Ok(())
64//! # }
65//! ```
66//!
67//! # Example Programs
68//!
69//! Two complete example programs can be found in the examples directory of the source repostitory. They are:
70//!
71//!  - businfo, which extracts information about bus settings, frames, pdus and signals from an autosar ECU extract
72//!  - `generate_files`, which for each Autosar version generates an arxml file containing a least one instance of every specified element
73//!
74
75#![warn(missing_docs)]
76
77use autosar_data_specification::{AttributeSpec, CharacterDataSpec, ContentMode, ElementType};
78use fxhash::{FxBuildHasher, FxHashMap};
79use indexmap::IndexMap;
80pub use iterators::*;
81use parking_lot::RwLock;
82use parser::ArxmlParser;
83use smallvec::SmallVec;
84use std::collections::HashSet;
85use std::path::{Path, PathBuf};
86use std::sync::{Arc, Weak};
87use std::{fs::File, io::Read};
88use thiserror::Error;
89
90mod arxmlfile;
91mod autosarmodel;
92mod chardata;
93mod element;
94mod elementraw;
95mod iterators;
96mod lexer;
97mod parser;
98
99// allow public access to the error sub-types
100pub use lexer::ArxmlLexerError;
101pub use parser::ArxmlParserError;
102
103// reexport some of the info from the specification
104pub use autosar_data_specification::AttributeName;
105pub use autosar_data_specification::AutosarVersion;
106pub use autosar_data_specification::ElementName;
107pub use autosar_data_specification::EnumItem;
108
109type FxIndexMap<K, V> = IndexMap<K, V, FxBuildHasher>;
110
111/// `AutosarModel` is the top level data type in the autosar-data crate.
112///
113/// An instance of `AutosarModel` is required for all other operations.
114///
115/// The model contains the hierarchy of Autosar elements. It can be created manually or loaded from one or more arxml files.
116/// It stores the association between elements and files.
117/// In addition, this top-level structure provides caching of Autosar paths, to allow quick resolution of cross-references.
118#[derive(Clone)]
119pub struct AutosarModel(Arc<RwLock<AutosarModelRaw>>);
120
121// Weak reference to an instance of AutosarModel
122#[derive(Clone)]
123pub(crate) struct WeakAutosarModel(Weak<RwLock<AutosarModelRaw>>);
124
125/// The inner autosar data model (unlocked)
126///
127/// The model contains the hierarchy of Autosar elements. It can be created manually or loaded from one or more arxml files.
128/// It stores the association between elements and files.
129/// In addition, this top-level structure provides caching of Autosar paths, to allow quick resolution of cross-references.
130pub(crate) struct AutosarModelRaw {
131    root_element: Element,
132    files: Vec<ArxmlFile>,
133    /// `identifiables` is a `HashMap` of all named elements, needed to resolve references without doing a full search.
134    identifiables: FxIndexMap<String, WeakElement>,
135    /// `reference_origins` is a `HashMap` of all referencing elements. This is needed to efficiently fix up the references when a referenced element is renamed.
136    reference_origins: FxHashMap<String, Vec<WeakElement>>,
137}
138
139/// The error type `AutosarDataError` wraps all errors that can be generated anywhere in the crate
140#[derive(Error, Debug)]
141#[non_exhaustive]
142pub enum AutosarDataError {
143    /// `IoErrorRead`: An `IoError` that occurred while reading a file
144    #[error("Failed to read {}: {ioerror}", .filename.to_string_lossy())]
145    IoErrorRead {
146        /// The filename that caused the error
147        filename: PathBuf,
148        /// The underlying `std::io::Error`
149        ioerror: std::io::Error,
150    },
151
152    /// `IoErrorOpen`: an `IoError` that occurres while opening a file
153    #[error("Failed to open {}: {ioerror}", .filename.to_string_lossy())]
154    IoErrorOpen {
155        /// The filename that caused the error
156        filename: PathBuf,
157        /// The underlying `std::io::Error`
158        ioerror: std::io::Error,
159    },
160
161    /// `IoErrorWrite`: An `IoError` that occurred while writing a file
162    #[error("Failed to write {}: {ioerror}", .filename.to_string_lossy())]
163    IoErrorWrite {
164        /// The filename that caused the error
165        filename: PathBuf,
166        /// The underlying `std::io::Error`
167        ioerror: std::io::Error,
168    },
169
170    /// `DuplicateFilenameError`: The model can'#'t contain two files with identical names
171    #[error("Could not {verb} file {}: A file with this name is already loaded", .filename.to_string_lossy())]
172    DuplicateFilenameError {
173        /// description of the operation that failed
174        verb: &'static str,
175        /// The filename that caused the error
176        filename: PathBuf,
177    },
178
179    /// `LexerError`: An error originating in the lexer, such as unclodes strings, mismatched '<' and '>', etc
180    #[error("Failed to tokenize {} on line {line}: {source}", .filename.to_string_lossy())]
181    LexerError {
182        /// The filename that caused the error
183        filename: PathBuf,
184        /// The line number where the error occurred
185        line: usize,
186        /// The underlying `ArxmlLexerError`
187        source: ArxmlLexerError,
188    },
189
190    /// `ParserError`: A parser error
191    #[error("Failed to parse {}:{line}: {source}", .filename.to_string_lossy())]
192    ParserError {
193        /// The filename that caused the error
194        filename: PathBuf,
195        /// The line number where the error occurred
196        line: usize,
197        /// The underlying `ArxmlParserError`
198        source: ArxmlParserError,
199    },
200
201    /// A file could not be loaded into the model, because the Autosar paths of the new data overlapped with the Autosar paths of the existing data
202    #[error("Loading failed: element path {path} of new data in {} overlaps with the existing loaded data", .filename.to_string_lossy())]
203    OverlappingDataError {
204        /// The filename that caused the error
205        filename: PathBuf,
206        /// Autosar path of the element that caused the error
207        path: String,
208    },
209
210    /// An operation failed because one of the elements involved is in the deleted state and will be freed once its reference count reaches zero
211    #[error("Operation failed: the item has been deleted")]
212    ItemDeleted,
213
214    /// A sub element could not be created at or moved to the given position
215    #[error("Invalid position for an element of this kind")]
216    InvalidPosition,
217
218    /// The Autosar version of the new file or element did not match the version already in use
219    #[error("Version mismatch between existing {} and new {}", .version_cur, .version_new)]
220    VersionMismatch {
221        /// The current version of the model
222        version_cur: AutosarVersion,
223        /// The version of the new file or element
224        version_new: AutosarVersion,
225    },
226
227    /// The Autosar version is not compatible with the data
228    #[error("Version {} is not compatible with the element data", .version)]
229    VersionIncompatibleData {
230        /// The incompatible version
231        version: AutosarVersion,
232    },
233
234    /// A function that only applies to identifiable elements was called on an element which is not identifiable
235    #[error("The element at {} is not identifiable", .xmlpath)]
236    ElementNotIdentifiable {
237        /// The "xml path" (a string representation of the path to the element) where the error occurred
238        xmlpath: String,
239    },
240
241    /// An item name is required to perform this action
242    #[error("An item name is required for element {}", .element)]
243    ItemNameRequired {
244        /// The element where the item name is required
245        element: ElementName,
246    },
247
248    /// The element has the wrong content type for the requested operation, e.g. inserting elements when the content type only allows character data
249    #[error("Incorrect content type for element {}", .element)]
250    IncorrectContentType {
251        /// The element where the content type is incorrect
252        element: ElementName,
253    },
254
255    /// Could not insert a sub element, because it conflicts with an existing sub element
256    #[error("Element insertion conflict: {} could not be inserted in {}", .element, .parent)]
257    ElementInsertionConflict {
258        /// The name of the parent element
259        parent: ElementName,
260        /// The name of the element that could not be inserted
261        element: ElementName,
262    },
263
264    /// The `ElementName` is not a valid sub element according to the specification.
265    #[error("Element {} is not a valid sub element of {}", .element, .parent)]
266    InvalidSubElement {
267        /// The name of the parent element
268        parent: ElementName,
269        /// The name of the element that is not a valid sub element
270        element: ElementName,
271    },
272
273    /// Remove operation failed: the given element is not a sub element of the element from which it was supposed to be removed
274    #[error("element {} not found in parent {}", .target, .parent)]
275    ElementNotFound {
276        /// The name of the element that was not found
277        target: ElementName,
278        /// The name of the parent element
279        parent: ElementName,
280    },
281
282    /// [`Element::remove_sub_element`] cannot remove the SHORT-NAME of identifiable elements, as this would render the data invalid
283    #[error("the SHORT-NAME sub element may not be removed")]
284    ShortNameRemovalForbidden,
285
286    /// get/set reference target was called for an element that is not a reference
287    #[error("The current element is not a reference")]
288    NotReferenceElement,
289
290    /// The reference is invalid
291    #[error("The reference is not valid")]
292    InvalidReference,
293
294    /// An element could not be renamed, since this item name is already used by a different element
295    #[error("Duplicate item name {} in {}", .item_name, .element)]
296    DuplicateItemName {
297        /// The name of the element that could not be renamed
298        element: ElementName,
299        /// The target name that caused the error
300        item_name: String,
301    },
302
303    /// Cannot move an element into its own sub element
304    #[error("Cannot move an element into its own sub element")]
305    ForbiddenMoveToSubElement,
306
307    /// Cannot copy an element (or a hierarchy including the element) into itself
308    #[error("Cannot create a copy that includes the destination")]
309    ForbiddenCopyOfParent,
310
311    /// A parent element is currently locked by a different operation. The operation wa aborted to avoid a deadlock.
312    #[error("A parent element is currently locked by a different operation")]
313    ParentElementLocked,
314
315    /// The attribute is invalid here
316    #[error("The attribute is not valid for this element")]
317    InvalidAttribute,
318
319    /// The attribute value is invalid
320    #[error("The given value is not valid for this attribute")]
321    InvalidAttributeValue,
322
323    /// The file is from a different model and may not be used in this operation
324    #[error("The file is from a different model and may not be used in this operation")]
325    InvalidFile,
326
327    /// The file is empty and cannot be serialized
328    #[error("The file is empty and cannot be serialized")]
329    EmptyFile,
330
331    /// The newly loaded file diverges from the combined model on an element which is not splittable according to the metamodel
332    #[error("The new file could not be merged, because it diverges from the model on non-splittable element {}", .path)]
333    InvalidFileMerge {
334        /// The path of the element where the merge failed
335        path: String,
336    },
337
338    /// The operation cannot be completed because the model does not contain any files
339    #[error("The operation cannot be completed because the model does not contain any files")]
340    NoFilesInModel,
341
342    /// Modifying the fileset of this element is not allowed
343    #[error(
344        "Modifying the fileset of this element is not allowed, because the parent of the element is not marked as splittable"
345    )]
346    FilesetModificationForbidden,
347}
348
349/// An Autosar arxml file
350#[derive(Clone)]
351pub struct ArxmlFile(Arc<RwLock<ArxmlFileRaw>>);
352
353/// Weak reference to an arxml file
354///
355/// (see the documentation of [`std::sync::Arc`] for an explanation of weak references)
356#[derive(Clone)]
357pub struct WeakArxmlFile(Weak<RwLock<ArxmlFileRaw>>);
358
359/// The data of an arxml file
360pub(crate) struct ArxmlFileRaw {
361    pub(crate) version: AutosarVersion,
362    model: WeakAutosarModel,
363    pub(crate) filename: PathBuf,
364    pub(crate) xml_standalone: Option<bool>, // preserve the xml standalone attribute
365}
366
367/// An arxml element
368///
369/// This is a wrapper type which provides all the necessary manipulation functions.
370#[derive(Clone)]
371pub struct Element(Arc<RwLock<ElementRaw>>);
372
373/// Weak reference to an Element
374///
375/// (see the documentation of [`std::sync::Arc`] for an explanation of weak references)
376///
377/// This `WeakElement` can be held indefinitely without forcing the referenced data to remain valid.
378/// When access is needed, the method `upgrade()` will attempt to get a strong reference and return an [Element]
379#[derive(Clone)]
380pub struct WeakElement(Weak<RwLock<ElementRaw>>);
381
382/// The data of an arxml element
383pub(crate) struct ElementRaw {
384    pub(crate) parent: ElementOrModel,
385    pub(crate) elemname: ElementName,
386    pub(crate) elemtype: ElementType,
387    pub(crate) content: SmallVec<[ElementContent; 4]>,
388    pub(crate) attributes: SmallVec<[Attribute; 1]>,
389    pub(crate) file_membership: HashSet<WeakArxmlFile>,
390    pub(crate) comment: Option<String>,
391}
392
393/// A single attribute of an arxml element
394#[derive(Clone, PartialEq, Eq)]
395pub struct Attribute {
396    /// The name of the attribute
397    pub attrname: AttributeName,
398    /// The content of the attribute
399    pub content: CharacterData,
400}
401
402/// One content item inside an arxml element
403///
404/// Elements may contain other elements, character data, or a mixture of both, depending on their type.
405#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
406pub enum ElementContent {
407    /// A sub element
408    Element(Element),
409    /// Character data
410    CharacterData(CharacterData),
411}
412
413/// The enum `CharacterData` provides typed access to the content of elements and attributes
414///
415/// Example:
416///
417/// In the xml string ```<SHORT-NAME>SomeName</SHORT-NAME>``` the character data
418/// "`SomeName`" will be loaded as `CharacterData::String("SomeName`"), while the content of the
419/// attribute <... DEST="UNIT"> will be loaded as `CharacterData::Enum(EnumItem::Unit`)
420#[derive(Debug, PartialEq, Clone)]
421pub enum CharacterData {
422    /// Character data is an enum value
423    Enum(EnumItem),
424    /// Character data is a string
425    String(String),
426    /// Character data is an unsigned integer
427    UnsignedInteger(u64),
428    /// Character data is a floating point number
429    Float(f64),
430}
431
432/// The content type of an [Element]
433#[derive(Debug, Eq, PartialEq, Clone, Copy)]
434pub enum ContentType {
435    /// The element only contains other elements
436    Elements,
437    /// The element only contains character data
438    CharacterData,
439    /// The element contains both character data and sub elements
440    Mixed,
441}
442
443/// Holds a weak reference to either an element or an arxml file
444///
445/// This enum is used for references to the parent of each element. For all elements other than the
446/// root element, the parent is an element. The root element itself has a reference to the `ArxmlFile` structure.
447#[derive(Clone)]
448pub(crate) enum ElementOrModel {
449    Element(WeakElement),
450    Model(WeakAutosarModel),
451    None, // needed while constructing the data trees, otherwise there's a chicken vs. egg problem
452}
453
454/// Possible kinds of compatibility errors that can be found by `ArxmlFile::check_version_compatibility()`
455pub enum CompatibilityError {
456    /// The element is not allowed in the target version
457    IncompatibleElement {
458        /// The element that is not allowed
459        element: Element,
460        /// The version mask of the element which indicates all allowed versions
461        version_mask: u32,
462    },
463    /// The attribute is not allowed in the target version
464    IncompatibleAttribute {
465        /// The element that contains the incompatible attribute
466        element: Element,
467        /// The incompatible attribute
468        attribute: AttributeName,
469        /// The version mask of the element which indicates all versions where the attribute is allowed
470        version_mask: u32,
471    },
472    /// The attribute value is not allowed in the target version
473    IncompatibleAttributeValue {
474        /// The element that contains the incompatible attribute
475        element: Element,
476        /// The incompatible attribute
477        attribute: AttributeName,
478        /// The incompatible attribute value
479        attribute_value: String,
480        /// The version mask of the element which indicates all versions where the attribute value is allowed
481        version_mask: u32,
482    },
483}
484
485/// information about a sub element
486///
487/// This structure is returned by [`Element::list_valid_sub_elements()`]
488pub struct ValidSubElementInfo {
489    /// name of the potential sub element
490    pub element_name: ElementName,
491    /// is the sub element named, i.e. does it need to be created with [`Element::create_named_sub_element()`]
492    pub is_named: bool,
493    /// is the sub element currently allowed, given the existing content of the element. Note that some sub elements are mutually exclusive.
494    pub is_allowed: bool,
495}
496
497const CHECK_FILE_SIZE: usize = 4096; // 4kb
498
499/// Check a file to see if it looks like an arxml file
500///
501/// Reads the beginning of the given file and checks if the data starts with a valid arxml header.
502/// If a header is found it immediately returns true and does not check any further data
503///
504/// The function returns false if the file cannot be read or if the data does not start with an arxml header
505///
506/// # Parameters
507/// - filename: name of the file to check
508///
509/// # Example
510///
511/// ```
512/// # let filename = "";
513/// if autosar_data::check_file(filename) {
514///     // it looks like an arxml file
515/// }
516/// ```
517pub fn check_file<P: AsRef<Path>>(filename: P) -> bool {
518    let mut buffer: [u8; CHECK_FILE_SIZE] = [0; CHECK_FILE_SIZE];
519
520    if File::open(filename).and_then(|mut file| file.read(&mut buffer)).is_ok() {
521        check_buffer(&buffer)
522    } else {
523        false
524    }
525}
526
527/// Check a buffer to see if the content looks like arxml data
528///
529/// The function returns true if the buffer starts with a valid arxml header (after skipping whitespace and comments).
530/// This function does not check anything after the header.
531///
532/// # Parameters
533/// - buffer: u8 slice containing the data to check
534///
535/// # Example
536/// ```
537/// # let buffer = Vec::new();
538/// if autosar_data::check_buffer(&buffer) {
539///     // it looks like arxml data
540/// }
541/// ```
542#[must_use]
543pub fn check_buffer(buffer: &[u8]) -> bool {
544    let mut parser = ArxmlParser::new(PathBuf::from("none"), buffer, false);
545    parser.check_arxml_header()
546}
547
548/// Custom Debug implementation for `Attribute`, in order to provide better formatting
549impl std::fmt::Debug for Attribute {
550    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
551        write!(f, "Attribute: {:?} = {:?}", self.attrname, self.content)
552    }
553}
554
555/// provide PartialOrd for attributes; this is used while sorting elements
556impl PartialOrd for Attribute {
557    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
558        Some(self.cmp(other))
559    }
560}
561
562/// provide Ord for attributes; this is used while sorting elements
563impl Ord for Attribute {
564    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
565        self.attrname
566            .to_str()
567            .cmp(other.attrname.to_str())
568            .then(self.content.cmp(&other.content))
569    }
570}
571
572#[cfg(test)]
573mod test {
574    use std::{error::Error, io::Write, path::PathBuf};
575    use tempfile::tempdir;
576
577    use crate::*;
578
579    #[test]
580    fn error_traits() {
581        let err = AutosarDataError::ParserError {
582            filename: PathBuf::from("filename.arxml"),
583            line: 123,
584            source: crate::parser::ArxmlParserError::InvalidArxmlFileHeader,
585        };
586        assert!(err.source().is_some());
587        let errstr = format!("{err}");
588        let errdbg = format!("{err:#?}");
589        assert!(errstr != errdbg);
590    }
591
592    #[test]
593    fn test_check_file() {
594        let dir = tempdir().unwrap();
595
596        // called on a directory rather than a file -> false
597        assert!(!check_file(dir.path()));
598
599        // nonexistent file -> false
600        let nonexistent = dir.path().with_file_name("nonexistent.arxml");
601        assert!(!check_file(nonexistent));
602
603        // arbitrary non-arxml data -> false
604        let not_arxml_file = dir.path().with_file_name("not_arxml.bin");
605        File::create(&not_arxml_file)
606            .and_then(|mut file| write!(file, "text"))
607            .unwrap();
608        assert!(!check_file(not_arxml_file));
609
610        // file containing a valid arxml header -> true
611        let header = r#"<?xml version="1.0" encoding="utf-8"?>
612        <!-- comment --><!-- comment 2 -->
613        <AUTOSAR xsi:schemaLocation="http://autosar.org/schema/r4.0 AUTOSAR_00050.xsd" xmlns="http://autosar.org/schema/r4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">"#;
614        let arxml_file = dir.path().with_file_name("file.arxml");
615        File::create(&arxml_file)
616            .and_then(|mut file| file.write(header.as_bytes()))
617            .unwrap();
618        assert!(check_file(arxml_file));
619
620        assert!(check_buffer(header.as_bytes()));
621    }
622
623    #[test]
624    fn attribute_order() {
625        // attribute ordering: first by name, then by value
626        let a1 = Attribute {
627            attrname: AttributeName::Uuid,
628            content: CharacterData::String("Value1".to_string()),
629        };
630
631        let a2 = Attribute {
632            attrname: AttributeName::Uuid,
633            content: CharacterData::String("Value2".to_string()),
634        };
635        assert!(a1 < a2);
636
637        let a3 = Attribute {
638            attrname: AttributeName::T,
639            content: CharacterData::String("xyz".to_string()),
640        };
641        assert!(a3 < a1);
642
643        // PartialOrd
644        assert!(a1.partial_cmp(&a2) == Some(std::cmp::Ordering::Less));
645    }
646}