Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/parser/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,23 @@ impl TryFrom<Device> for S::Device {
.map(S::Pdo::try_from)
.collect::<Result<_>>()?;

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,
Expand Down
1 change: 1 addition & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct Name {
pub enum DeviceProperty {
Type(DeviceType),
Name(Vec<Name>),
Comment(String),
RxPdo(Vec<RxPdo>),
TxPdo(Vec<TxPdo>),
Sm(Vec<Sm>),
Expand Down
1 change: 1 addition & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ pub struct Device {
pub physics: Option<String>,
pub name: Names,
pub desc: String,
pub comment: Option<String>,
pub product_code: Option<u32>,
pub revision_no: Option<u32>,
pub sm: Vec<Sm>,
Expand Down
56 changes: 56 additions & 0 deletions tests/comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use ethercat_esi::EtherCatInfo;

#[test]
fn parses_optional_device_comment() {
// Minimal ESI with a Device Comment
let xml = r##"
<EtherCATInfo Version="1.0">
<Vendor>
<Id>#x00000002</Id>
<Name>Demo Vendor</Name>
</Vendor>
<Descriptions>
<Groups/>
<Devices>
<Device>
<Type ProductCode="#x1" RevisionNo="#x1">Demo Device</Type>
<Name>Device Name</Name>
<Comment>This is a demo device.</Comment>
</Device>
</Devices>
</Descriptions>
</EtherCATInfo>
"##;

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##"
<EtherCATInfo Version="1.0">
<Vendor>
<Id>#x00000002</Id>
<Name>Demo Vendor</Name>
</Vendor>
<Descriptions>
<Groups/>
<Devices>
<Device>
<Type ProductCode="#x1" RevisionNo="#x1">Demo Device</Type>
<Name>Device Name</Name>
</Device>
</Devices>
</Descriptions>
</EtherCATInfo>
"##;

let esi = EtherCatInfo::from_xml_str(xml).expect("parse ESI");
let dev = &esi.description.devices[0];
assert!(dev.comment.is_none());
}