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>
impl<T> Operation<T>
Sourcepub fn state(&self) -> OperationState
pub fn state(&self) -> OperationState
Inspect the local lifecycle without advancing execution.
Sourcepub fn error(&self) -> Option<&Error>
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.
Sourcepub fn progress(&self) -> Option<&T>
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.
Sourcepub fn command(&self) -> Result<&CommandApdu, Error>
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.
Sourcepub fn result(&self) -> Result<&T, Error>
pub fn result(&self) -> Result<&T, Error>
Borrow the completed typed result without card access.
§Errors
Returns ErrorKind::OperationStateError unless the state is Completed.
Sourcepub fn take_result(&mut self) -> Result<T, Error>
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.
Sourcepub fn cancel(&mut self)
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.
Sourcepub fn start(&mut self) -> Result<Step, Error>
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.
Sourcepub fn advance(&mut self, bytes: &[u8]) -> Result<Step, Error>
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.