use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::body::Body;
use crate::error::Result;
use crate::face::Face;
use crate::id::DeedId;
use crate::kind::Kind;
use crate::source::Source;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProducedBy {
pub agent_id: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub activity_id: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Grant {
Host(String),
Path(PathBuf),
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Evidence {
pub deed_id: DeedId,
pub produced_by: ProducedBy,
pub grants: Vec<Grant>,
pub unix_time: u64,
pub signature: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Deed {
pub id: DeedId,
pub kind: Kind,
pub name: String,
pub paths: Vec<PathBuf>,
pub sources: Vec<Source>,
pub produced_by: ProducedBy,
pub grants: Vec<Grant>,
pub face: Face,
pub body: Body,
}
#[derive(Clone, Debug)]
pub struct Draft {
pub id: Option<DeedId>,
pub name: String,
pub sources: Vec<Source>,
pub produced_by: ProducedBy,
pub grants: Vec<Grant>,
pub body: Body,
}
impl Deed {
pub fn from_draft(draft: Draft) -> Result<Self> {
draft.body.validate()?;
if draft.produced_by.agent_id.trim().is_empty() {
return Err(crate::Error::InvalidBody(
"producedBy agent is empty".into(),
));
}
let kind = draft.body.kind();
let id = match draft.id {
Some(id) => {
if !id.as_str().starts_with(&format!("deed-{}-", kind.token())) {
return Err(crate::Error::InvalidId(id.to_string()));
}
id
}
None => mint_id(&kind, &draft.name)?,
};
let mut sources = draft.sources;
if let Body::Quote { urls, .. } = &draft.body {
for url in urls {
let src = Source::url(url)?;
if !sources.contains(&src) {
sources.push(src);
}
}
}
let name = if draft.name.trim().is_empty() {
id.to_string()
} else {
draft.name
};
let face = Face::from_kind_and_media(&kind, draft.body.media_type());
Ok(Self {
id,
kind,
name,
paths: draft.body.paths(),
sources,
produced_by: draft.produced_by,
grants: draft.grants,
face,
body: draft.body,
})
}
pub fn label(&self) -> String {
format!("{} ยท {}", self.kind.token(), self.name)
}
}
fn mint_id(kind: &Kind, name: &str) -> Result<DeedId> {
let mut slug = String::new();
for c in name.chars() {
if c.is_ascii_alphanumeric() {
slug.push(c.to_ascii_lowercase());
} else if (c == ' ' || c == '-' || c == '_') && !slug.ends_with('-') {
slug.push('-');
}
}
let slug = slug.trim_matches('-');
let slug = if slug.is_empty() { "item" } else { slug };
DeedId::mint(kind, slug)
}