use crate::api::json::{Entry, Summary};
use crate::api::text::line;
use crate::brain::Brain;
use crate::model::{RepoLink, Task};
use anyhow::Result;
use serde::Serialize;
#[derive(Serialize)]
struct Shown<'a> {
#[serde(flatten)]
entry: Entry<'a>,
#[serde(skip_serializing_if = "Vec::is_empty")]
repo_linked_from: Vec<String>,
}
impl<'a> Shown<'a> {
fn new(brain: &Brain, task: &'a Task) -> Self {
let here = brain.project.as_ref().map(|p| RepoLink { repo: p.repo.clone(), id: task.id });
let repo_linked_from = brain
.repo_related(task.id)
.into_iter()
.filter(|(_, other)| here.as_ref().is_some_and(|here| other.repo_links.contains(here)))
.map(|(repo, other)| RepoLink { repo, id: other.id }.to_string())
.collect();
Self { entry: Entry::new(brain, task), repo_linked_from }
}
}
pub(super) fn print_entry(brain: &Brain, id: u32, json: bool, full: bool) -> Result<()> {
let task = brain.entry(id)?;
if json {
println!("{}", serde_json::to_string(&Shown::new(brain, task))?);
} else if full {
print!("{}", brain.entry_text(id)?);
} else {
println!("{}", line(task));
}
Ok(())
}
pub(super) fn print_search(brain: &Brain, query: &str, json: bool) -> Result<()> {
if json {
let summaries: Vec<Summary> = brain.search(query).into_iter().map(Summary::new).collect();
println!("{}", serde_json::to_string(&summaries)?);
} else {
print!("{}", brain.search_text(query));
}
Ok(())
}