diff --git a/src/utils/activation_token.rs b/src/utils/activation_token.rs index 117ab480..1e96b4ef 100644 --- a/src/utils/activation_token.rs +++ b/src/utils/activation_token.rs @@ -1,5 +1,6 @@ use { - crate::utils::opaque::{opaque, Opaque, OpaqueError}, + crate::utils::opaque::{opaque, Opaque, OpaqueError, OPAQUE_LEN}, + arrayvec::ArrayString, std::{ fmt::{Display, Formatter}, str::FromStr, @@ -13,6 +14,12 @@ pub fn activation_token() -> ActivationToken { ActivationToken(opaque()) } +impl ActivationToken { + pub fn to_string(self) -> ArrayString { + self.0.to_string() + } +} + impl Display for ActivationToken { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) diff --git a/src/utils/opaque.rs b/src/utils/opaque.rs index 53fb6e6c..82e9744d 100644 --- a/src/utils/opaque.rs +++ b/src/utils/opaque.rs @@ -1,4 +1,5 @@ use { + arrayvec::ArrayString, rand::{thread_rng, Rng}, std::{ fmt::{Debug, Display, Formatter}, @@ -22,6 +23,15 @@ pub fn opaque() -> Opaque { } } +impl Opaque { + pub fn to_string(self) -> ArrayString { + use std::fmt::Write; + let mut s = ArrayString::new(); + write!(s, "{}", self).unwrap(); + s + } +} + impl Display for Opaque { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{:016x}", self.hi)?; @@ -40,20 +50,20 @@ impl FromStr for Opaque { type Err = OpaqueError; fn from_str(s: &str) -> Result { - if s.len() != LEN { + if s.len() != OPAQUE_LEN { return Err(OpaqueError::InvalidLength); } - if !s.is_char_boundary(LEN / 2) { + if !s.is_char_boundary(OPAQUE_LEN / 2) { return Err(OpaqueError::NotAscii); } - let (hi, lo) = s.split_at(LEN / 2); + let (hi, lo) = s.split_at(OPAQUE_LEN / 2); let hi = u64::from_str_radix(hi, 16).map_err(OpaqueError::Parse)?; let lo = u64::from_str_radix(lo, 16).map_err(OpaqueError::Parse)?; Ok(Self { lo, hi }) } } -const LEN: usize = 32; +pub const OPAQUE_LEN: usize = 32; #[derive(Debug, Error)] pub enum OpaqueError { diff --git a/src/utils/toplevel_identifier.rs b/src/utils/toplevel_identifier.rs index 6e6a5b43..61f44f2f 100644 --- a/src/utils/toplevel_identifier.rs +++ b/src/utils/toplevel_identifier.rs @@ -1,5 +1,6 @@ use { - crate::utils::opaque::{opaque, Opaque, OpaqueError}, + crate::utils::opaque::{opaque, Opaque, OpaqueError, OPAQUE_LEN}, + arrayvec::ArrayString, std::{ fmt::{Display, Formatter}, str::FromStr, @@ -13,6 +14,12 @@ pub fn toplevel_identifier() -> ToplevelIdentifier { ToplevelIdentifier(opaque()) } +impl ToplevelIdentifier { + pub fn to_string(self) -> ArrayString { + self.0.to_string() + } +} + impl Display for ToplevelIdentifier { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { self.0.fmt(f)