Skip to main content

ara_com/
types.rs

1//! Newtype wrappers for SOME/IP and AUTOSAR identifiers.
2//!
3//! These types prevent accidental ID confusion (e.g. passing a [`MethodId`]
4//! where a [`ServiceId`] is expected) and provide consistent `Display` and
5//! `From<u16>` implementations.
6
7use std::fmt;
8
9/// SOME/IP Service ID (16-bit).
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct ServiceId(pub u16);
12
13/// SOME/IP Method ID (16-bit). Valid range for methods: `0x0001..=0x7FFF`.
14/// The range `0x8000..=0xFFFE` is reserved for events.
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub struct MethodId(pub u16);
17
18/// SOME/IP Event ID (16-bit). By convention in the `0x8000..=0xFFFE` range.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20pub struct EventId(pub u16);
21
22/// SOME/IP Event Group ID (16-bit). Groups related events for
23/// subscription management.
24#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
25pub struct EventGroupId(pub u16);
26
27/// Service Instance ID (16-bit). Distinguishes multiple instances of the
28/// same service on the network.
29#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
30pub struct InstanceId(pub u16);
31
32/// Major interface version (8-bit). Incompatible changes increment this.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
34pub struct MajorVersion(pub u8);
35
36/// Minor interface version (32-bit). Backwards-compatible changes increment this.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub struct MinorVersion(pub u32);
39
40/// Composite identifier for a discovered service instance on the network.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
42pub struct ServiceInstanceId {
43    pub service_id: ServiceId,
44    pub instance_id: InstanceId,
45    pub major_version: MajorVersion,
46    pub minor_version: MinorVersion,
47}
48
49// --- Display impls ---
50
51impl fmt::Display for ServiceId {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        write!(f, "ServiceId(0x{:04X})", self.0)
54    }
55}
56
57impl fmt::Display for MethodId {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        write!(f, "MethodId(0x{:04X})", self.0)
60    }
61}
62
63impl fmt::Display for EventId {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        write!(f, "EventId(0x{:04X})", self.0)
66    }
67}
68
69impl fmt::Display for EventGroupId {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        write!(f, "EventGroupId(0x{:04X})", self.0)
72    }
73}
74
75impl fmt::Display for InstanceId {
76    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
77        write!(f, "InstanceId(0x{:04X})", self.0)
78    }
79}
80
81impl fmt::Display for MajorVersion {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        write!(f, "MajorVersion({})", self.0)
84    }
85}
86
87impl fmt::Display for MinorVersion {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        write!(f, "MinorVersion({})", self.0)
90    }
91}
92
93impl fmt::Display for ServiceInstanceId {
94    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95        write!(
96            f,
97            "ServiceInstance(service={}, instance={}, version={}.{})",
98            self.service_id, self.instance_id, self.major_version.0, self.minor_version.0
99        )
100    }
101}
102
103// --- From conversions ---
104
105impl From<u16> for ServiceId {
106    fn from(v: u16) -> Self {
107        ServiceId(v)
108    }
109}
110
111impl From<u16> for MethodId {
112    fn from(v: u16) -> Self {
113        MethodId(v)
114    }
115}
116
117impl From<u16> for EventId {
118    fn from(v: u16) -> Self {
119        EventId(v)
120    }
121}
122
123impl From<u16> for EventGroupId {
124    fn from(v: u16) -> Self {
125        EventGroupId(v)
126    }
127}
128
129impl From<u16> for InstanceId {
130    fn from(v: u16) -> Self {
131        InstanceId(v)
132    }
133}
134
135impl From<u8> for MajorVersion {
136    fn from(v: u8) -> Self {
137        MajorVersion(v)
138    }
139}
140
141impl From<u32> for MinorVersion {
142    fn from(v: u32) -> Self {
143        MinorVersion(v)
144    }
145}
146
147#[cfg(test)]
148mod tests {
149    use super::*;
150
151    #[test]
152    fn test_from_conversions() {
153        assert_eq!(ServiceId::from(0x1234), ServiceId(0x1234));
154        assert_eq!(MethodId::from(0x0001), MethodId(0x0001));
155        assert_eq!(EventId::from(0x8001), EventId(0x8001));
156        assert_eq!(EventGroupId::from(0x0010), EventGroupId(0x0010));
157        assert_eq!(InstanceId::from(0x0001), InstanceId(0x0001));
158        assert_eq!(MajorVersion::from(1u8), MajorVersion(1));
159        assert_eq!(MinorVersion::from(0u32), MinorVersion(0));
160    }
161
162    #[test]
163    fn test_display() {
164        assert_eq!(ServiceId(0x1234).to_string(), "ServiceId(0x1234)");
165        assert_eq!(MethodId(0x0001).to_string(), "MethodId(0x0001)");
166        assert_eq!(MajorVersion(2).to_string(), "MajorVersion(2)");
167        assert_eq!(MinorVersion(100).to_string(), "MinorVersion(100)");
168    }
169
170    #[test]
171    fn test_service_instance_id_display() {
172        let id = ServiceInstanceId {
173            service_id: ServiceId(0x1234),
174            instance_id: InstanceId(0x0001),
175            major_version: MajorVersion(1),
176            minor_version: MinorVersion(0),
177        };
178        assert_eq!(
179            id.to_string(),
180            "ServiceInstance(service=ServiceId(0x1234), instance=InstanceId(0x0001), version=1.0)"
181        );
182    }
183}