use std::collections::BTreeSet;
use chrono::{DateTime, Utc};
use uuid::Uuid;
use crate::{
domain::{
requirement::{ContentRef, Parent},
Hrid,
},
Requirement,
};
#[derive(Debug, Clone)]
pub struct RequirementView<'a> {
pub uuid: &'a Uuid,
pub hrid: &'a Hrid,
pub created: &'a DateTime<Utc>,
pub title: &'a str,
pub body: &'a str,
pub tags: &'a BTreeSet<String>,
pub parents: Vec<(Uuid, Parent)>,
}
impl RequirementView<'_> {
#[must_use]
pub fn fingerprint(&self) -> String {
ContentRef {
title: self.title,
body: self.body,
tags: self.tags,
}
.fingerprint()
}
#[must_use]
pub fn to_requirement(&self) -> Requirement {
use std::collections::HashMap;
Requirement {
content: crate::domain::requirement::Content {
title: self.title.to_string(),
body: self.body.to_string(),
tags: self.tags.clone(),
},
metadata: crate::domain::requirement::Metadata {
uuid: *self.uuid,
hrid: self.hrid.clone(),
created: *self.created,
parents: self
.parents
.iter()
.map(|(uuid, parent)| (*uuid, parent.clone()))
.collect::<HashMap<_, _>>(),
},
}
}
}