use jacquard_common::{BosStr, CowStr, DefaultStr, FromStaticStr};
use jacquard_derive::IntoStatic;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Hash)]
pub struct AutomatedTest;
impl core::fmt::Display for AutomatedTest {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "automatedTest")
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Hash)]
pub struct CodeReview;
impl core::fmt::Display for CodeReview {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "codeReview")
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Hash)]
pub struct FormalProof;
impl core::fmt::Display for FormalProof {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "formalProof")
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum VerificationMethod<S: BosStr = DefaultStr> {
CodeReview,
FormalProof,
SignedHash,
AutomatedTest,
Other(S),
}
impl<S: BosStr> VerificationMethod<S> {
pub fn as_str(&self) -> &str {
match self {
Self::CodeReview => "codeReview",
Self::FormalProof => "formalProof",
Self::SignedHash => "signedHash",
Self::AutomatedTest => "automatedTest",
Self::Other(s) => s.as_ref(),
}
}
pub fn from_value(s: S) -> Self {
match s.as_ref() {
"codeReview" => Self::CodeReview,
"formalProof" => Self::FormalProof,
"signedHash" => Self::SignedHash,
"automatedTest" => Self::AutomatedTest,
_ => Self::Other(s),
}
}
}
impl<S: BosStr> AsRef<str> for VerificationMethod<S> {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl<S: BosStr> core::fmt::Display for VerificationMethod<S> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.as_str())
}
}
impl<S: BosStr> Serialize for VerificationMethod<S> {
fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where
Ser: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de, S: Deserialize<'de> + BosStr> Deserialize<'de> for VerificationMethod<S> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = S::deserialize(deserializer)?;
Ok(Self::from_value(s))
}
}
impl<S: BosStr> jacquard_common::IntoStatic for VerificationMethod<S>
where
S: BosStr + jacquard_common::IntoStatic,
S::Output: BosStr,
{
type Output = VerificationMethod<S::Output>;
fn into_static(self) -> Self::Output {
match self {
VerificationMethod::CodeReview => VerificationMethod::CodeReview,
VerificationMethod::FormalProof => VerificationMethod::FormalProof,
VerificationMethod::SignedHash => VerificationMethod::SignedHash,
VerificationMethod::AutomatedTest => VerificationMethod::AutomatedTest,
VerificationMethod::Other(v) => VerificationMethod::Other(v.into_static()),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic, Hash)]
pub struct SignedHash;
impl core::fmt::Display for SignedHash {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "signedHash")
}
}