From ee269e212e94ee30edafa822906a07342e759244 Mon Sep 17 00:00:00 2001 From: Sebastian Kuzminsky Date: Wed, 23 Jul 2025 20:23:03 -0600 Subject: [PATCH 1/4] refactor Issue::read_tags() to handle escaping --- src/issue.rs | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 102 insertions(+), 1 deletion(-) diff --git a/src/issue.rs b/src/issue.rs index 81559f4..6a7abe9 100644 --- a/src/issue.rs +++ b/src/issue.rs @@ -48,6 +48,10 @@ pub enum IssueError { ChronoParseError(#[from] chrono::format::ParseError), #[error("Failed to parse issue")] IssueParseError, + #[error("invalid escape character {escape:?} in tag file {filename:?}")] + TagInvalidEscape { escape: String, filename: String }, + #[error("invalid trailing escape character ',' in tag file {filename:?}")] + TagTrailingEscape { filename: String }, #[error("Failed to parse state")] StateParseError, #[error("Failed to run git")] @@ -527,13 +531,52 @@ impl Issue { let mut tags = Vec::::new(); for direntry in tags_direntry.path().read_dir()? { if let Ok(direntry) = direntry { - tags.push(String::from(direntry.file_name().to_string_lossy())); + let tag = Issue::tag_from_filename(&direntry.file_name().to_string_lossy())?; + tags.push(tag); } } tags.sort(); Ok(tags) } + /// Perform un-escape on a filename to make it into a tag: + /// ",0" => "," + /// ",1" => "/" + fn tag_from_filename(filename: &str) -> Result { + let mut tag = String::new(); + let mut token_iter = filename.split(','); + let Some(start) = token_iter.next() else { + return Err(IssueError::StdIoError(std::io::Error::from( + std::io::ErrorKind::NotFound, + ))); + }; + tag.push_str(start); + for token in token_iter { + match token.chars().nth(0) { + Some('0') => { + tag.push(','); + tag.push_str(&token[1..]); + } + Some('1') => { + tag.push('/'); + tag.push_str(&token[1..]); + } + Some(bogus) => { + return Err(IssueError::TagInvalidEscape { + escape: String::from(bogus), + filename: String::from(filename), + }); + } + None => { + return Err(IssueError::TagTrailingEscape { + filename: String::from(filename), + }); + } + } + } + Ok(tag) + } + fn commit_tags(&self, commit_message: &str) -> Result<(), IssueError> { let mut tags_filename = self.dir.clone(); tags_filename.push("tags"); @@ -560,6 +603,64 @@ mod tests { use super::*; use pretty_assertions::assert_eq; + #[test] + fn parse_tag_0() { + assert_eq!( + Issue::tag_from_filename("hello").unwrap(), + String::from("hello") + ); + } + + #[test] + fn parse_tag_1() { + assert_eq!( + Issue::tag_from_filename("hello,0world").unwrap(), + String::from("hello,world") + ); + } + + #[test] + fn parse_tag_2() { + assert_eq!( + Issue::tag_from_filename("hello,1world").unwrap(), + String::from("hello/world") + ); + } + + #[test] + fn parse_tag_3() { + assert_eq!( + Issue::tag_from_filename(",0hello,1world,0").unwrap(), + String::from(",hello/world,") + ); + } + + #[test] + fn parse_tag_4() { + // std::io::Error does not impl PartialEq :-( + let filename = "hello,"; + match Issue::tag_from_filename(filename) { + Ok(tag) => panic!( + "tag_from_filename() accepted invalid input {:?} and returned {:?}", + filename, tag + ), + Err(_e) => (), + } + } + + #[test] + fn parse_tag_5() { + // std::io::Error does not impl PartialEq :-( + let filename = "hello,world"; + match Issue::tag_from_filename(filename) { + Ok(tag) => panic!( + "tag_from_filename() accepted invalid input {:?} and returned {:?}", + filename, tag + ), + Err(_e) => (), + } + } + #[test] fn read_issue_0() { let issue_dir = std::path::Path::new("test/0000/3943fc5c173fdf41c0a22251593cd476/"); From 9db4e6bef86485ccfc1899443e61850456e18269 Mon Sep 17 00:00:00 2001 From: Sebastian Kuzminsky Date: Wed, 23 Jul 2025 20:24:42 -0600 Subject: [PATCH 2/4] add some tags with escapes to the tests --- src/issue.rs | 5 +++++ src/issues.rs | 5 +++++ test/0000/3943fc5c173fdf41c0a22251593cd476/tags/bird,1wing | 0 .../tags/bird,1wing,1feather | 0 test/0000/3943fc5c173fdf41c0a22251593cd476/tags/deer,0antler | 0 .../tags/deer,0antler,0tassle | 0 .../3943fc5c173fdf41c0a22251593cd476/tags/hop,0scotch,1shoe | 0 7 files changed, 10 insertions(+) create mode 100644 test/0000/3943fc5c173fdf41c0a22251593cd476/tags/bird,1wing create mode 100644 test/0000/3943fc5c173fdf41c0a22251593cd476/tags/bird,1wing,1feather create mode 100644 test/0000/3943fc5c173fdf41c0a22251593cd476/tags/deer,0antler create mode 100644 test/0000/3943fc5c173fdf41c0a22251593cd476/tags/deer,0antler,0tassle create mode 100644 test/0000/3943fc5c173fdf41c0a22251593cd476/tags/hop,0scotch,1shoe diff --git a/src/issue.rs b/src/issue.rs index 6a7abe9..fb05cb6 100644 --- a/src/issue.rs +++ b/src/issue.rs @@ -674,6 +674,11 @@ mod tests { done_time: None, tags: Vec::::from([ String::from("TAG2"), + String::from("bird/wing"), + String::from("bird/wing/feather"), + String::from("deer,antler"), + String::from("deer,antler,tassle"), + String::from("hop,scotch/shoe"), String::from("i-am-also-a-tag"), String::from("tag1"), ]), diff --git a/src/issues.rs b/src/issues.rs index d3c57c0..49d721d 100644 --- a/src/issues.rs +++ b/src/issues.rs @@ -128,6 +128,11 @@ mod tests { done_time: None, tags: Vec::::from([ String::from("TAG2"), + String::from("bird/wing"), + String::from("bird/wing/feather"), + String::from("deer,antler"), + String::from("deer,antler,tassle"), + String::from("hop,scotch/shoe"), String::from("i-am-also-a-tag"), String::from("tag1"), ]), diff --git a/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/bird,1wing b/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/bird,1wing new file mode 100644 index 0000000..e69de29 diff --git a/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/bird,1wing,1feather b/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/bird,1wing,1feather new file mode 100644 index 0000000..e69de29 diff --git a/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/deer,0antler b/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/deer,0antler new file mode 100644 index 0000000..e69de29 diff --git a/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/deer,0antler,0tassle b/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/deer,0antler,0tassle new file mode 100644 index 0000000..e69de29 diff --git a/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/hop,0scotch,1shoe b/test/0000/3943fc5c173fdf41c0a22251593cd476/tags/hop,0scotch,1shoe new file mode 100644 index 0000000..e69de29 From 01ef8679b4b6b289d9233b059085882666255eaf Mon Sep 17 00:00:00 2001 From: Sebastian Kuzminsky Date: Thu, 24 Jul 2025 12:33:57 -0600 Subject: [PATCH 3/4] Issue::commit_tags(): write new-style escaped tags --- src/issue.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/src/issue.rs b/src/issue.rs index fb05cb6..093d2e4 100644 --- a/src/issue.rs +++ b/src/issue.rs @@ -577,12 +577,28 @@ impl Issue { Ok(tag) } + // Perform escape on a tag to make it into a filename: + // "," => ",0" + // "/" => ",1" + fn tag_to_filename(tag: &str) -> String { + let mut filename = tag.replace(",", ",0"); + filename = filename.replace("/", ",1"); + return filename; + } + fn commit_tags(&self, commit_message: &str) -> Result<(), IssueError> { - let mut tags_filename = self.dir.clone(); - tags_filename.push("tags"); - let mut tags_file = std::fs::File::create(&tags_filename)?; + let mut tags_dir_name = self.dir.clone(); + tags_dir_name.push("tags"); + match std::fs::remove_dir_all(&tags_dir_name) { + Err(e) if e.kind() == std::io::ErrorKind::NotFound => (), + Err(e) => return Err(e.into()), + Ok(_) => (), + } + std::fs::create_dir(&tags_dir_name)?; for tag in &self.tags { - writeln!(tags_file, "{}", tag)?; + let mut tag_filename = tags_dir_name.clone(); + tag_filename.push(Issue::tag_to_filename(tag)); + std::fs::File::create(&tag_filename)?; } self.commit(commit_message)?; Ok(()) @@ -661,6 +677,42 @@ mod tests { } } + #[test] + fn tag_to_filename_0() { + let tag = "hello"; + assert_eq!(Issue::tag_to_filename(tag), "hello"); + } + + #[test] + fn tag_to_filename_1() { + let tag = "hello,"; + assert_eq!(Issue::tag_to_filename(tag), "hello,0"); + } + + #[test] + fn tag_to_filename_2() { + let tag = "/hello"; + assert_eq!(Issue::tag_to_filename(tag), ",1hello"); + } + + #[test] + fn tag_to_filename_3() { + let tag = "hello/bye,boo"; + assert_eq!(Issue::tag_to_filename(tag), "hello,1bye,0boo"); + } + + #[test] + fn tag_to_filename_4() { + let tag = ",,,///,,,"; + assert_eq!(Issue::tag_to_filename(tag), ",0,0,0,1,1,1,0,0,0"); + } + + #[test] + fn tag_to_filename_5() { + let tag = ",0,0,1,1"; + assert_eq!(Issue::tag_to_filename(tag), ",00,00,01,01"); + } + #[test] fn read_issue_0() { let issue_dir = std::path::Path::new("test/0000/3943fc5c173fdf41c0a22251593cd476/"); From 3d008bd390e98559f98fc1a47c8f04fa780f4de2 Mon Sep 17 00:00:00 2001 From: Sebastian Kuzminsky Date: Thu, 24 Jul 2025 12:09:22 -0600 Subject: [PATCH 4/4] update-tags-encoding: escape '/' and ',' in tags --- tools/update-tags-encoding | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/update-tags-encoding b/tools/update-tags-encoding index b276b83..03b866e 100755 --- a/tools/update-tags-encoding +++ b/tools/update-tags-encoding @@ -12,6 +12,13 @@ set -e #set -x +function escape_tag() { + TAG="$1" + TAG=$(echo "${TAG}" | sed -re 's/,/,0/g') + TAG=$(echo "${TAG}" | sed -re 's/\//,1/g') + echo "${TAG}" +} + BRANCH="" if [[ -n "$1" ]] && [[ -d "$1" ]]; then @@ -58,6 +65,7 @@ for ISSUE_ID in $(find . -maxdepth 1 -type d -regextype posix-extended -regex '\ mkdir tags for TAG in ${TAGS}; do + TAG=$(escape_tag "${TAG}") touch "tags/${TAG}" done