use serde_json::Value;
use super::super::Aim;
use crate::reply::balls::BoundBall;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Authoring {
pub at: Aim,
pub name: String,
pub ball: Option<BoundBall>,
pub project: String,
pub title: String,
pub body: String,
pub note: String,
pub arm: String,
}
impl Authoring {
pub(super) fn of(at: Aim, name: String, ball: Option<BoundBall>) -> Self {
Self {
project: ball
.as_ref()
.map(|held| held.project.clone())
.unwrap_or_default(),
at,
name,
ball,
title: String::new(),
body: String::new(),
note: String::new(),
arm: String::new(),
}
}
pub fn filing(&self) -> bool {
self.ball.is_none()
}
pub fn filed(&self) -> Option<Value> {
if self.ball.is_some() {
return None;
}
let (project, title) = (said(&self.project)?, said(&self.title)?);
Some(crate::verbs::create(
project,
self.name.clone(),
title,
said(&self.body),
))
}
pub fn amended(&self) -> Option<Value> {
let ball = self.ball.as_ref()?;
let (title, body, note) = (said(&self.title), said(&self.body), said(&self.note));
if title.is_none() && body.is_none() && note.is_none() {
return None;
}
Some(crate::verbs::update(
ball.project.clone(),
ball.id.clone(),
self.name.clone(),
title,
body,
note,
))
}
pub fn released(&self) -> Option<Value> {
let ball = self.ball.as_ref()?;
Some(crate::verbs::release(
ball.project.clone(),
ball.id.clone(),
self.name.clone(),
))
}
pub fn delivered(&self) -> Option<Value> {
let ball = self
.ball
.as_ref()
.filter(|held| said(&self.arm) == Some(held.id.clone()))?;
Some(crate::verbs::close(
ball.project.clone(),
ball.id.clone(),
self.name.clone(),
))
}
}
fn said(typed: &str) -> Option<String> {
Some(typed.trim())
.filter(|text| !text.is_empty())
.map(str::to_owned)
}
#[cfg(test)]
mod tests;