#[cfg(feature = "encryption")]
use crate::security::EncryptionMetadata;
#[cfg(feature = "signatures")]
use crate::archive::SIGNATURES_PATH;
#[cfg(feature = "signatures")]
use crate::manifest::SecurityRef;
#[cfg(feature = "signatures")]
use crate::security::{Signature, SignatureFile};
use crate::Result;
use super::Document;
use super::MutableResource;
impl Document {
#[cfg(feature = "signatures")]
#[must_use]
pub fn signature_file(&self) -> Option<&SignatureFile> {
self.signature_file.as_ref()
}
#[cfg(feature = "signatures")]
#[must_use]
pub fn signatures(&self) -> &[Signature] {
self.signature_file
.as_ref()
.map_or(&[], |sf| sf.signatures.as_slice())
}
#[cfg(feature = "signatures")]
pub fn add_signature(&mut self, signature: Signature) -> Result<()> {
let doc_id = self.compute_id()?;
if let Some(sig_file) = self.signature_file.as_mut() {
sig_file.document_id = doc_id;
sig_file.add_signature(signature);
} else {
let mut sig_file = SignatureFile::new(doc_id);
sig_file.add_signature(signature);
self.signature_file = Some(sig_file);
}
self.manifest.security = Some(SecurityRef {
signatures: Some(SIGNATURES_PATH.to_string()),
encryption: self
.manifest
.security
.as_ref()
.and_then(|s| s.encryption.clone()),
});
Ok(())
}
#[cfg(feature = "signatures")]
#[must_use]
pub fn has_signatures(&self) -> bool {
self.signature_file
.as_ref()
.is_some_and(|sf| !sf.is_empty())
}
#[cfg(not(feature = "signatures"))]
#[must_use]
pub fn has_signatures(&self) -> bool {
false
}
#[cfg(feature = "encryption")]
#[must_use]
pub fn encryption_metadata(&self) -> Option<&EncryptionMetadata> {
self.encryption_metadata.as_ref()
}
#[cfg(feature = "encryption")]
#[must_use]
pub fn is_encrypted(&self) -> bool {
self.encryption_metadata.is_some()
}
#[cfg(not(feature = "encryption"))]
#[must_use]
pub fn is_encrypted(&self) -> bool {
false
}
#[cfg(feature = "encryption")]
pub fn set_encryption(&mut self, metadata: EncryptionMetadata) -> Result<()> {
self.require_mutable("set encryption")?;
self.encryption_metadata = Some(metadata);
self.manifest.modified = chrono::Utc::now();
Ok(())
}
#[cfg(feature = "encryption")]
pub fn clear_encryption(&mut self) -> Result<()> {
self.require_mutable("remove encryption")?;
self.encryption_metadata = None;
self.manifest.modified = chrono::Utc::now();
Ok(())
}
}