Skip to main content

Operation

Struct Operation 

Source
pub struct Operation<T> { /* private fields */ }
Expand description

An owned protocol operation. All getters are local and never send commands.

Applet factories copy required profile configuration and own their inputs; there is no caller lifetime, connection handle, or mutable global registry. Hold one exclusive application connection lease across start and every advance. Drop releases memory only, including on application transport failure.

§Examples

use canokey_protocol::{ApduHeader, ExpectedLength, OperationState, Step};
use canokey_protocol::operation::{conversation, LogicalCommand};
let command = LogicalCommand::new(ApduHeader::new(0, 0xca, 0, 0),
    vec![], ExpectedLength::Exact(256));
let mut op = conversation(command, Default::default())?;
assert_eq!(op.start()?, Step::Exchange);
assert_eq!(op.command()?.as_bytes(), &[0, 0xca, 0, 0, 0]);
// Offline response fixture; real applications supply their raw I/O result.
assert_eq!(op.advance(&[0x42, 0x90, 0])?, Step::Done);
let response = op.take_result()?;
assert_eq!(op.state(), OperationState::ResultTaken);
drop(op);
assert_eq!(response.data.as_bytes(), &[0x42]);

Implementations§

Source§

impl<T> Operation<T>

Source

pub fn state(&self) -> OperationState

Inspect the local lifecycle without advancing execution.

Source

pub fn error(&self) -> Option<&Error>

Borrow the stored protocol failure, or None before any failure. Invalid-state getter/drive calls do not overwrite this error.

Source

pub fn progress(&self) -> Option<&T>

Borrow partial results only for an operation explicitly designed to expose progress (PIV Batch and Admin). Other operations return None. Failures retain completed items/write counts without retaining execution secrets. Returns None after cancellation/result transfer and on completion, when the ordinary result getter applies. Repeated calls never advance execution.

Source

pub fn command(&self) -> Result<&CommandApdu, Error>

Borrow the pending complete physical APDU. Repeated reads never resend it.

§Errors

Returns ErrorKind::OperationStateError outside AwaitingResponse. The borrow cannot outlive the next mutable call to this operation.

Source

pub fn result(&self) -> Result<&T, Error>

Borrow the completed typed result without card access.

§Errors

Returns ErrorKind::OperationStateError unless the state is Completed.

Source

pub fn take_result(&mut self) -> Result<T, Error>

Move the result into caller ownership and enter ResultTaken. The result can outlive this operation.

§Errors

Returns ErrorKind::OperationStateError unless the state is Completed, including on any second attempt.

Source

pub fn cancel(&mut self)

Discard working data from Created/AwaitingResponse and enter Cancelled. Other states are unchanged. Does not cancel transport I/O, send logout, or roll back card effects; drain or isolate pending I/O before connection reuse.

Source

pub fn start(&mut self) -> Result<Step, Error>

Start a Created operation, exposing its first command or completing locally.

§Errors

Returns ErrorKind::OperationStateError in any other state, without changing it. Construction/encoding/machine errors enter Failed and are retained.

Source

pub fn advance(&mut self, bytes: &[u8]) -> Result<Step, Error>

Consume one complete response (data followed by SW1/SW2) to the pending command. Input is borrowed only during this call. Do not supply transport errors or responses already processed by another continuation loop.

§Errors

Outside AwaitingResponse, returns ErrorKind::OperationStateError unchanged. Malformed responses, exhausted limits, conversation violations and applet failures enter Failed, retain the error and release working state.

Trait Implementations§

Source§

impl<T> Debug for Operation<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Operation<T>

§

impl<T> !Sync for Operation<T>

§

impl<T> !UnwindSafe for Operation<T>

§

impl<T> Freeze for Operation<T>
where Option<Box<dyn Machine<T>>>: Freeze, Option<T>: Freeze,

§

impl<T> Send for Operation<T>
where Option<Box<dyn Machine<T>>>: Send, Option<T>: Send,

§

impl<T> Unpin for Operation<T>
where Option<Box<dyn Machine<T>>>: Unpin, Option<T>: Unpin,

§

impl<T> UnsafeUnpin for Operation<T>
where Option<Box<dyn Machine<T>>>: UnsafeUnpin, Option<T>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.