use std::path::Path;
use std::pin::Pin;
use std::sync::Arc;
use async_fs as fs;
use similar::{ChangeTag, TextDiff};
pub trait TaskBlockerResolver: Send + Sync + 'static {
fn is_open(&self, mark: &str) -> impl Future<Output = Result<bool, TaskBlockerError>> + Send;
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum TaskBlockerError {
#[error("the mark {mark} cannot be resolved: {reason}")]
Unavailable {
mark: String,
reason: String,
},
}
type IsOpen<'a> = Pin<Box<dyn Future<Output = Result<bool, TaskBlockerError>> + Send + 'a>>;
trait TaskBlockerResolverImpl: Send + Sync {
fn is_open<'a>(&'a self, mark: &'a str) -> IsOpen<'a>;
}
impl<T: TaskBlockerResolver> TaskBlockerResolverImpl for T {
fn is_open<'a>(&'a self, mark: &'a str) -> IsOpen<'a> {
Box::pin(TaskBlockerResolver::is_open(self, mark))
}
}
#[derive(Clone)]
pub struct AnyTaskBlockerResolver(Arc<dyn TaskBlockerResolverImpl>);
impl AnyTaskBlockerResolver {
#[must_use]
pub fn new(resolver: impl TaskBlockerResolver) -> Self {
Self(Arc::new(resolver))
}
pub fn is_open<'a>(
&'a self,
mark: &'a str,
) -> impl Future<Output = Result<bool, TaskBlockerError>> + Send + 'a {
self.0.is_open(mark)
}
}
impl std::fmt::Debug for AnyTaskBlockerResolver {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AnyTaskBlockerResolver")
.finish_non_exhaustive()
}
}
#[derive(Debug, Clone, Default)]
pub struct WorkingDocsSnapshot {
pub tasks_md: Option<String>,
}
pub async fn read_snapshot(sandbox_dir: &Path) -> WorkingDocsSnapshot {
let tasks_path = sandbox_dir.join("tasks.md");
let tasks_md = read_file_if_exists(&tasks_path).await;
WorkingDocsSnapshot { tasks_md }
}
#[must_use]
pub fn tasks_md_diff(
previous: &WorkingDocsSnapshot,
current: &WorkingDocsSnapshot,
) -> Option<String> {
if previous.tasks_md == current.tasks_md {
return None;
}
let diff = TextDiff::from_lines(
previous.tasks_md.as_deref().unwrap_or_default(),
current.tasks_md.as_deref().unwrap_or_default(),
);
let mut lines = Vec::new();
for change in diff.iter_all_changes() {
let prefix = match change.tag() {
ChangeTag::Delete => "- ",
ChangeTag::Insert => "+ ",
ChangeTag::Equal => continue,
};
let value = change.value().trim_end_matches('\n');
if value.is_empty() {
continue;
}
let mut line = String::from(prefix);
line.push_str(value);
lines.push(line);
if lines.len() == 48 {
lines.push("...".to_string());
break;
}
}
if lines.is_empty() {
return Some("tasks.md changed.".to_string());
}
Some(lines.join("\n"))
}
#[cfg(feature = "languages")]
#[must_use]
pub fn tasks_md_added(
previous: &WorkingDocsSnapshot,
current: &WorkingDocsSnapshot,
) -> Option<String> {
if previous.tasks_md == current.tasks_md {
return None;
}
let diff = TextDiff::from_lines(
previous.tasks_md.as_deref().unwrap_or_default(),
current.tasks_md.as_deref().unwrap_or_default(),
);
let added: String = diff
.iter_all_changes()
.filter(|change| change.tag() == ChangeTag::Insert)
.map(|change| change.value())
.collect();
(!added.trim().is_empty()).then_some(added)
}
async fn read_file_if_exists(path: &Path) -> Option<String> {
match fs::read_to_string(path).await {
Ok(content) => Some(content),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
Err(err) => {
tracing::warn!(path = %path.display(), error = %err, "failed to read working doc");
None
}
}
}