use super::signed_info::SignedInfo;
use super::stamp_annot::StampAnnot;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SealImageType {
OFD,
PNG,
GIF,
SVG,
JPEG,
Unknown,
}
impl SealImageType {
#[must_use]
pub fn from_mime(s: &str) -> Self {
match s.to_lowercase().as_str() {
"ofd" => Self::OFD,
"png" => Self::PNG,
"gif" => Self::GIF,
"svg" => Self::SVG,
"jpeg" | "jpg" => Self::JPEG,
_ => Self::Unknown,
}
}
#[must_use]
pub fn as_str(&self) -> &'static str {
match self {
Self::OFD => "OFD",
Self::PNG => "PNG",
Self::GIF => "GIF",
Self::SVG => "SVG",
Self::JPEG => "JPEG",
Self::Unknown => "Unknown",
}
}
}
impl std::fmt::Display for SealImageType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone)]
pub struct StampAnnotEntity {
pub signed_info: SignedInfo,
pub image_byte: Vec<u8>,
pub img_type: SealImageType,
}
impl StampAnnotEntity {
#[must_use]
pub fn new(signed_info: SignedInfo, image_byte: Vec<u8>, img_type: SealImageType) -> Self {
Self {
signed_info,
image_byte,
img_type,
}
}
pub fn stamp_annots(&self) -> &[StampAnnot] {
&self.signed_info.stamp_annots
}
pub fn image_byte(&self) -> &[u8] {
&self.image_byte
}
pub fn img_type(&self) -> SealImageType {
self.img_type
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::signatures::reference::Reference;
use crate::signatures::references::References;
use crate::signatures::signed_info::Provider;
fn make_signed_info() -> SignedInfo {
SignedInfo::new(
Provider::new("TestProvider"),
References::new().add_reference(Reference::new("/Doc_0/Document.xml", "hash")),
)
}
#[test]
fn test_stamp_annot_entity_new() {
let entity = StampAnnotEntity::new(
make_signed_info(),
vec![0x89, 0x50, 0x4E, 0x47],
SealImageType::PNG,
);
assert_eq!(entity.image_byte(), &[0x89, 0x50, 0x4E, 0x47]);
assert_eq!(entity.img_type(), SealImageType::PNG);
assert!(entity.stamp_annots().is_empty());
}
#[test]
fn test_stamp_annot_entity_with_annots() {
use crate::basic_type::{ST_Box, ST_RefID};
let si = make_signed_info().add_stamp_annot(StampAnnot::new(
"s1",
ST_RefID::new(1),
ST_Box::new(0.0, 0.0, 100.0, 100.0),
));
let entity = StampAnnotEntity::new(si, vec![1, 2, 3], SealImageType::OFD);
assert_eq!(entity.stamp_annots().len(), 1);
assert_eq!(entity.stamp_annots()[0].id, "s1");
}
#[test]
fn test_seal_image_type_from_mime() {
assert_eq!(SealImageType::from_mime("PNG"), SealImageType::PNG);
assert_eq!(SealImageType::from_mime("png"), SealImageType::PNG);
assert_eq!(SealImageType::from_mime("jpg"), SealImageType::JPEG);
assert_eq!(SealImageType::from_mime("svg"), SealImageType::SVG);
assert_eq!(SealImageType::from_mime("bmp"), SealImageType::Unknown);
}
#[test]
fn test_seal_image_type_display() {
assert_eq!(SealImageType::PNG.to_string(), "PNG");
assert_eq!(SealImageType::OFD.to_string(), "OFD");
}
}