From ee96033639b1d2689056ff183e7e183db7b8890c Mon Sep 17 00:00:00 2001 From: Konstantin Fickel Date: Mon, 13 Apr 2026 19:49:50 +0200 Subject: [PATCH] refactor: remove duplicate code --- src/cli/commands/daily.rs | 34 +++++------------------------ src/cli/commands/edit.rs | 41 ++++++++--------------------------- src/cli/commands/mod.rs | 35 ++++++++++++++++++++++++++++++ src/cli/commands/timesheet.rs | 32 +++------------------------ src/cli/commands/todo.rs | 38 ++++++++------------------------ 5 files changed, 61 insertions(+), 119 deletions(-) diff --git a/src/cli/commands/daily.rs b/src/cli/commands/daily.rs index 9b108f6..b202d0f 100644 --- a/src/cli/commands/daily.rs +++ b/src/cli/commands/daily.rs @@ -4,38 +4,13 @@ use std::process::Command; use chrono::{Days, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc}; use chrono_tz::Tz; -use walkdir::WalkDir; use crate::config::Settings; use crate::error::StreamdError; -use crate::extract::parse_markdown_file; -use crate::localize::localize_stream_file; -use crate::models::{LocalizedShard, RepositoryConfiguration}; +use crate::models::RepositoryConfiguration; use crate::timesheet::load_repository_config; -fn load_all_shards(base_folder: &Path, tz: Tz) -> Result, StreamdError> { - let config = RepositoryConfiguration::new(); - let mut shards = Vec::new(); - - for entry in WalkDir::new(base_folder) - .max_depth(1) - .into_iter() - .filter_map(|e| e.ok()) - { - let path = entry.path(); - if path.extension().map(|e| e == "md").unwrap_or(false) { - let file_name = path.to_string_lossy().to_string(); - let content = fs::read_to_string(path)?; - let stream_file = parse_markdown_file(&file_name, &content); - - if let Ok(shard) = localize_stream_file(&stream_file, &config, tz) { - shards.push(shard); - } - } - } - - Ok(shards) -} +use super::load_markdown_shards; pub fn run(date: Option) -> Result<(), StreamdError> { let settings = Settings::load()?; @@ -69,7 +44,7 @@ pub fn run(date: Option) -> Result<(), StreamdError> { .unwrap() .with_timezone(&Utc); - let all_shards = load_all_shards(base_folder, tz)?; + let all_shards = load_markdown_shards(base_folder, &RepositoryConfiguration::new(), tz)?; let mut daily_shards: Vec<_> = all_shards .into_iter() .filter(|s| { @@ -77,8 +52,9 @@ pub fn run(date: Option) -> Result<(), StreamdError> { .get("file_type") .map(|v| v == "daily") .unwrap_or(false) + && s.moment >= day_start + && s.moment < day_end }) - .filter(|s| s.moment >= day_start && s.moment < day_end) .collect(); daily_shards.sort_by_key(|s| s.moment); diff --git a/src/cli/commands/edit.rs b/src/cli/commands/edit.rs index a8761f3..cfc28c1 100644 --- a/src/cli/commands/edit.rs +++ b/src/cli/commands/edit.rs @@ -1,42 +1,19 @@ -use std::fs; +use std::path::Path; use std::process::Command; -use walkdir::WalkDir; - use crate::config::Settings; use crate::error::StreamdError; -use crate::extract::parse_markdown_file; -use crate::localize::{localize_stream_file, TaskConfiguration}; -use crate::models::LocalizedShard; +use crate::localize::TaskConfiguration; -fn all_files() -> Result, StreamdError> { - let settings = Settings::load()?; - let mut shards = Vec::new(); - - for entry in WalkDir::new(&settings.base_folder) - .max_depth(1) - .into_iter() - .filter_map(|e| e.ok()) - { - let path = entry.path(); - if path.extension().map(|e| e == "md").unwrap_or(false) { - let file_name = path.to_string_lossy().to_string(); - let content = fs::read_to_string(path)?; - let stream_file = parse_markdown_file(&file_name, &content); - - if let Ok(shard) = - localize_stream_file(&stream_file, &TaskConfiguration, chrono_tz::UTC) - { - shards.push(shard); - } - } - } - - Ok(shards) -} +use super::load_markdown_shards; pub fn run(number: i32) -> Result<(), StreamdError> { - let all_shards = all_files()?; + let settings = Settings::load()?; + let all_shards = load_markdown_shards( + Path::new(&settings.base_folder), + &TaskConfiguration, + chrono_tz::UTC, + )?; // Sort by moment (timestamp) let mut sorted_shards = all_shards; diff --git a/src/cli/commands/mod.rs b/src/cli/commands/mod.rs index fa6dbba..e49668b 100644 --- a/src/cli/commands/mod.rs +++ b/src/cli/commands/mod.rs @@ -1,6 +1,41 @@ +use std::fs; +use std::path::Path; + +use chrono_tz::Tz; +use walkdir::WalkDir; + +use crate::error::StreamdError; +use crate::extract::parse_markdown_file; +use crate::localize::localize_stream_file; +use crate::models::{LocalizedShard, RepositoryConfiguration}; + pub mod completions; pub mod daily; pub mod edit; pub mod new; pub mod timesheet; pub mod todo; + +pub fn load_markdown_shards( + base_folder: &Path, + config: &RepositoryConfiguration, + tz: Tz, +) -> Result, StreamdError> { + let mut shards = Vec::new(); + for entry in WalkDir::new(base_folder) + .max_depth(1) + .into_iter() + .filter_map(|e| e.ok()) + { + let path = entry.path(); + if path.extension().map(|e| e == "md").unwrap_or(false) { + let file_name = path.to_string_lossy().to_string(); + let content = fs::read_to_string(path)?; + let stream_file = parse_markdown_file(&file_name, &content); + if let Ok(shard) = localize_stream_file(&stream_file, config, tz) { + shards.push(shard); + } + } + } + Ok(shards) +} diff --git a/src/cli/commands/timesheet.rs b/src/cli/commands/timesheet.rs index a1c4a37..1b422c9 100644 --- a/src/cli/commands/timesheet.rs +++ b/src/cli/commands/timesheet.rs @@ -1,49 +1,23 @@ use std::collections::HashMap; -use std::fs; use std::path::Path; use chrono::Datelike; use chrono::NaiveDate; use chrono::Utc; use chrono_tz::Tz; -use walkdir::WalkDir; use crate::config::Settings; const SEPARATOR_WIDTH: usize = 71; const COLUMN_SEPARATOR_WIDTH: usize = 65; use crate::error::StreamdError; -use crate::extract::parse_markdown_file; -use crate::localize::localize_stream_file; -use crate::models::{LocalizedShard, Timesheet}; +use crate::models::Timesheet; use crate::timesheet::{ extract_timesheets, generate_report, load_repository_config, BasicTimesheetConfiguration, DayType, DayWarning, MonthReport, TimesheetReport, }; -fn load_all_shards(base_folder: &Path, tz: Tz) -> Result, StreamdError> { - let mut shards = Vec::new(); - - for entry in WalkDir::new(base_folder) - .max_depth(1) - .into_iter() - .filter_map(|e| e.ok()) - { - let path = entry.path(); - if path.extension().map(|e| e == "md").unwrap_or(false) { - let file_name = path.to_string_lossy().to_string(); - let content = fs::read_to_string(path)?; - let stream_file = parse_markdown_file(&file_name, &content); - - if let Ok(shard) = localize_stream_file(&stream_file, &BasicTimesheetConfiguration, tz) - { - shards.push(shard); - } - } - } - - Ok(shards) -} +use super::load_markdown_shards; enum DisplayMode { Minutes, @@ -350,7 +324,7 @@ pub fn run(decimal: bool, debug: bool) -> Result<(), StreamdError> { let now = Utc::now(); // Load all markdown files and extract timesheets - let all_shards = load_all_shards(base_folder, tz)?; + let all_shards = load_markdown_shards(base_folder, &BasicTimesheetConfiguration, tz)?; let timesheets = extract_timesheets(&all_shards, now, tz)?; // Generate the report diff --git a/src/cli/commands/todo.rs b/src/cli/commands/todo.rs index 17bfba2..9f25289 100644 --- a/src/cli/commands/todo.rs +++ b/src/cli/commands/todo.rs @@ -1,44 +1,24 @@ use std::fs; +use std::path::Path; use std::process::Command; use chrono::Utc; -use walkdir::WalkDir; use crate::config::Settings; use crate::error::StreamdError; -use crate::extract::parse_markdown_file; -use crate::localize::{localize_stream_file, TaskConfiguration}; +use crate::localize::TaskConfiguration; use crate::models::LocalizedShard; use crate::query::find_shard_by_position; -fn all_files() -> Result, StreamdError> { - let settings = Settings::load()?; - let mut shards = Vec::new(); - - for entry in WalkDir::new(&settings.base_folder) - .max_depth(1) - .into_iter() - .filter_map(|e| e.ok()) - { - let path = entry.path(); - if path.extension().map(|e| e == "md").unwrap_or(false) { - let file_name = path.to_string_lossy().to_string(); - let content = fs::read_to_string(path)?; - let stream_file = parse_markdown_file(&file_name, &content); - - if let Ok(shard) = - localize_stream_file(&stream_file, &TaskConfiguration, chrono_tz::UTC) - { - shards.push(shard); - } - } - } - - Ok(shards) -} +use super::load_markdown_shards; pub fn collect_open_tasks(show_future: bool) -> Result, StreamdError> { - let all_shards = all_files()?; + let settings = Settings::load()?; + let all_shards = load_markdown_shards( + Path::new(&settings.base_folder), + &TaskConfiguration, + chrono_tz::UTC, + )?; let now = Utc::now(); let mut tasks: Vec = find_shard_by_position(&all_shards, "task", "open")