From 2e740e5cf26c24039a8b78e66cc7ecd94e65b82d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uli=20K=C3=B6hler?= Date: Sat, 1 Nov 2025 05:23:23 +0100 Subject: [PATCH] Implement parsing of the tag --- src/parser/conversions.rs | 13 +++++++++ src/parser/mod.rs | 1 + src/structs.rs | 1 + tests/comment.rs | 56 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+) create mode 100644 tests/comment.rs diff --git a/src/parser/conversions.rs b/src/parser/conversions.rs index f5a7d9f..82b1d4a 100644 --- a/src/parser/conversions.rs +++ b/src/parser/conversions.rs @@ -272,10 +272,23 @@ impl TryFrom for S::Device { .map(S::Pdo::try_from) .collect::>()?; + let comment = d + .items + .iter() + .filter_map(|p| { + if let DeviceProperty::Comment(c) = p { + Some(c.clone()) + } else { + None + } + }) + .next(); + Ok(S::Device { physics: d.Physics, name, desc, + comment, product_code, revision_no, sm, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 79bf794..92b8489 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -100,6 +100,7 @@ pub struct Name { pub enum DeviceProperty { Type(DeviceType), Name(Vec), + Comment(String), RxPdo(Vec), TxPdo(Vec), Sm(Vec), diff --git a/src/structs.rs b/src/structs.rs index b3edcc3..7ed81e0 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -57,6 +57,7 @@ pub struct Device { pub physics: Option, pub name: Names, pub desc: String, + pub comment: Option, pub product_code: Option, pub revision_no: Option, pub sm: Vec, diff --git a/tests/comment.rs b/tests/comment.rs new file mode 100644 index 0000000..918a71f --- /dev/null +++ b/tests/comment.rs @@ -0,0 +1,56 @@ +use ethercat_esi::EtherCatInfo; + +#[test] +fn parses_optional_device_comment() { + // Minimal ESI with a Device Comment + let xml = r##" + + + #x00000002 + Demo Vendor + + + + + + Demo Device + Device Name + This is a demo device. + + + + + "##; + + let esi = EtherCatInfo::from_xml_str(xml).expect("parse ESI"); + assert_eq!(esi.description.devices.len(), 1); + let dev = &esi.description.devices[0]; + assert_eq!(dev.desc, "Demo Device"); + assert_eq!(dev.comment.as_deref(), Some("This is a demo device.")); +} + +#[test] +fn device_comment_is_optional() { + // Same as above but without Comment tag + let xml = r##" + + + #x00000002 + Demo Vendor + + + + + + Demo Device + Device Name + + + + + "##; + + let esi = EtherCatInfo::from_xml_str(xml).expect("parse ESI"); + let dev = &esi.description.devices[0]; + assert!(dev.comment.is_none()); +}