Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ impl TryFrom<&str> for OneNonce {
}
}

impl TryFrom<String> for OneNonce {
type Error = Error;

fn try_from(v: String) -> Result<Self, Error> {
if v.len() >= 8 && v.len() <= 88 {
Ok(OneNonce::String(v))
} else {
Err(Error::ParseError(
"nonce must be between 8 and 88 characters".to_string(),
))
}
}
}

impl fmt::Display for OneNonce {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let enc: String;
Expand Down Expand Up @@ -171,6 +185,14 @@ impl TryFrom<&str> for Nonce {
}
}

impl TryFrom<String> for Nonce {
type Error = Error;

fn try_from(v: String) -> Result<Self, Error> {
Ok(Nonce(vec![OneNonce::try_from(v)?]))
}
}

impl TryFrom<&[&str]> for Nonce {
type Error = Error;

Expand All @@ -189,6 +211,24 @@ impl TryFrom<&[&str]> for Nonce {
}
}

impl TryFrom<&[String]> for Nonce {
type Error = Error;

fn try_from(vals: &[String]) -> Result<Self, Error> {
let mut res: Nonce = Nonce(vec![]);
for (i, v) in vals.iter().enumerate() {
res.0.push(OneNonce::try_from(v.as_str()).map_err(|e| {
let msg = match e {
Error::ParseError(s) => s,
_ => e.to_string(),
};
Error::ParseError(format!("item {i}: {msg}"))
})?);
}
Ok(res)
}
}

impl TryFrom<&[Vec<u8>]> for Nonce {
type Error = Error;

Expand Down
15 changes: 15 additions & 0 deletions src/trust/claim.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

// SPDX-License-Identifier: Apache-2.0
use crate::error::Error;

Expand Down Expand Up @@ -455,6 +457,7 @@ impl TrustClaim {
}
}

/// Return the `ValueDescription` for the current value of this claim.
fn value_desc(&self) -> Option<&ValueDescription> {
let val = self.value();
if (-1..=1).contains(&val) || val == 99 {
Expand All @@ -476,6 +479,12 @@ impl std::fmt::Debug for TrustClaim {
}
}

impl Display for TrustClaim {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.value_name().fmt(f)
}
}

impl PartialEq<TrustClaim> for TrustClaim {
fn eq(&self, other: &TrustClaim) -> bool {
self.value() == other.value()
Expand Down Expand Up @@ -554,6 +563,12 @@ impl From<TrustClaim> for String {
}
}

impl From<&TrustClaim> for String {
fn from(val: &TrustClaim) -> String {
val.tag().to_string()
}
}

impl From<TrustClaim> for i8 {
fn from(val: TrustClaim) -> i8 {
val.key()
Expand Down
25 changes: 24 additions & 1 deletion src/trust/tier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{
ser::{Serialize, Serializer},
Deserialize,
};
use std::fmt;
use std::fmt::{self, Display};

/// Tier of a trustworthiness claim's value
///
Expand All @@ -21,6 +21,12 @@ pub enum TrustTier {
Contraindicated,
}

impl TrustTier {
pub fn as_str(&self) -> &str {
self.into()
}
}

impl Serialize for TrustTier {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -111,6 +117,12 @@ impl Visitor<'_> for TrustTierVisitor {
}
}

impl Display for TrustTier {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}

impl TryFrom<&str> for TrustTier {
type Error = Error;

Expand Down Expand Up @@ -150,6 +162,17 @@ impl From<TrustTier> for String {
}
}

impl From<&TrustTier> for String {
fn from(val: &TrustTier) -> String {
match val {
TrustTier::None => "none".to_string(),
TrustTier::Affirming => "affirming".to_string(),
TrustTier::Warning => "warning".to_string(),
TrustTier::Contraindicated => "contraindicated".to_string(),
}
}
}

impl<'a, 'b> From<&'a TrustTier> for &'b str {
fn from(val: &'a TrustTier) -> &'b str {
match val {
Expand Down