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