use crate::tmf667::document::Document;
use crate::{DateTime, HasDescription, HasId, HasName, HasValidity};
use serde::{Deserialize, Serialize};
use tmflib_derive::{HasDescription, HasId, HasName, HasValidity};
use crate::TimePeriod;
use super::MOD_PATH;
const CLASS_PATH: &str = "attachment";
#[derive(Clone, Default, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum AttachmentType {
#[default]
InLine,
External,
}
#[derive(Clone, Default, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AttachmentSize {
pub amount: f64,
pub units: String,
}
#[derive(
Clone, Default, Debug, Deserialize, HasId, HasName, HasDescription, HasValidity, Serialize,
)]
#[serde(rename_all = "camelCase")]
pub struct AttachmentRefOrValue {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub href: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub attachment_type: Option<AttachmentType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub mime_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<AttachmentSize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub valid_for: Option<TimePeriod>,
}
impl AttachmentRefOrValue {
pub fn new() -> AttachmentRefOrValue {
AttachmentRefOrValue {
valid_for: Some(TimePeriod::default()),
..AttachmentRefOrValue::create()
}
}
}
impl From<&Document> for AttachmentRefOrValue {
fn from(value: &Document) -> Self {
let validity = value
.last_update
.as_ref()
.map(|t| TimePeriod::from(t.clone() as DateTime));
AttachmentRefOrValue {
name: Some(value.get_name()),
id: Some(value.get_id()),
href: Some(value.get_href()),
description: value.description.clone(),
valid_for: validity,
..Default::default()
}
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::HasDescription;
use crate::{tmf667::document::Document, HasName};
const ATTACH_TYPE_JSON: &str = "\"inLine\"";
const ATTACH_SIZE: &str = "{
\"amount\" : 123.4,
\"units\" : \"bytes\"
}";
const ATTACH_NAME: &str = "AttachmentName";
const ATTACH_JSON: &str = "{}";
const ATTACH_DESC: &str = "AttachmentDescription";
#[test]
fn test_attachment_default() {
let attachment = AttachmentRefOrValue::new();
assert_eq!(attachment.valid_for.is_some(), true);
}
#[test]
fn test_attachment_from_document() {
let document = Document::new("A Document");
let attachment = AttachmentRefOrValue::from(&document);
assert_eq!(attachment.get_name(), document.get_name());
}
#[test]
fn test_attachmenttype_deserialize() {
let attach_type: AttachmentType =
serde_json::from_str(ATTACH_TYPE_JSON).expect("Could not parse test json");
assert_eq!(attach_type, AttachmentType::InLine);
}
#[test]
fn test_attachmentsize_deserialize() {
let attach_size: AttachmentSize =
serde_json::from_str(ATTACH_SIZE).expect("Could not parse test json");
assert_eq!(attach_size.amount, 123.4);
assert_eq!(attach_size.units.as_str(), "bytes");
}
#[test]
fn test_attach_deserialize() {
let _attach: AttachmentRefOrValue =
serde_json::from_str(ATTACH_JSON).expect("Could not parse attach JSON");
}
#[test]
fn test_attach_hasname() {
let mut attach = AttachmentRefOrValue::new();
attach.set_name(ATTACH_NAME);
assert_eq!(attach.get_name().as_str(), ATTACH_NAME);
}
#[test]
fn test_attach_hasvalidity() {
let mut attach = AttachmentRefOrValue::new();
attach.set_validity(TimePeriod::period_30days());
assert_eq!(attach.valid_for.is_some(), true);
assert_eq!(attach.valid_for.unwrap().started(), true);
}
#[test]
fn test_attach_description() {
let attach = AttachmentRefOrValue::new().description(ATTACH_DESC);
assert_eq!(attach.description.is_some(), true);
assert_eq!(attach.get_description().as_str(), ATTACH_DESC);
}
#[test]
fn test_attach_setdescription() {
let mut attach = AttachmentRefOrValue::new();
attach.set_description(ATTACH_DESC);
assert_eq!(attach.description.is_some(), true);
assert_eq!(attach.get_description().as_str(), ATTACH_DESC);
}
}