use trust_tasks_rs::{TransportContext, TransportHandler};
pub const TSP_BINDING_URI: &str = "https://trusttasks.org/binding/tsp/0.1";
#[derive(Debug, Clone)]
pub struct TspTransportHandler {
local: Option<String>,
peer: Option<String>,
}
impl TspTransportHandler {
pub fn new(local: impl Into<Option<String>>, peer: impl Into<Option<String>>) -> Self {
Self {
local: local.into(),
peer: peer.into(),
}
}
pub fn local(&self) -> Option<&str> {
self.local.as_deref()
}
pub fn peer(&self) -> Option<&str> {
self.peer.as_deref()
}
}
impl TransportHandler for TspTransportHandler {
fn binding_uri(&self) -> &str {
TSP_BINDING_URI
}
fn derive_parties(&self) -> TransportContext {
TransportContext {
issuer: self.peer.clone(),
recipient: self.local.clone(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn derive_parties_maps_peer_to_issuer_and_local_to_recipient() {
let h = TspTransportHandler::new(
"did:web:maintainer.example".to_string(),
"did:web:admin.example".to_string(),
);
let ctx = h.derive_parties();
assert_eq!(ctx.issuer.as_deref(), Some("did:web:admin.example"));
assert_eq!(ctx.recipient.as_deref(), Some("did:web:maintainer.example"));
assert_eq!(h.binding_uri(), TSP_BINDING_URI);
}
#[test]
fn none_peer_yields_no_issuer() {
let h = TspTransportHandler::new("did:web:maintainer.example".to_string(), None);
let ctx = h.derive_parties();
assert!(ctx.issuer.is_none());
assert_eq!(ctx.recipient.as_deref(), Some("did:web:maintainer.example"));
}
}