use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::{ActionDescriptor, ParamType};
pub const AWAITING_CONFIRMATION: &str = "action_awaiting_confirmation";
pub const AWAITING_CAUSE: &str = "action_awaiting_cause";
pub const DECLINED: &str = "action_declined";
pub const MESSAGE_PREVIEW_TAG: &str = "draft:message";
pub const MAX_FIELD_CHARS: usize = 64 * 1024;
pub const SUMMARY_CHARS: usize = 48;
const MAX_LISTED: usize = 12;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DraftLayout {
#[default]
Fields,
Message,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MessageRole {
To,
Cc,
Bcc,
Subject,
Body,
Attachments,
}
impl MessageRole {
fn of(name: &str) -> Option<Self> {
Some(match name {
"to" | "recipients" => Self::To,
"cc" => Self::Cc,
"bcc" => Self::Bcc,
"subject" | "title" => Self::Subject,
"body" | "text" | "message" => Self::Body,
"attachments" | "files" => Self::Attachments,
_ => return None,
})
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(remote = "Self", rename_all = "snake_case")]
pub enum DraftValue {
Text {
text: String,
},
Number {
text: String,
},
Flag {
on: bool,
},
List {
count: usize,
labels: Vec<String>,
},
Resource {
label: String,
},
Withheld,
}
crate::tagged_through_json_value!(DraftValue, "type");
impl DraftValue {
#[must_use]
pub fn line(&self) -> String {
match self {
Self::Text { text } | Self::Number { text } => text.clone(),
Self::Flag { on } => if *on { "yes" } else { "no" }.to_owned(),
Self::List { count, labels } => {
let line = labels.join(", ");
match count.checked_sub(labels.len()) {
Some(more) if more > 0 => format!("{line} +{more}"),
_ => line,
}
}
Self::Resource { label } => label.clone(),
Self::Withheld => "•••".to_owned(),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DraftField {
pub path: Vec<String>,
pub label: String,
pub value: DraftValue,
#[serde(default)]
pub editable: bool,
#[serde(default)]
pub multiline: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub shows: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub role: Option<MessageRole>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Draft {
pub action_label: String,
pub resource_label: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default)]
pub layout: DraftLayout,
pub fields: Vec<DraftField>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub undoable: bool,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct DraftEdit {
pub path: Vec<String>,
pub text: String,
}
impl Draft {
#[must_use]
pub fn render(descriptor: &ActionDescriptor, arguments: &Map<String, Value>) -> Self {
let mut fields = Vec::new();
for param in &descriptor.params {
let Some(value) = arguments.get(param.name.as_ref()) else {
continue;
};
let path = vec![param.name.to_string()];
match (param.ty, value) {
(ParamType::Secret, _) => {
fields.push(field(path, ¶m.name, DraftValue::Withheld));
}
(ParamType::Resource, _) if crate::remote::person_vault::takes_item(param) => {
fields.push(field(path, ¶m.name, DraftValue::Withheld));
}
(ParamType::Object, Value::Object(object)) => {
let properties = param
.schema
.as_deref()
.and_then(|schema| schema.get("properties"))
.and_then(Value::as_object);
for (name, inner) in object {
let schema = properties.and_then(|properties| properties.get(name));
let mut path = path.clone();
path.push(name.clone());
if let Some(field) = value_field(path, name, inner, schema) {
fields.push(field);
}
}
}
(ty, value) => {
let schema = param.schema.as_deref();
let mut made = value_field(path, ¶m.name, value, schema);
if let Some(made) = made.as_mut()
&& ty == ParamType::Resource
{
made.value = DraftValue::Resource {
label: made.value.line(),
};
made.editable = false;
}
if let (Some(made), Some(raw)) = (made.as_mut(), value.as_str())
&& !raw.is_empty()
&& let Some(label) = param.choice_label(raw)
&& !label.trim().is_empty()
{
made.shows = Some(label.to_owned());
}
fields.extend(made);
}
}
}
let message = descriptor
.contract
.tags
.iter()
.any(|tag| tag == MESSAGE_PREVIEW_TAG)
|| (fields
.iter()
.any(|field| field.role == Some(MessageRole::To))
&& fields
.iter()
.any(|field| field.role == Some(MessageRole::Body)));
if !message {
for field in &mut fields {
field.role = None;
}
}
Self {
action_label: descriptor.action_label.clone(),
resource_label: descriptor.resource_label.clone(),
description: descriptor.description.clone(),
layout: if message {
DraftLayout::Message
} else {
DraftLayout::Fields
},
fields,
undoable: descriptor.contract.can_be_undone(),
}
}
#[must_use]
pub fn role(&self, role: MessageRole) -> Option<&DraftField> {
self.fields.iter().find(|field| field.role == Some(role))
}
#[must_use]
pub fn shown_line(field: &DraftField) -> String {
field.shows.clone().unwrap_or_else(|| field.value.line())
}
#[must_use]
pub fn gist(&self) -> Option<String> {
if self.layout == DraftLayout::Message {
let parts: Vec<String> = [MessageRole::To, MessageRole::Subject]
.into_iter()
.filter_map(|role| self.role(role))
.map(Draft::shown_line)
.filter(|line| !line.is_empty())
.collect();
if !parts.is_empty() {
return Some(parts.join(" · "));
}
}
let parts: Vec<String> = self
.fields
.iter()
.filter(|field| !field.multiline && field.value != DraftValue::Withheld)
.map(Draft::shown_line)
.filter(|line| !line.is_empty())
.take(2)
.collect();
(!parts.is_empty()).then(|| parts.join(" · "))
}
#[must_use]
pub fn summary(&self) -> Vec<(String, String)> {
self.fields
.iter()
.map(|field| {
let line = field
.value
.line()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ");
let cut = if line.chars().count() > SUMMARY_CHARS {
let kept: String = line.chars().take(SUMMARY_CHARS).collect();
format!("{}…", kept.trim_end())
} else {
line
};
(field.label.clone(), cut)
})
.collect()
}
pub fn apply_edits(
&self,
arguments: &Map<String, Value>,
edits: &[DraftEdit],
) -> Result<Map<String, Value>, String> {
let mut edited = arguments.clone();
for edit in edits {
set_path(&mut edited, &edit.path, self.edit_value(edit)?)?;
}
Ok(edited)
}
pub fn check_edits(&self, edits: &[DraftEdit]) -> Result<(), String> {
edits
.iter()
.try_for_each(|edit| self.edit_value(edit).map(drop))
}
fn edit_value(&self, edit: &DraftEdit) -> Result<Value, String> {
let field = self
.fields
.iter()
.find(|field| field.path == edit.path && field.editable)
.ok_or_else(|| format!("{} cannot be changed here", edit.path.join(".")))?;
if edit.text.chars().count() > MAX_FIELD_CHARS {
return Err(format!("{} is too long", field.label));
}
Ok(match &field.value {
DraftValue::Text { .. } => Value::String(edit.text.clone()),
DraftValue::Number { .. } => {
let number: serde_json::Number = edit
.text
.trim()
.parse()
.map_err(|_| format!("{} needs a number", field.label))?;
Value::Number(number)
}
DraftValue::Flag { .. } => match edit.text.trim() {
"true" | "yes" => Value::Bool(true),
"false" | "no" => Value::Bool(false),
_ => return Err(format!("{} needs yes or no", field.label)),
},
_ => return Err(format!("{} cannot be changed here", field.label)),
})
}
#[must_use]
pub fn arguments(&self) -> Option<Map<String, Value>> {
let mut arguments = Map::new();
for field in &self.fields {
let value = match &field.value {
DraftValue::Text { text } => Value::String(text.clone()),
DraftValue::Number { text } => Value::Number(text.parse().ok()?),
DraftValue::Flag { on } => Value::Bool(*on),
DraftValue::List { .. } | DraftValue::Resource { .. } | DraftValue::Withheld => {
return None;
}
};
let (last, parents) = field.path.split_last()?;
let mut object = &mut arguments;
for parent in parents {
object = object
.entry(parent.clone())
.or_insert_with(|| Value::Object(Map::new()))
.as_object_mut()?;
}
object.insert(last.clone(), value);
}
Some(arguments)
}
#[must_use]
pub fn edited(&self, edits: &[DraftEdit]) -> Self {
let mut draft = self.clone();
for edit in edits {
let Ok(value) = self.edit_value(edit) else {
continue;
};
if let Some(field) = draft
.fields
.iter_mut()
.find(|field| field.path == edit.path)
{
field.value = match value {
Value::Bool(on) => DraftValue::Flag { on },
Value::Number(number) => DraftValue::Number {
text: number.to_string(),
},
_ => DraftValue::Text {
text: edit.text.clone(),
},
};
field.shows = None;
}
}
draft
}
}
#[must_use]
pub fn arguments_digest(arguments: &Map<String, Value>) -> [u8; 32] {
let mut hasher = blake3::Hasher::new_derive_key("Tilo draft arguments v1");
tilo_canonical_json::write_object_bytes(arguments, |chunk| {
hasher.update(chunk);
});
*hasher.finalize().as_bytes()
}
fn set_path(
arguments: &mut Map<String, Value>,
path: &[String],
value: Value,
) -> Result<(), String> {
let Some((last, parents)) = path.split_last() else {
return Err("an edit names no field".into());
};
let mut object = arguments;
for parent in parents {
object = object
.get_mut(parent)
.and_then(Value::as_object_mut)
.ok_or_else(|| format!("{} is not in the draft", path.join(".")))?;
}
object.insert(last.clone(), value);
Ok(())
}
fn field(path: Vec<String>, name: &str, value: DraftValue) -> DraftField {
let role = path.last().and_then(|last| MessageRole::of(last));
DraftField {
path,
label: humanize(name),
value,
editable: false,
multiline: false,
shows: None,
role,
}
}
fn value_field(
path: Vec<String>,
name: &str,
value: &Value,
schema: Option<&Value>,
) -> Option<DraftField> {
let label = schema
.and_then(|schema| schema.get("title"))
.and_then(Value::as_str)
.map_or_else(|| humanize(name), str::to_owned);
let mut made = field(path, name, DraftValue::Withheld);
made.label = label;
if schema
.and_then(|schema| schema.get("format"))
.is_some_and(|format| format == "password")
{
return Some(made);
}
match value {
Value::Null => return None,
Value::String(text) if text.trim().is_empty() => return None,
Value::String(text) => {
let long = text.chars().count() > MAX_FIELD_CHARS;
let shown: String = text.chars().take(MAX_FIELD_CHARS).collect();
made.multiline = text.contains('\n')
|| text.chars().count() > 120
|| made.role == Some(MessageRole::Body);
made.editable = !long;
made.value = DraftValue::Text { text: shown };
}
Value::Number(number) => {
made.editable = true;
made.value = DraftValue::Number {
text: number.to_string(),
};
}
Value::Bool(on) => {
made.editable = true;
made.value = DraftValue::Flag { on: *on };
}
Value::Array(items) if items.is_empty() => return None,
Value::Array(items) => {
made.value = DraftValue::List {
count: items.len(),
labels: items.iter().take(MAX_LISTED).map(entry_label).collect(),
};
}
Value::Object(object) => {
made.value = DraftValue::Resource {
label: object_label(object).unwrap_or_else(|| humanize(name)),
};
}
}
Some(made)
}
const LABEL_KEYS: [&str; 8] = [
"label",
"name",
"title",
"filename",
"file_name",
"path",
"address",
"id",
];
fn object_label(object: &Map<String, Value>) -> Option<String> {
LABEL_KEYS.iter().find_map(|key| {
let text = object.get(*key)?.as_str()?;
let text = if *key == "path" {
text.rsplit('/').next().unwrap_or(text)
} else {
text
};
(!text.is_empty()).then(|| text.to_owned())
})
}
fn entry_label(value: &Value) -> String {
match value {
Value::String(text) => text.rsplit('/').next().unwrap_or(text).to_owned(),
Value::Object(object) => object_label(object).unwrap_or_else(|| "…".to_owned()),
other => other.to_string(),
}
}
fn humanize(name: &str) -> String {
let spaced = name.replace(['_', '-'], " ");
let mut chars = spaced.trim().chars();
chars.next().map_or_else(String::new, |first| {
first.to_uppercase().chain(chars).collect()
})
}
#[cfg(test)]
#[path = "draft_tests.rs"]
mod tests;