refactor: split up main
All checks were successful
Test / test (push) Successful in 7s

This commit is contained in:
Paul Campbell 2024-09-19 19:12:41 +01:00
parent 64ddc6a6e6
commit 5357bbe4c5
3 changed files with 71 additions and 53 deletions

25
src/init.rs Normal file
View file

@ -0,0 +1,25 @@
//
use crate::model::Config;
use crate::patterns::{issue_pattern, marker_pattern};
use anyhow::{Context, Result};
pub fn init_config() -> Result<Config, anyhow::Error> {
let config = Config::builder()
.fs(kxio::fs::new(
std::env::var("GITHUB_WORKSPACE")
.context("GITHUB_WORKSPACE")?
.into(),
))
.repo(std::env::var("GITHUB_REPOSITORY").context("GITHUB_REPOSITORY")?)
.server(std::env::var("GITHUB_SERVER_URL").context("GITHUB_SERVER_URL")?)
.prefix_pattern(marker_pattern()?)
.issue_pattern(issue_pattern()?)
.maybe_auth_token(std::env::var("REPO_TOKEN").ok())
.build();
println!("Repo: {}", config.repo());
println!("Prefix: {}", config.prefix_pattern());
println!("Issues: {}", config.issue_pattern());
Ok(config)
}

View file

@ -1,43 +1,20 @@
// //
use std::path::Path; use anyhow::Result;
use init::init_config;
use anyhow::{Context, Result}; use scanner::find_markers;
use model::{Config, FoundMarkers, Line, Marker};
use patterns::{issue_pattern, marker_pattern};
mod init;
mod model; mod model;
mod patterns; mod patterns;
mod scanner;
fn main() -> Result<()> { fn main() -> Result<()> {
println!("Forgejo TODO Checker!"); println!("Forgejo TODO Checker!");
let config = Config::builder() let config = init_config()?;
.fs(kxio::fs::new(
std::env::var("GITHUB_WORKSPACE")
.context("GITHUB_WORKSPACE")?
.into(),
))
.repo(std::env::var("GITHUB_REPOSITORY").context("GITHUB_REPOSITORY")?)
.server(std::env::var("GITHUB_SERVER_URL").context("GITHUB_SERVER_URL")?)
.prefix_pattern(marker_pattern()?)
.issue_pattern(issue_pattern()?)
.maybe_auth_token(std::env::var("REPO_TOKEN").ok())
.build();
println!("Repo: {}", config.repo()); let markers = find_markers(config)?;
println!("Prefix: {}", config.prefix_pattern()); println!("{markers}");
println!("Issues: {}", config.issue_pattern());
let mut found_markers = FoundMarkers::default();
for file in ignore::Walk::new(config.fs().base()).flatten() {
let path = file.path();
if config.fs().path_is_file(path)? {
scan_file(path, &config, &mut found_markers)?;
}
}
println!("{found_markers}");
// TODO: add authentication when provided // TODO: add authentication when provided
// let issues = ureq::get(&api).call()?.into_string()?; // let issues = ureq::get(&api).call()?.into_string()?;
@ -51,25 +28,3 @@ fn main() -> Result<()> {
Ok(()) Ok(())
} }
fn scan_file(file: &Path, config: &Config, found_markers: &mut FoundMarkers) -> Result<()> {
let relative_path = file.strip_prefix(config.fs().base())?.to_path_buf();
config
.fs()
.file_read_to_string(file)?
.lines()
.enumerate()
.map(|(n, line)| {
Line::builder()
.file(file.to_path_buf())
.relative_path(relative_path.clone())
.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(())
}

38
src/scanner.rs Normal file
View file

@ -0,0 +1,38 @@
//
use std::path::Path;
use crate::model::{Config, FoundMarkers, Line, Marker};
use anyhow::Result;
pub fn find_markers(config: Config) -> Result<FoundMarkers, anyhow::Error> {
let mut markers = FoundMarkers::default();
for file in ignore::Walk::new(config.fs().base()).flatten() {
let path = file.path();
if config.fs().path_is_file(path)? {
scan_file(path, &config, &mut markers)?;
}
}
Ok(markers)
}
fn scan_file(file: &Path, config: &Config, found_markers: &mut FoundMarkers) -> Result<()> {
let relative_path = file.strip_prefix(config.fs().base())?.to_path_buf();
config
.fs()
.file_read_to_string(file)?
.lines()
.enumerate()
.map(|(n, line)| {
Line::builder()
.file(file.to_path_buf())
.relative_path(relative_path.clone())
.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(())
}