#[allow(unused_imports)]
use alloc::collections::BTreeMap;
#[allow(unused_imports)]
use core::marker::PhantomData;
use jacquard_common::{BosStr, CowStr, DefaultStr, FromStaticStr};
#[allow(unused_imports)]
use jacquard_common::deps::codegen::unicode_segmentation::UnicodeSegmentation;
use jacquard_common::deps::smol_str::SmolStr;
use jacquard_common::types::string::{AtUri, Datetime};
use jacquard_common::types::value::Data;
use jacquard_derive::IntoStatic;
use jacquard_lexicon::lexicon::LexiconDoc;
use jacquard_lexicon::schema::LexiconSchema;
#[allow(unused_imports)]
use jacquard_lexicon::validation::{ConstraintError, ValidationPath};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, IntoStatic)]
#[serde(
rename_all = "camelCase",
bound(deserialize = "S: Deserialize<'de> + BosStr")
)]
pub struct Signature<S: BosStr = DefaultStr> {
pub attestation: S,
#[serde(skip_serializing_if = "Option::is_none")]
pub comment: Option<S>,
pub kid: S,
#[serde(skip_serializing_if = "Option::is_none")]
pub retracted_at: Option<Datetime>,
pub signed_at: Datetime,
pub signed_fields: Vec<S>,
pub src: AtUri<S>,
#[serde(flatten, default, skip_serializing_if = "Option::is_none")]
pub extra_data: Option<BTreeMap<SmolStr, Data<S>>>,
}
impl<S: BosStr> LexiconSchema for Signature<S> {
fn nsid() -> &'static str {
"dev.keytrace.signature"
}
fn def_name() -> &'static str {
"main"
}
fn lexicon_doc() -> LexiconDoc<'static> {
lexicon_doc_dev_keytrace_signature()
}
fn validate(&self) -> Result<(), ConstraintError> {
if let Some(ref value) = self.comment {
{
let count = UnicodeSegmentation::graphemes(value.as_ref(), true).count();
if count > 512usize {
return Err(ConstraintError::MaxGraphemes {
path: ValidationPath::from_field("comment"),
max: 512usize,
actual: count,
});
}
}
}
Ok(())
}
}
pub mod signature_state {
pub use crate::builder_types::{IsSet, IsUnset, Set, Unset};
#[allow(unused)]
use ::core::marker::PhantomData;
mod sealed {
pub trait Sealed {}
}
pub trait State: sealed::Sealed {
type Kid;
type SignedFields;
type Src;
type Attestation;
type SignedAt;
}
pub struct Empty(());
impl sealed::Sealed for Empty {}
impl State for Empty {
type Kid = Unset;
type SignedFields = Unset;
type Src = Unset;
type Attestation = Unset;
type SignedAt = Unset;
}
pub struct SetKid<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetKid<St> {}
impl<St: State> State for SetKid<St> {
type Kid = Set<members::kid>;
type SignedFields = St::SignedFields;
type Src = St::Src;
type Attestation = St::Attestation;
type SignedAt = St::SignedAt;
}
pub struct SetSignedFields<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetSignedFields<St> {}
impl<St: State> State for SetSignedFields<St> {
type Kid = St::Kid;
type SignedFields = Set<members::signed_fields>;
type Src = St::Src;
type Attestation = St::Attestation;
type SignedAt = St::SignedAt;
}
pub struct SetSrc<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetSrc<St> {}
impl<St: State> State for SetSrc<St> {
type Kid = St::Kid;
type SignedFields = St::SignedFields;
type Src = Set<members::src>;
type Attestation = St::Attestation;
type SignedAt = St::SignedAt;
}
pub struct SetAttestation<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetAttestation<St> {}
impl<St: State> State for SetAttestation<St> {
type Kid = St::Kid;
type SignedFields = St::SignedFields;
type Src = St::Src;
type Attestation = Set<members::attestation>;
type SignedAt = St::SignedAt;
}
pub struct SetSignedAt<St: State = Empty>(PhantomData<fn() -> St>);
impl<St: State> sealed::Sealed for SetSignedAt<St> {}
impl<St: State> State for SetSignedAt<St> {
type Kid = St::Kid;
type SignedFields = St::SignedFields;
type Src = St::Src;
type Attestation = St::Attestation;
type SignedAt = Set<members::signed_at>;
}
#[allow(non_camel_case_types)]
pub mod members {
pub struct kid(());
pub struct signed_fields(());
pub struct src(());
pub struct attestation(());
pub struct signed_at(());
}
}
pub struct SignatureBuilder<S: BosStr, St: signature_state::State> {
_state: PhantomData<fn() -> St>,
_fields: (
Option<S>,
Option<S>,
Option<S>,
Option<Datetime>,
Option<Datetime>,
Option<Vec<S>>,
Option<AtUri<S>>,
),
_type: PhantomData<fn() -> S>,
}
impl<S: BosStr> Signature<S> {
pub fn new() -> SignatureBuilder<S, signature_state::Empty> {
SignatureBuilder::new()
}
}
impl<S: BosStr> SignatureBuilder<S, signature_state::Empty> {
pub fn new() -> Self {
SignatureBuilder {
_state: PhantomData,
_fields: (None, None, None, None, None, None, None),
_type: PhantomData,
}
}
}
impl<S: BosStr, St> SignatureBuilder<S, St>
where
St: signature_state::State,
St::Attestation: signature_state::IsUnset,
{
pub fn attestation(
mut self,
value: impl Into<S>,
) -> SignatureBuilder<S, signature_state::SetAttestation<St>> {
self._fields.0 = Option::Some(value.into());
SignatureBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St: signature_state::State> SignatureBuilder<S, St> {
pub fn comment(mut self, value: impl Into<Option<S>>) -> Self {
self._fields.1 = value.into();
self
}
pub fn maybe_comment(mut self, value: Option<S>) -> Self {
self._fields.1 = value;
self
}
}
impl<S: BosStr, St> SignatureBuilder<S, St>
where
St: signature_state::State,
St::Kid: signature_state::IsUnset,
{
pub fn kid(mut self, value: impl Into<S>) -> SignatureBuilder<S, signature_state::SetKid<St>> {
self._fields.2 = Option::Some(value.into());
SignatureBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St: signature_state::State> SignatureBuilder<S, St> {
pub fn retracted_at(mut self, value: impl Into<Option<Datetime>>) -> Self {
self._fields.3 = value.into();
self
}
pub fn maybe_retracted_at(mut self, value: Option<Datetime>) -> Self {
self._fields.3 = value;
self
}
}
impl<S: BosStr, St> SignatureBuilder<S, St>
where
St: signature_state::State,
St::SignedAt: signature_state::IsUnset,
{
pub fn signed_at(
mut self,
value: impl Into<Datetime>,
) -> SignatureBuilder<S, signature_state::SetSignedAt<St>> {
self._fields.4 = Option::Some(value.into());
SignatureBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> SignatureBuilder<S, St>
where
St: signature_state::State,
St::SignedFields: signature_state::IsUnset,
{
pub fn signed_fields(
mut self,
value: impl Into<Vec<S>>,
) -> SignatureBuilder<S, signature_state::SetSignedFields<St>> {
self._fields.5 = Option::Some(value.into());
SignatureBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> SignatureBuilder<S, St>
where
St: signature_state::State,
St::Src: signature_state::IsUnset,
{
pub fn src(
mut self,
value: impl Into<AtUri<S>>,
) -> SignatureBuilder<S, signature_state::SetSrc<St>> {
self._fields.6 = Option::Some(value.into());
SignatureBuilder {
_state: PhantomData,
_fields: self._fields,
_type: PhantomData,
}
}
}
impl<S: BosStr, St> SignatureBuilder<S, St>
where
St: signature_state::State,
St::Kid: signature_state::IsSet,
St::SignedFields: signature_state::IsSet,
St::Src: signature_state::IsSet,
St::Attestation: signature_state::IsSet,
St::SignedAt: signature_state::IsSet,
{
pub fn build(self) -> Signature<S> {
Signature {
attestation: self._fields.0.unwrap(),
comment: self._fields.1,
kid: self._fields.2.unwrap(),
retracted_at: self._fields.3,
signed_at: self._fields.4.unwrap(),
signed_fields: self._fields.5.unwrap(),
src: self._fields.6.unwrap(),
extra_data: Default::default(),
}
}
pub fn build_with_data(self, extra_data: BTreeMap<SmolStr, Data<S>>) -> Signature<S> {
Signature {
attestation: self._fields.0.unwrap(),
comment: self._fields.1,
kid: self._fields.2.unwrap(),
retracted_at: self._fields.3,
signed_at: self._fields.4.unwrap(),
signed_fields: self._fields.5.unwrap(),
src: self._fields.6.unwrap(),
extra_data: Some(extra_data),
}
}
}
fn lexicon_doc_dev_keytrace_signature() -> LexiconDoc<'static> {
use alloc::collections::BTreeMap;
#[allow(unused_imports)]
use jacquard_common::{CowStr, deps::smol_str::SmolStr, types::blob::MimeType};
use jacquard_lexicon::lexicon::*;
LexiconDoc {
lexicon: Lexicon::Lexicon1,
id: CowStr::new_static("dev.keytrace.signature"),
defs: {
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("main"),
LexUserType::Object(LexObject {
description: Some(
CowStr::new_static(
"A cryptographic signature attesting to a claim",
),
),
required: Some(
vec![
SmolStr::new_static("kid"), SmolStr::new_static("src"),
SmolStr::new_static("signedAt"),
SmolStr::new_static("attestation"),
SmolStr::new_static("signedFields")
],
),
properties: {
#[allow(unused_mut)]
let mut map = BTreeMap::new();
map.insert(
SmolStr::new_static("attestation"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"The cryptographic signature (base64-encoded).",
),
),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("comment"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static("Optional comment or description."),
),
max_graphemes: Some(512usize),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("kid"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"Key identifier (e.g., date in YYYY-MM-DD format).",
),
),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("retractedAt"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"Datetime when this signature was retracted. Present only if the signature has been retracted (ISO 8601).",
),
),
format: Some(LexStringFormat::Datetime),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("signedAt"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"Datetime when the signature was created (ISO 8601).",
),
),
format: Some(LexStringFormat::Datetime),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("signedFields"),
LexObjectProperty::Array(LexArray {
description: Some(
CowStr::new_static(
"Ordered list of field names included in the signed payload (e.g., ['did', 'subject', 'type', 'verifiedAt'])",
),
),
items: LexArrayItem::String(LexString {
..Default::default()
}),
..Default::default()
}),
);
map.insert(
SmolStr::new_static("src"),
LexObjectProperty::String(LexString {
description: Some(
CowStr::new_static(
"AT URI reference to the signing key record published by the verification service (e.g., at://did:plc:serviceaccount/dev.keytrace.serverPublicKey/2024-01-15).",
),
),
format: Some(LexStringFormat::AtUri),
..Default::default()
}),
);
map
},
..Default::default()
}),
);
map
},
..Default::default()
}
}