Skip to content

Commit

Permalink
feat: updated the path parser (#135)
Browse files Browse the repository at this point in the history
* feat: updated the path parser

* fix: test

* feat: update the time
  • Loading branch information
hitohata authored Nov 23, 2024
1 parent be2ceec commit ed3d744
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: bun run build

test-build-top-page-doc:
timeout-minutes: 1
timeout-minutes: 2
name: build a top page
runs-on: ubuntu-latest
defaults:
Expand Down Expand Up @@ -126,4 +126,4 @@ jobs:
- test-build-web-api-doc
runs-on: ubuntu-latest
steps:
- uses: Kesin11/actions-timeline@v2
- uses: Kesin11/actions-timeline@v2
40 changes: 35 additions & 5 deletions crates/time_file_name/src/file_datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct PathDateTime {
pub hour: u32,
pub minute: u32,
pub second: u32,
pub file_name: String,
pub unix_time: i64,
pub iso_string: String,
}
Expand All @@ -30,12 +31,14 @@ impl PathDateTime {
/// assert_eq!(date_time.day, 4);
/// assert_eq!(date_time.hour, 12);
/// assert_eq!(date_time.minute, 34);
/// assert_eq!(date_time.second, 50);
/// assert_eq!(date_time.second, 50 );
/// assert_eq!(date_time.file_name, "1984-4-4-12-34-50.video".to_string());
/// assert_eq!(date_time.unix_time, Utc.with_ymd_and_hms(1984, 4, 4, 12, 34, 50).unwrap().timestamp_millis());
/// # }
/// ```
pub fn parse(file_path: &str) -> Result<Self, String> {
let date_time = from_file_name_to_date_time(file_path)?;
let file_name = retrieve_file_name(file_path)?;

Ok(Self {
year: date_time.year(),
Expand All @@ -44,6 +47,7 @@ impl PathDateTime {
hour: date_time.hour(),
minute: date_time.minute(),
second: date_time.second(),
file_name,
unix_time: date_time.timestamp_millis(),
iso_string: date_time.to_rfc3339().to_string(),
})
Expand All @@ -52,10 +56,7 @@ impl PathDateTime {

/// Convert from the file path to the DateTime of Chrono
fn from_file_name_to_date_time(path: &str) -> Result<DateTime<Utc>, String> {
let file_path = match &path[..1] == "/" {
true => path[1..].to_owned(),
false => path.to_owned(),
};
let file_path = remove_slash(path);

let vec_path = file_path.split("/").collect::<Vec<&str>>();

Expand Down Expand Up @@ -104,6 +105,28 @@ fn from_file_name_to_date_time(path: &str) -> Result<DateTime<Utc>, String> {
}
}

/// retrieve a file name form the file path
fn retrieve_file_name(path: &str) -> Result<String, String> {
let file_path = remove_slash(path);

let vec_path = file_path.split("/").collect::<Vec<&str>>();

// it will be four elements, year, month, day, file name
if vec_path.len() != 4 {
return Err("invalid file path is provided".to_string());
};

Ok(vec_path[3].to_string())
}

/// remove the slash
fn remove_slash(path: &str) -> String {
match &path[..1] == "/" {
true => path[1..].to_owned(),
false => path.to_owned(),
}
}

#[cfg(test)]
mod test_from_file_name_to_date_time {
use super::*;
Expand Down Expand Up @@ -178,4 +201,11 @@ mod test_from_file_name_to_date_time {
// Assert
assert!(result.is_err());
}

#[test]
fn test_retrieve_file_name() {
let path = "1984/04/04/1984-4-4-12-34-56.video";
let object_name = retrieve_file_name(path).unwrap();
assert_eq!(object_name, "1984-4-4-12-34-56.video".to_string());
}
}

0 comments on commit ed3d744

Please sign in to comment.