use std::{path::PathBuf, str::FromStr};
use anyhow::Result;
use crate::{
ingress::SrcType,
utils::{get_file_content, write_to_file},
};
pub fn remove_task(task_line: String, source_type: SrcType, source_location: String) -> Result<()> {
match source_type {
SrcType::File => {
let path_buf = PathBuf::from_str(&source_location)?;
let file_content = get_file_content(&path_buf)?;
let new_file: String = file_content
.lines()
.filter(|l| !l.eq(&task_line))
.map(|l| l.to_string() + "\n")
.collect();
write_to_file(source_location, new_file)
}
}
}
pub fn add_completed_task(
task_line: String,
source_type: SrcType,
source_location: String,
) -> Result<()> {
match source_type {
SrcType::File => {
let path_buf = PathBuf::from_str(&source_location)?;
let mut file_content = get_file_content(&path_buf)?;
file_content.push_str(&task_line);
file_content.push('\n');
write_to_file(source_location, file_content)
}
}
}