Skip to main content

fmi_schema/
lib.rs

1#![doc=include_str!( "../README.md")]
2//! ## Feature flags
3#![doc = document_features::document_features!()]
4#![deny(unsafe_code)]
5#![deny(clippy::all)]
6
7use std::fmt::Display;
8
9use thiserror::Error;
10
11pub mod date_time;
12#[cfg(feature = "fmi2")]
13pub mod fmi2;
14#[cfg(feature = "fmi3")]
15pub mod fmi3;
16pub mod minimal;
17pub mod traits;
18pub mod utils;
19pub mod variable_counts;
20
21/// The major version of the FMI standard
22#[derive(Debug, PartialEq, Eq)]
23pub enum MajorVersion {
24    FMI1,
25    FMI2,
26    FMI3,
27}
28
29impl Display for MajorVersion {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        match self {
32            MajorVersion::FMI1 => write!(f, "1.0"),
33            MajorVersion::FMI2 => write!(f, "2.0"),
34            MajorVersion::FMI3 => write!(f, "3.0"),
35        }
36    }
37}
38
39#[derive(Debug, Error)]
40pub enum Error {
41    #[error("Variable {0} not found")]
42    VariableNotFound(String),
43
44    #[error(transparent)]
45    Semver(#[from] lenient_semver::parser::OwnedError),
46
47    #[error(transparent)]
48    XmlParse(#[from] hard_xml::XmlError),
49
50    #[error("Error in model: {0}")]
51    Model(String),
52}
53
54/// Serialize a value to XML string. If `fragment` is true, the XML declaration is omitted.
55pub fn serialize<T: hard_xml::XmlWrite>(value: &T, fragment: bool) -> Result<String, Error> {
56    let xml = hard_xml::XmlWrite::to_string(value).map_err(Error::XmlParse)?;
57    if fragment {
58        Ok(xml)
59    } else {
60        Ok(format!(r#"<?xml version="1.0" encoding="UTF-8"?>{}"#, xml))
61    }
62}
63
64pub fn deserialize<'a, T: hard_xml::XmlRead<'a>>(xml: &'a str) -> Result<T, Error> {
65    hard_xml::XmlRead::from_str(xml).map_err(Error::XmlParse)
66}