use crate::axdr::Data;
use crate::codec::{Decode, Encode, ErrorKind, Reader, Result, Writer};
macro_rules! result_enum {
($(#[$m:meta])* $name:ident { $($code:literal $variant:ident $doc:literal,)* }) => {
$(#[$m])*
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum $name {
$(
#[doc = $doc]
$variant,
)*
Other(u8),
}
impl $name {
#[must_use]
pub const fn from_u8(v: u8) -> Self {
match v {
$($code => Self::$variant,)*
other => Self::Other(other),
}
}
#[must_use]
pub const fn as_u8(self) -> u8 {
match self {
$(Self::$variant => $code,)*
Self::Other(v) => v,
}
}
#[must_use]
pub const fn is_success(self) -> bool {
matches!(self, Self::Success)
}
pub const fn ok(self) -> core::result::Result<(), Self> {
if self.is_success() { Ok(()) } else { Err(self) }
}
}
};
}
result_enum! {
DataAccessResult {
0 Success "The attribute was read or written.",
1 HardwareFault "The meter's hardware could not serve the request.",
2 TemporaryFailure "Try again later.",
3 ReadWriteDenied "The association's access rights forbid this.",
4 ObjectUndefined "No object with that logical name and class.",
9 ObjectClassInconsistent "The class id does not match the object with that name.",
11 ObjectUnavailable "The object exists but is not accessible now.",
12 TypeUnmatched "The value's type is not the attribute's type.",
13 ScopeOfAccessViolated "Outside the range this association may address.",
14 DataBlockUnavailable "The requested block is gone.",
15 LongGetAborted "The block transfer was abandoned.",
16 NoLongGetInProgress "There is no block transfer to continue.",
17 LongSetAborted "The block transfer was abandoned.",
18 NoLongSetInProgress "There is no block transfer to continue.",
19 DataBlockNumberInvalid "The block number is not the expected one.",
250 OtherReason "Unspecified.",
}
}
result_enum! {
ActionResult {
0 Success "The method ran.",
1 HardwareFault "The meter's hardware could not serve the request.",
2 TemporaryFailure "Try again later.",
3 ReadWriteDenied "The association's access rights forbid this.",
4 ObjectUndefined "No object with that logical name and class.",
9 ObjectClassInconsistent "The class id does not match the object with that name.",
11 ObjectUnavailable "The object exists but is not accessible now.",
12 TypeUnmatched "The parameter's type is not the method's type.",
13 ScopeOfAccessViolated "Outside the range this association may address.",
14 DataBlockUnavailable "The requested block is gone.",
15 LongActionAborted "The block transfer was abandoned.",
16 NoLongActionInProgress "There is no block transfer to continue.",
250 OtherReason "Unspecified.",
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GetDataResult<'a> {
Data(Data<'a>),
Error(DataAccessResult),
}
impl<'a> GetDataResult<'a> {
pub const fn value(self) -> core::result::Result<Data<'a>, DataAccessResult> {
match self {
Self::Data(d) => Ok(d),
Self::Error(e) => Err(e),
}
}
}
impl Encode for GetDataResult<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
match self {
Self::Data(d) => {
w.write_u8(0)?;
d.encode(w)
}
Self::Error(e) => {
w.write_u8(1)?;
w.write_u8(e.as_u8())
}
}
}
}
impl<'a> Decode<'a> for GetDataResult<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
match r.u8()? {
0 => Ok(Self::Data(Data::decode(r)?)),
1 => Ok(Self::Error(DataAccessResult::from_u8(r.u8()?))),
other => Err(r.err_back(ErrorKind::InvalidTag(other), 1)),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DataBlockG<'a> {
pub last_block: bool,
pub block_number: u32,
pub result: core::result::Result<&'a [u8], DataAccessResult>,
}
impl Encode for DataBlockG<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_u8(u8::from(self.last_block))?;
w.write_u32(self.block_number)?;
match self.result {
Ok(raw) => {
w.write_u8(0)?;
w.write_length_prefixed(raw)
}
Err(e) => {
w.write_u8(1)?;
w.write_u8(e.as_u8())
}
}
}
}
impl<'a> Decode<'a> for DataBlockG<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
let last_block = r.u8()? != 0;
let block_number = r.u32()?;
let result = match r.u8()? {
0 => Ok(r.length_prefixed()?),
1 => Err(DataAccessResult::from_u8(r.u8()?)),
other => return Err(r.err_back(ErrorKind::InvalidTag(other), 1)),
};
Ok(Self { last_block, block_number, result })
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DataBlockSA<'a> {
pub last_block: bool,
pub block_number: u32,
pub raw_data: &'a [u8],
}
impl Encode for DataBlockSA<'_> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_u8(u8::from(self.last_block))?;
w.write_u32(self.block_number)?;
w.write_length_prefixed(self.raw_data)
}
}
impl<'a> Decode<'a> for DataBlockSA<'a> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
Ok(Self { last_block: r.u8()? != 0, block_number: r.u32()?, raw_data: r.length_prefixed()? })
}
}
#[derive(Debug, Clone, Copy)]
pub struct List<'a, T> {
count: usize,
raw: &'a [u8],
base: usize,
_marker: core::marker::PhantomData<fn() -> T>,
}
impl<T> PartialEq for List<'_, T> {
fn eq(&self, other: &Self) -> bool {
self.count == other.count && self.raw == other.raw
}
}
impl<T> Eq for List<'_, T> {}
impl<'a, T: Decode<'a>> List<'a, T> {
#[must_use]
pub const fn len(&self) -> usize {
self.count
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.count == 0
}
#[must_use]
pub const fn as_bytes(&self) -> &'a [u8] {
self.raw
}
pub fn iter(&self) -> impl Iterator<Item = Result<T>> + 'a {
let mut r = Reader::with_base(self.raw, self.base);
let mut left = self.count;
core::iter::from_fn(move || {
if left == 0 {
return None;
}
left -= 1;
Some(T::decode(&mut r))
})
}
#[must_use]
pub const fn from_raw(count: usize, raw: &'a [u8]) -> Self {
Self { count, raw, base: 0, _marker: core::marker::PhantomData }
}
}
impl<'a, T: Decode<'a>> Decode<'a> for List<'a, T> {
fn decode(r: &mut Reader<'a>) -> Result<Self> {
let count = r.length()?;
let base = r.offset();
let mut probe = Reader::with_base(r.rest(), base);
for _ in 0..count {
T::decode(&mut probe)?;
}
let raw = r.take(probe.consumed())?;
Ok(Self { count, raw, base, _marker: core::marker::PhantomData })
}
}
impl<T> Encode for List<'_, T> {
fn encode(&self, w: &mut dyn Writer) -> Result<()> {
w.write_length(self.count)?;
w.write_bytes(self.raw)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec::SliceWriter;
#[test]
fn results_round_trip_including_unnamed_codes() {
for v in 0u8..=255 {
assert_eq!(DataAccessResult::from_u8(v).as_u8(), v);
assert_eq!(ActionResult::from_u8(v).as_u8(), v);
}
assert_eq!(DataAccessResult::from_u8(200), DataAccessResult::Other(200));
}
#[test]
fn only_zero_is_success() {
assert!(DataAccessResult::Success.is_success());
assert!(!DataAccessResult::ReadWriteDenied.is_success());
assert!(DataAccessResult::Success.ok().is_ok());
assert_eq!(DataAccessResult::ObjectUndefined.ok().unwrap_err(), DataAccessResult::ObjectUndefined);
}
#[test]
fn get_data_result_discriminates_a_value_from_a_refusal() {
let mut buf = [0u8; 32];
let mut w = SliceWriter::new(&mut buf);
GetDataResult::Error(DataAccessResult::ReadWriteDenied).encode(&mut w).unwrap();
assert_eq!(w.as_slice(), [1, 3]);
let decoded = GetDataResult::from_bytes(&[1, 3]).unwrap();
assert_eq!(decoded, GetDataResult::Error(DataAccessResult::ReadWriteDenied));
assert!(decoded.value().is_err());
}
#[test]
fn a_data_block_carries_its_number_and_last_flag() {
let b = DataBlockG { last_block: false, block_number: 2, result: Ok(&[0xAA, 0xBB]) };
let mut buf = [0u8; 32];
let mut w = SliceWriter::new(&mut buf);
b.encode(&mut w).unwrap();
assert_eq!(w.as_slice(), [0, 0, 0, 0, 2, 0, 2, 0xAA, 0xBB]);
assert_eq!(DataBlockG::from_bytes(w.as_slice()).unwrap(), b);
}
#[test]
fn a_list_validates_every_element_before_it_is_iterated() {
let raw = [
2u8, 0x00, 0x03, 1, 0, 1, 8, 0, 255, 2, 0, 0x00, 0x08, 0, 0, 1, 0, 0, 255, 2, 0,
];
let list: List<'_, super::super::AttributeDescriptorWithSelection<'_>> =
List::from_bytes(&raw).unwrap();
assert_eq!(list.len(), 2);
let items: alloc::vec::Vec<_> = list.iter().map(|d| d.unwrap().descriptor.class_id).collect();
assert_eq!(items, [3, 8]);
}
#[test]
fn a_list_whose_elements_do_not_parse_fails_at_decode() {
let raw = [2u8, 0x00, 0x03, 1, 0, 1, 8, 0, 255, 2, 0];
let e = List::<'_, super::super::AttributeDescriptorWithSelection<'_>>::from_bytes(&raw).unwrap_err();
assert!(e.is_truncated());
}
}