Skip to content
Open
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
93 changes: 93 additions & 0 deletions zvt/src/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,89 @@ pub struct SelectLanguage {
#[zvt_control_field(class = 0x80, instr = 0x00)]
pub struct Ack {}

#[derive(Debug, PartialEq)]
pub struct Nack {
pub code: u8, // The error code from the INSTR byte (0 = success, others = error)
}

impl zvt_builder::ZvtCommand for Nack {
const CLASS: u8 = 0x84;
const INSTR: u8 = 0x00; // for Nack, INSTR is the error code, that can be any u8
}

// NOTE: These methods are not actually called during serialization/deserialization.
// They exist only to satisfy the trait bound required by ZvtSerializerImpl.
// The actual serialization is handled by the specialized impl below.
impl zvt_builder::encoding::Encoding<Nack> for zvt_builder::encoding::Default {
fn decode(_bytes: &[u8]) -> zvt_builder::ZVTResult<(Nack, &[u8])> {
Err(zvt_builder::ZVTError::NonImplemented)
}

fn encode(_input: &Nack) -> Vec<u8> {
vec![]
}
}

/// Specialized impl for Nack with ADPU serialization
/// This impl overrides the default behavior to use `code` as the INSTR byte
impl zvt_builder::ZvtSerializerImpl<zvt_builder::length::Adpu, zvt_builder::encoding::Default, zvt_builder::encoding::BigEndian> for Nack {
Copy link
Contributor

@dorezyuk dorezyuk Jan 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

puh I'm not sure - from what i see we would fall into

impl<T> ZvtSerializer for T
where
    Self: ZvtCommand
        + ZvtSerializerImpl<length::Adpu, encoding::Default, encoding::BigEndian>
        + ZvtSerializerImpl,
    encoding::Default: encoding::Encoding<Self>,
{
    fn zvt_serialize(&self) -> Vec<u8> {
        // Find a more elegant way to express this.
        let tag: Tag = encoding::BigEndian::decode(&[Self::CLASS, Self::INSTR])
            .unwrap()
            .0;
        <Self as ZvtSerializerImpl<length::Adpu, encoding::Default, encoding::BigEndian>>::serialize_tagged(self, Some(tag))
    }

    fn zvt_deserialize(bytes: &[u8]) -> ZVTResult<(Self, &[u8])> {
        let tag: Tag = encoding::BigEndian::decode(&[Self::CLASS, Self::INSTR])
            .unwrap()
            .0;
        <Self as ZvtSerializerImpl<length::Adpu, encoding::Default, encoding::BigEndian>>::deserialize_tagged(bytes, Some(tag))
    }
}
```... 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think I would to the following

pub struct Nack {
    error: u8
}

impl zvt_builder::encoding::Encoding<Nack> for zvt_builder::encoding::Default {
    fn decode(_bytes: &[u8]) -> zvt_builder::ZVTResult<(Nack, &[u8])> {
        Err(zvt_builder::ZVTError::NonImplemented)
    }

    fn encode(_input: &Nack) -> Vec<u8> {
        vec![]
    }
}

impl zvt_builder::ZvtSerializerImpl for Nack {
    fn serialize_tagged(&self, tag: Option<zvt_builder::Tag>) -> Vec<u8> {
        todo!()
    }

    fn deserialize_tagged(mut bytes: &[u8], tag: Option<zvt_builder::Tag>) -> zvt_builder::ZVTResult<(Self, &[u8])> {
        todo!()
    }
}

impl zvt_builder::ZvtSerializer for Nack {
    fn zvt_serialize(&self) -> Vec<u8> {
        // here goes the real impl
        todo!()
    }

    fn zvt_deserialize(bytes: &[u8]) -> zvt_builder::ZVTResult<(Self, &[u8])> {
        // here goes the real impl
        todo!()
    }
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want me to change the implementation as you proposed?
Or are you going to integrate it as you said?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you do :)

fn serialize_tagged(&self, _tag: Option<zvt_builder::Tag>) -> Vec<u8> {
// For Nack: [CLASS] [code as INSTR] [length=0]
vec![0x84u8, self.code, 0x00]
}

fn deserialize_tagged(bytes: &[u8], _tag: Option<zvt_builder::Tag>) -> zvt_builder::ZVTResult<(Self, &[u8])> {
if bytes.len() < 3 {
return Err(zvt_builder::ZVTError::IncompleteData);
}

let class = bytes[0];
let code = bytes[1];
let length = bytes[2] as usize;

// Verify CLASS matches
if class != 0x84u8 {
return Err(zvt_builder::ZVTError::WrongTag(zvt_builder::Tag((class as u16) << 8 | code as u16)));
}

// Skip the length field and any data bytes that follow
let remaining = &bytes[3 + length..];
Ok((Nack { code }, remaining))
}
}

/// Specialized impl for Nack with protocol parsing (Empty length, Default encoding, Default endianness)
/// Used when Nack is parsed from protocol messages
/// NOTE: This is identical to the Adpu impl but required due to Rust's trait system
impl zvt_builder::ZvtSerializerImpl<zvt_builder::length::Empty, zvt_builder::encoding::Default, zvt_builder::encoding::Default> for Nack {
fn serialize_tagged(&self, _tag: Option<zvt_builder::Tag>) -> Vec<u8> {
// For protocol format: [CLASS] [code as INSTR] [length=0]
vec![0x84u8, self.code, 0x00]
}

fn deserialize_tagged(bytes: &[u8], _tag: Option<zvt_builder::Tag>) -> zvt_builder::ZVTResult<(Self, &[u8])> {
if bytes.len() < 3 {
return Err(zvt_builder::ZVTError::IncompleteData);
}

let class = bytes[0];
let code = bytes[1];
let length = bytes[2] as usize;

// Verify CLASS matches
if class != 0x84u8 {
return Err(zvt_builder::ZVTError::WrongTag(zvt_builder::Tag((class as u16) << 8 | code as u16)));
}

// Skip the length field and any data bytes that follow
let remaining = &bytes[3 + length..];
Ok((Nack { code }, remaining))
}
}




#[cfg(test)]
pub mod tests {
use super::*;
Expand Down Expand Up @@ -821,4 +904,14 @@ pub mod tests {
assert_eq!(actual, expected);
assert_eq!(expected.zvt_serialize(), bytes);
}

#[rstest::rstest]
fn test_nack() {
let bytes = vec![0x84u8, 0x12, 0x00];
let (actual, remaining) = Nack::zvt_deserialize(&bytes).unwrap();
assert_eq!(remaining.len(), 0);
let expected = Nack { code: 0x12 };
assert_eq!(actual, expected);
assert_eq!(expected.zvt_serialize(), bytes);
}
}