From 3bdb9140522600277cfa16af0bc3cb9055d8f584 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 18 Sep 2024 15:28:17 +0100 Subject: [PATCH] refactor: markers as enum parsed from lines Fold Marker and IssueMarker into an enum. Add functions to Line to parse into a Marker. --- .ignore | 1 + src/main.rs | 45 +++++++++++-------------------------- src/model/line.rs | 29 ++++++++++++++++++++++++ src/model/markers/found.rs | 6 +---- src/model/markers/kind.rs | 7 ------ src/model/markers/marker.rs | 36 +++++------------------------ src/model/markers/mod.rs | 3 --- src/model/mod.rs | 3 ++- 8 files changed, 51 insertions(+), 79 deletions(-) create mode 100644 .ignore delete mode 100644 src/model/markers/kind.rs diff --git a/.ignore b/.ignore new file mode 100644 index 0000000..e986a72 --- /dev/null +++ b/.ignore @@ -0,0 +1 @@ +**/tests diff --git a/src/main.rs b/src/main.rs index bd2b834..adba03e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use std::path::Path; use anyhow::{Context, Result}; -use model::{Config, FoundMarkers, Line, Marker, MarkerKind}; +use model::{Config, FoundMarkers, Line, Marker}; use patterns::{issue_pattern, marker_pattern}; mod model; @@ -37,7 +37,7 @@ fn main() -> Result<()> { } } - println!("{found_markers:?}"); + println!("{found_markers:#?}"); // TODO: add authentication when provided // let issues = ureq::get(&api).call()?.into_string()?; @@ -59,35 +59,16 @@ fn scan_file(file: &Path, config: &Config, found_markers: &mut FoundMarkers) -> .file_read_to_string(file)? .lines() .enumerate() - .filter_map(|(n, line)| prefix_match(n, line, file, config)) - .for_each(|marker| { - println!("- {}", marker.line().value()); - if let Some(issue) = config - .issue_pattern() - .find(marker.line().value()) - .map(|issue| issue.as_str()) - .and_then(|issue| issue.parse::().ok()) - { - found_markers.add_issue_marker(marker.into_issue_marker(issue)); - } else { - found_markers.add_marker(marker); - } - }); + .map(|(n, line)| { + Line::builder() + .file(file.to_path_buf()) + .num(n) + .value(line.to_owned()) + .build() + }) + .filter_map(|line| line.into_marker().ok()) + .filter(|marker| !matches!(marker, Marker::Unmarked)) + .for_each(|marker| found_markers.add_marker(marker)); + Ok(()) } - -fn prefix_match(num: usize, line: &str, path: &Path, config: &Config) -> Option { - let find = config.prefix_pattern().find(line)?; - let kind = match find.as_str() { - "TODO" => Some(MarkerKind::Todo), - "FIXME" => Some(MarkerKind::Fixme), - _ => None, - }?; - Some( - Marker::builder() - .kind(kind) - .file(path.to_path_buf()) - .line(Line::builder().num(num).value(line.to_owned()).build()) - .build(), - ) -} diff --git a/src/model/line.rs b/src/model/line.rs index 6d21791..cae6d4c 100644 --- a/src/model/line.rs +++ b/src/model/line.rs @@ -1,18 +1,47 @@ // #![allow(dead_code)] +use std::path::{Path, PathBuf}; + +use anyhow::{Context, Result}; use bon::Builder; +use crate::patterns::{issue_pattern, marker_pattern}; + +use super::Marker; + #[derive(Debug, Builder)] pub struct Line { + file: PathBuf, num: usize, value: String, } impl Line { + pub fn file(&self) -> &Path { + &self.file + } pub fn num(&self) -> usize { self.num } pub fn value(&self) -> &str { &self.value } + + pub fn into_marker(self) -> Result { + if marker_pattern()?.find(&self.value).is_some() { + match issue_pattern()?.captures(&self.value) { + Some(capture) => { + let issue = capture + .name("ISSUE_NUMBER") + .context("ISSUE_NUMBER")? + .as_str() + .to_owned(); + Ok(Marker::Valid(self, issue)) + } + None => Ok(Marker::Invalid(self)), + } + } else { + Ok(Marker::Unmarked) + } + } } diff --git a/src/model/markers/found.rs b/src/model/markers/found.rs index c2fa490..74940ce 100644 --- a/src/model/markers/found.rs +++ b/src/model/markers/found.rs @@ -1,16 +1,12 @@ -use super::{IssueMarker, Marker}; +use super::Marker; // #[derive(Debug, Default)] pub struct FoundMarkers { markers: Vec, - issue_markers: Vec, } impl FoundMarkers { pub fn add_marker(&mut self, marker: Marker) { self.markers.push(marker); } - pub fn add_issue_marker(&mut self, issue_marker: IssueMarker) { - self.issue_markers.push(issue_marker); - } } diff --git a/src/model/markers/kind.rs b/src/model/markers/kind.rs deleted file mode 100644 index fc5a4bc..0000000 --- a/src/model/markers/kind.rs +++ /dev/null @@ -1,7 +0,0 @@ -// -/// What type of comment #[derive(Debug)] -#[derive(Debug)] -pub enum MarkerKind { - Todo, - Fixme, -} diff --git a/src/model/markers/marker.rs b/src/model/markers/marker.rs index c7b32c1..314793f 100644 --- a/src/model/markers/marker.rs +++ b/src/model/markers/marker.rs @@ -1,37 +1,11 @@ // #![allow(dead_code)] -use std::path::{Path, PathBuf}; - -use bon::Builder; - use crate::model::Line; -use super::{IssueMarker, MarkerKind}; - -/// Represents a TODO or FIXME comment that doesn't have any issue number -#[derive(Debug, Builder)] -pub struct Marker { - /// What type of marker - kind: MarkerKind, - - /// Path of the file - file: PathBuf, - - /// The line from the file - line: Line, -} -impl Marker { - pub fn kind(&self) -> &MarkerKind { - &self.kind - } - pub fn file(&self) -> &Path { - &self.file - } - pub fn line(&self) -> &Line { - &self.line - } - pub fn into_issue_marker(self, issue: usize) -> IssueMarker { - IssueMarker::builder().marker(self).issue(issue).build() - } +#[derive(Debug)] +pub enum Marker { + Unmarked, + Invalid(Line), + Valid(Line, String), } diff --git a/src/model/markers/mod.rs b/src/model/markers/mod.rs index 7f1f014..c6f6f0b 100644 --- a/src/model/markers/mod.rs +++ b/src/model/markers/mod.rs @@ -1,10 +1,7 @@ // mod found; mod issue; -mod kind; mod marker; pub use found::FoundMarkers; -pub use issue::IssueMarker; -pub use kind::MarkerKind; pub use marker::Marker; diff --git a/src/model/mod.rs b/src/model/mod.rs index b6820a5..de93b0d 100644 --- a/src/model/mod.rs +++ b/src/model/mod.rs @@ -5,4 +5,5 @@ mod markers; pub use config::Config; pub use line::Line; -pub use markers::*; +pub use markers::FoundMarkers; +pub use markers::Marker;