Iterate over commands in all files

This commit is contained in:
Joscha 2021-11-22 15:22:06 +00:00
parent 6f49c32e79
commit bac673ea65

View file

@ -5,7 +5,7 @@ use std::{fs, io, result};
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use tzfile::Tz; use tzfile::Tz;
use self::commands::File; use self::commands::{Command, File};
pub mod commands; pub mod commands;
mod format; mod format;
@ -23,6 +23,18 @@ impl LoadedFile {
} }
} }
#[derive(Debug)]
pub struct Source<'a> {
file: &'a Path,
index: usize,
}
#[derive(Debug)]
pub struct SourcedCommand<'a> {
pub source: Source<'a>,
pub command: &'a Command,
}
#[derive(Debug)] #[derive(Debug)]
pub struct Files { pub struct Files {
files: HashMap<PathBuf, LoadedFile>, files: HashMap<PathBuf, LoadedFile>,
@ -124,6 +136,17 @@ impl Files {
} }
} }
pub fn commands(&self) -> Vec<SourcedCommand<'_>> {
let mut result = vec![];
for (path, file) in &self.files {
for (index, command) in file.file.commands.iter().enumerate() {
let source = Source { file: path, index };
result.push(SourcedCommand { source, command });
}
}
result
}
pub fn now(&self) -> DateTime<&Tz> { pub fn now(&self) -> DateTime<&Tz> {
Utc::now().with_timezone(&&self.timezone) Utc::now().with_timezone(&&self.timezone)
} }