1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//! # EtherCAT Slave Information (ESI).
//!
//! The EtherCAT Slave Information (ESI) file is an XML file that is used by
//! some EtherCAT master stacks to configure the slaves and generate network
//! description files.
//! However, it's main purpose is to describe how data is shared with the
//! slave, including what sync managers it uses, and what PDOs are in
//! each sync manager.
//!
//! The official XML schema can be found in the
//! *[EtherCAT Slave Information (ESI) Schema](https://www.ethercat.org/en/downloads/downloads_981F0A9A81044A878CE329DC8818F495.htm)*
//! (see `EtherCATInfo.xsd`).
//!
//! ## Example
//!
//! ```rust
//! use ethercat_esi::EtherCatInfo;
//! use std::{
//!     env,
//!     fs::File,
//!     io::{self, prelude::*},
//! };
//!
//! fn main() -> io::Result<()> {
//!     match env::args().nth(1) {
//!         None => {
//!             eprintln!("Missing filename");
//!         }
//!         Some(file_name) => {
//!             let mut xml_file = File::open(file_name)?;
//!             let mut xml_string = String::new();
//!             xml_file.read_to_string(&mut xml_string)?;
//!             let info = EtherCatInfo::from_xml_str(&xml_string)?;
//!             println!("{:#?}", info);
//!         }
//!     }
//!     Ok(())
//! }
//! ```

use std::{
    convert::TryInto,
    io::{Error, ErrorKind, Result},
};

mod parser;
mod structs;

pub use structs::*;

impl EtherCatInfo {
    pub fn from_xml_str(xml: &str) -> Result<Self> {
        let raw_info: parser::EtherCATInfo = serde_xml_rs::from_reader(xml.as_bytes())
            .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?;
        raw_info.try_into()
    }
}