refactor: remove duplicate code
This commit is contained in:
parent
b8a73bfb3e
commit
ee96033639
5 changed files with 61 additions and 119 deletions
|
|
@ -4,38 +4,13 @@ use std::process::Command;
|
||||||
|
|
||||||
use chrono::{Days, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
|
use chrono::{Days, NaiveDate, NaiveDateTime, NaiveTime, TimeZone, Utc};
|
||||||
use chrono_tz::Tz;
|
use chrono_tz::Tz;
|
||||||
use walkdir::WalkDir;
|
|
||||||
|
|
||||||
use crate::config::Settings;
|
use crate::config::Settings;
|
||||||
use crate::error::StreamdError;
|
use crate::error::StreamdError;
|
||||||
use crate::extract::parse_markdown_file;
|
use crate::models::RepositoryConfiguration;
|
||||||
use crate::localize::localize_stream_file;
|
|
||||||
use crate::models::{LocalizedShard, RepositoryConfiguration};
|
|
||||||
use crate::timesheet::load_repository_config;
|
use crate::timesheet::load_repository_config;
|
||||||
|
|
||||||
fn load_all_shards(base_folder: &Path, tz: Tz) -> Result<Vec<LocalizedShard>, StreamdError> {
|
use super::load_markdown_shards;
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(date: Option<String>) -> Result<(), StreamdError> {
|
pub fn run(date: Option<String>) -> Result<(), StreamdError> {
|
||||||
let settings = Settings::load()?;
|
let settings = Settings::load()?;
|
||||||
|
|
@ -69,7 +44,7 @@ pub fn run(date: Option<String>) -> Result<(), StreamdError> {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.with_timezone(&Utc);
|
.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
|
let mut daily_shards: Vec<_> = all_shards
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|s| {
|
.filter(|s| {
|
||||||
|
|
@ -77,8 +52,9 @@ pub fn run(date: Option<String>) -> Result<(), StreamdError> {
|
||||||
.get("file_type")
|
.get("file_type")
|
||||||
.map(|v| v == "daily")
|
.map(|v| v == "daily")
|
||||||
.unwrap_or(false)
|
.unwrap_or(false)
|
||||||
|
&& s.moment >= day_start
|
||||||
|
&& s.moment < day_end
|
||||||
})
|
})
|
||||||
.filter(|s| s.moment >= day_start && s.moment < day_end)
|
|
||||||
.collect();
|
.collect();
|
||||||
daily_shards.sort_by_key(|s| s.moment);
|
daily_shards.sort_by_key(|s| s.moment);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,42 +1,19 @@
|
||||||
use std::fs;
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
use walkdir::WalkDir;
|
|
||||||
|
|
||||||
use crate::config::Settings;
|
use crate::config::Settings;
|
||||||
use crate::error::StreamdError;
|
use crate::error::StreamdError;
|
||||||
use crate::extract::parse_markdown_file;
|
use crate::localize::TaskConfiguration;
|
||||||
use crate::localize::{localize_stream_file, TaskConfiguration};
|
|
||||||
use crate::models::LocalizedShard;
|
|
||||||
|
|
||||||
fn all_files() -> Result<Vec<LocalizedShard>, StreamdError> {
|
use super::load_markdown_shards;
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn run(number: i32) -> Result<(), StreamdError> {
|
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)
|
// Sort by moment (timestamp)
|
||||||
let mut sorted_shards = all_shards;
|
let mut sorted_shards = all_shards;
|
||||||
|
|
|
||||||
|
|
@ -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 completions;
|
||||||
pub mod daily;
|
pub mod daily;
|
||||||
pub mod edit;
|
pub mod edit;
|
||||||
pub mod new;
|
pub mod new;
|
||||||
pub mod timesheet;
|
pub mod timesheet;
|
||||||
pub mod todo;
|
pub mod todo;
|
||||||
|
|
||||||
|
pub fn load_markdown_shards(
|
||||||
|
base_folder: &Path,
|
||||||
|
config: &RepositoryConfiguration,
|
||||||
|
tz: Tz,
|
||||||
|
) -> Result<Vec<LocalizedShard>, 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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,49 +1,23 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use chrono::Datelike;
|
use chrono::Datelike;
|
||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use chrono_tz::Tz;
|
use chrono_tz::Tz;
|
||||||
use walkdir::WalkDir;
|
|
||||||
|
|
||||||
use crate::config::Settings;
|
use crate::config::Settings;
|
||||||
|
|
||||||
const SEPARATOR_WIDTH: usize = 71;
|
const SEPARATOR_WIDTH: usize = 71;
|
||||||
const COLUMN_SEPARATOR_WIDTH: usize = 65;
|
const COLUMN_SEPARATOR_WIDTH: usize = 65;
|
||||||
use crate::error::StreamdError;
|
use crate::error::StreamdError;
|
||||||
use crate::extract::parse_markdown_file;
|
use crate::models::Timesheet;
|
||||||
use crate::localize::localize_stream_file;
|
|
||||||
use crate::models::{LocalizedShard, Timesheet};
|
|
||||||
use crate::timesheet::{
|
use crate::timesheet::{
|
||||||
extract_timesheets, generate_report, load_repository_config, BasicTimesheetConfiguration,
|
extract_timesheets, generate_report, load_repository_config, BasicTimesheetConfiguration,
|
||||||
DayType, DayWarning, MonthReport, TimesheetReport,
|
DayType, DayWarning, MonthReport, TimesheetReport,
|
||||||
};
|
};
|
||||||
|
|
||||||
fn load_all_shards(base_folder: &Path, tz: Tz) -> Result<Vec<LocalizedShard>, StreamdError> {
|
use super::load_markdown_shards;
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
enum DisplayMode {
|
enum DisplayMode {
|
||||||
Minutes,
|
Minutes,
|
||||||
|
|
@ -350,7 +324,7 @@ pub fn run(decimal: bool, debug: bool) -> Result<(), StreamdError> {
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
|
|
||||||
// Load all markdown files and extract timesheets
|
// 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)?;
|
let timesheets = extract_timesheets(&all_shards, now, tz)?;
|
||||||
|
|
||||||
// Generate the report
|
// Generate the report
|
||||||
|
|
|
||||||
|
|
@ -1,44 +1,24 @@
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use walkdir::WalkDir;
|
|
||||||
|
|
||||||
use crate::config::Settings;
|
use crate::config::Settings;
|
||||||
use crate::error::StreamdError;
|
use crate::error::StreamdError;
|
||||||
use crate::extract::parse_markdown_file;
|
use crate::localize::TaskConfiguration;
|
||||||
use crate::localize::{localize_stream_file, TaskConfiguration};
|
|
||||||
use crate::models::LocalizedShard;
|
use crate::models::LocalizedShard;
|
||||||
use crate::query::find_shard_by_position;
|
use crate::query::find_shard_by_position;
|
||||||
|
|
||||||
fn all_files() -> Result<Vec<LocalizedShard>, StreamdError> {
|
use super::load_markdown_shards;
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn collect_open_tasks(show_future: bool) -> Result<Vec<LocalizedShard>, StreamdError> {
|
pub fn collect_open_tasks(show_future: bool) -> Result<Vec<LocalizedShard>, 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 now = Utc::now();
|
||||||
|
|
||||||
let mut tasks: Vec<LocalizedShard> = find_shard_by_position(&all_shards, "task", "open")
|
let mut tasks: Vec<LocalizedShard> = find_shard_by_position(&all_shards, "task", "open")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue