Skip to content

Commit 582d54d

Browse files
committed
🚧 Working out a few ideas
1 parent a4295a0 commit 582d54d

File tree

7 files changed

+129
-2
lines changed

7 files changed

+129
-2
lines changed

src/modules/logs/log_dir.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ pub enum LogDirError {
2323
}
2424

2525
impl LogDir {
26-
pub fn new(dir_path: PathBuf) -> LogDir {
27-
LogDir { dir_path }
26+
pub fn new(dir_path: impl Into<PathBuf>) -> LogDir {
27+
LogDir {
28+
dir_path: dir_path.into(),
29+
}
2830
}
2931

3032
pub fn path(&self) -> &Path {

src/modules/logs/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,4 @@ pub mod blocking;
283283
#[cfg(feature = "asynchronous")]
284284
#[cfg_attr(docsrs, doc(cfg(feature = "asynchronous")))]
285285
pub mod asynchronous;
286+
mod models;

src/modules/logs/models.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod log_dir;
2+
mod log_file;

src/modules/logs/models/log_dir.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use std::borrow::Cow;
2+
use std::path::{Path, PathBuf};
3+
4+
mod iter;
5+
mod try_iter;
6+
7+
pub use iter::Iter;
8+
use crate::logs::models::log_dir::try_iter::TryIter;
9+
10+
pub struct LogDir<'a> {
11+
path: Cow<'a, Path>,
12+
}
13+
14+
impl<'a> LogDir<'a> {
15+
pub fn new(path: &'a Path) -> Self {
16+
LogDir { path: Cow::Borrowed(path) }
17+
}
18+
19+
pub fn iter(&'a self) -> Iter<'a> {
20+
Iter { inner: self }
21+
}
22+
23+
pub fn try_iter(&'a self) -> TryIter<'a> {
24+
TryIter { inner: self }
25+
}
26+
}
27+
28+
#[cfg(test)]
29+
mod tests {
30+
use crate::logs::models::log_dir::LogDir;
31+
32+
#[test]
33+
fn api_design() {
34+
let log_dir = LogDir::new("./path".as_ref());
35+
36+
log_dir.iter();
37+
}
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::logs::models::log_dir::LogDir;
2+
use crate::logs::models::log_file::LogFile;
3+
4+
pub struct Iter<'a> {
5+
pub(crate) inner: &'a LogDir<'a>,
6+
}
7+
8+
impl<'a> Iterator for Iter<'a> {
9+
type Item = LogFile<'a>;
10+
11+
fn next(&mut self) -> Option<Self::Item> {
12+
todo!()
13+
}
14+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::logs::models::log_file::{LogFile, LogFileError};
2+
use crate::logs::models::log_dir::LogDir;
3+
4+
pub struct TryIter<'a> {
5+
pub(crate) inner: &'a LogDir<'a>,
6+
}
7+
8+
impl<'a> TryIter<'a> {
9+
pub fn new(inner: &'a LogDir) -> Self {
10+
TryIter { inner }
11+
}
12+
}
13+
14+
impl<'a> Iterator for TryIter<'a> {
15+
type Item = Result<LogFile<'a>, LogFileError>;
16+
17+
fn next(&mut self) -> Option<Self::Item> {
18+
let dir = self.inner
19+
.path
20+
.read_dir();
21+
22+
let value = match dir {
23+
Ok(value) => value,
24+
Err(error) => return Some(Err(error.into())),
25+
};
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use std::borrow::Cow;
2+
use std::fs::DirEntry;
3+
use std::path::Path;
4+
use lazy_static::lazy_static;
5+
use regex::Regex;
6+
use thiserror::Error;
7+
8+
pub struct LogFile<'a> {
9+
path: Cow<'a, Path>,
10+
}
11+
12+
#[derive(Debug, Error)]
13+
#[error(transparent)]
14+
pub enum LogFileError {
15+
IO(#[from] std::io::Error),
16+
}
17+
18+
#[cfg(not(feature = "legacy"))]
19+
type RegexList = [(Regex, &'static str); 1];
20+
21+
#[cfg(feature = "legacy")]
22+
type RegexList = [(Regex, &'static str); 2];
23+
24+
lazy_static! {
25+
static ref FILE_NAME_REGEXES: RegexList = [
26+
// Journal.YYYY-MM-DDTHHmmss.01.log
27+
(Regex::new(r"Journal\.(\d{4}-\d{2}-\d{2}T\d+)\.(\d{2})\.log").unwrap(), "%Y-%m-%dT%H%M%S"),
28+
29+
// Journal.YYMMDDHHMMSS.01.log
30+
#[cfg(feature = "legacy")]
31+
(Regex::new(r"Journal\.(\d{12})\.(\d{2})\.log").unwrap(), "%y%m%d%H%M%S"),
32+
];
33+
}
34+
35+
impl<'a> TryFrom<DirEntry> for LogFile<'a> {
36+
type Error = LogFileError;
37+
38+
fn try_from(value: DirEntry) -> Result<Self, Self::Error> {
39+
let file_name = value.file_name();
40+
41+
// let path = value.path();
42+
}
43+
}

0 commit comments

Comments
 (0)