basic comment support in lib

This commit is contained in:
Sebastian Kuzminsky 2025-07-07 15:26:26 -06:00
parent 5df03337d9
commit 50509dcf59
5 changed files with 170 additions and 0 deletions

View file

@ -23,6 +23,7 @@ pub struct Issue {
pub description: String,
pub state: State,
pub dependencies: Option<Vec<IssueHandle>>,
pub comments: std::collections::HashMap<String, crate::comment::Comment>,
/// This is the directory that the issue lives in. Only used
/// internally by the entomologist library.
@ -33,6 +34,8 @@ pub struct Issue {
pub enum IssueError {
#[error(transparent)]
StdIoError(#[from] std::io::Error),
#[error(transparent)]
CommentError(#[from] crate::comment::CommentError),
#[error("Failed to parse issue")]
IssueParseError,
#[error("Failed to run git")]
@ -83,6 +86,7 @@ impl Issue {
let mut description: Option<String> = None;
let mut state = State::New; // default state, if not specified in the issue
let mut dependencies: Option<Vec<String>> = None;
let mut comments = std::collections::HashMap::<String, crate::comment::Comment>::new();
for direntry in dir.read_dir()? {
if let Ok(direntry) = direntry {
@ -101,6 +105,8 @@ impl Issue {
if deps.len() > 0 {
dependencies = Some(deps);
}
} else if file_name == "comments" && direntry.metadata()?.is_dir() {
Self::read_comments(&mut comments, &direntry.path())?;
} else {
#[cfg(feature = "log")]
debug!("ignoring unknown file in issue directory: {:?}", file_name);
@ -116,10 +122,43 @@ impl Issue {
description: description.unwrap(),
state: state,
dependencies,
comments,
dir: std::path::PathBuf::from(dir),
})
}
fn read_comments(
comments: &mut std::collections::HashMap<String, crate::comment::Comment>,
dir: &std::path::Path,
) -> Result<(), IssueError> {
for direntry in dir.read_dir()? {
if let Ok(direntry) = direntry {
let uuid = direntry.file_name();
let comment = crate::comment::Comment::new_from_dir(&direntry.path())?;
comments.insert(String::from(uuid.to_string_lossy()), comment);
}
}
Ok(())
}
pub fn new_comment(&mut self) -> Result<crate::comment::Comment, IssueError> {
let mut dir = std::path::PathBuf::from(&self.dir);
dir.push("comments");
if !dir.exists() {
println!("creating {}", dir.to_string_lossy());
std::fs::create_dir(&dir)?;
}
let rnd: u128 = rand::random();
dir.push(&format!("{:032x}", rnd));
std::fs::create_dir(&dir)?;
Ok(crate::comment::Comment {
description: String::from(""), // FIXME
dir,
})
}
pub fn new(dir: &std::path::Path) -> Result<Self, IssueError> {
let mut issue_dir = std::path::PathBuf::from(dir);
let rnd: u128 = rand::random();
@ -129,6 +168,7 @@ impl Issue {
description: String::from(""), // FIXME: kind of bogus to use the empty string as None
state: State::New,
dependencies: None,
comments: std::collections::HashMap::<String, crate::comment::Comment>::new(),
dir: issue_dir,
})
}
@ -204,6 +244,7 @@ mod tests {
description: String::from("this is the title of my issue\n\nThis is the description of my issue.\nIt is multiple lines.\n* Arbitrary contents\n* But let's use markdown by convention\n"),
state: State::New,
dependencies: None,
comments: std::collections::HashMap::<String, crate::comment::Comment>::new(),
dir: std::path::PathBuf::from(issue_dir),
};
assert_eq!(issue, expected);
@ -217,6 +258,7 @@ mod tests {
description: String::from("minimal"),
state: State::InProgress,
dependencies: None,
comments: std::collections::HashMap::<String, crate::comment::Comment>::new(),
dir: std::path::PathBuf::from(issue_dir),
};
assert_eq!(issue, expected);