use super::{Emit, Error, Work, Writer, XADES};
use crate::timestamp::XADES_141;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimestampKind {
Signature,
Archive,
}
pub struct Timestamp<'a, D, G, N> {
pub(super) snapshot: crate::xml::Snapshot<D, G>,
pub(super) signature: N,
pub(super) method: N,
pub(super) kind: TimestampKind,
pub(super) token: &'a structured_asn1::Element<'a>,
pub(super) producer: structured_pkix::algorithm::Producer<'a>,
pub(super) evidence: &'a structured_cms::timestamp::Evidence,
}
impl<D: Copy, G: Copy, N: Copy> Timestamp<'_, D, G, N> {
#[must_use]
pub fn evidence(&self) -> &structured_cms::timestamp::Evidence {
self.evidence
}
#[must_use]
pub fn snapshot(&self) -> crate::xml::Snapshot<D, G> {
self.snapshot
}
#[must_use]
pub fn method(&self) -> N {
self.method
}
pub(super) fn write<B: Writer, W: Work>(
&self,
writer: &mut B,
work: &W,
method: impl FnOnce(&mut B, &structured_asn1::memory::Execution<'_>) -> Result<(), B::Error>,
) -> Result<(), Error<B::Error>> {
let scoped = self.producer.execution_in(work);
let mut emit = Emit {
writer,
work: &scoped,
};
let (namespace, local) = match self.kind {
TimestampKind::Signature => (XADES, "SignatureTimeStamp"),
TimestampKind::Archive => (XADES_141, "ArchiveTimeStamp"),
};
emit.begin(namespace, local)?;
scoped.charge_work(1).map_err(Error::Resource)?;
method(emit.writer, &scoped).map_err(Error::Writer)?;
emit.binary(XADES, "EncapsulatedTimeStamp", self.token.as_bytes())?;
emit.end()
}
}