ctaphid-types 0.2.0

Data types for the CTAPHID protocol
Documentation
// Copyright (C) 2021 Robin Krahl <robin.krahl@ireas.org>
// SPDX-License-Identifier: Apache-2.0 or MIT

use crate::{channel::Channel, command::Command, message::Message};

/// A CTAPHID transaction.
///
/// See [ยง 11.2.2 of the CTAP specification][spec].
///
/// [spec]: https://fidoalliance.org/specs/fido-v2.1-ps-20210615/fido-client-to-authenticator-protocol-v2.1-ps-20210615.html#usb-protocol-and-framing
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Transaction<T: AsRef<[u8]>, S: AsRef<[u8]>> {
    /// The channel used for this transaction.
    pub channel: Channel,
    /// The command that is executed in this transaction.
    pub command: Command,
    /// The request payload.
    pub request: T,
    /// The response payload.
    pub response: S,
}

impl<T: AsRef<[u8]>, S: AsRef<[u8]>> Transaction<T, S> {
    /// Returns the request message of this transaction.
    pub fn request_message(&self) -> Message<&[u8]> {
        Message {
            channel: self.channel,
            command: self.command,
            data: self.request.as_ref(),
        }
    }

    /// Returns the response message of this transaction.
    pub fn response_message(&self) -> Message<&[u8]> {
        Message {
            channel: self.channel,
            command: self.command,
            data: self.response.as_ref(),
        }
    }
}