use std::fmt;
use serde::{Deserialize, Serialize};
fn debug_redacted(name: &str, value: &str, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct(name).field("len", &value.len()).finish()
}
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CapabilityToken(String);
impl CapabilityToken {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for CapabilityToken {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
debug_redacted("CapabilityToken", &self.0, f)
}
}
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ActorRef(String);
impl ActorRef {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for ActorRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
debug_redacted("ActorRef", &self.0, f)
}
}
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct OpaqueRef(String);
impl OpaqueRef {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for OpaqueRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
debug_redacted("OpaqueRef", &self.0, f)
}
}
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct PredicateRef(String);
impl PredicateRef {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for PredicateRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
debug_redacted("PredicateRef", &self.0, f)
}
}
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct IdToken(String);
impl IdToken {
pub fn new(value: impl Into<String>) -> Self {
Self(value.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Debug for IdToken {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("IdToken").field(&self.0).finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ref_debug_does_not_dump_raw_value() {
let secret = "sk-this-must-not-appear";
let shown = format!("{:?}", OpaqueRef::new(secret));
assert!(shown.contains("len"));
assert!(!shown.contains(secret));
assert!(!shown.contains("sk-"));
let cap = format!("{:?}", CapabilityToken::new("contract: agent-wait/v0"));
assert!(cap.contains("len"));
assert!(!cap.contains("agent-wait"));
let pred = format!("{:?}", PredicateRef::new("pred:secret-query"));
assert!(!pred.contains("secret-query"));
}
}