Ibis/src/util.rs
2023-05-24 11:11:12 +02:00

45 lines
1.3 KiB
Rust

use anyhow::Result;
use chrono::{DateTime, Datelike, Local};
use std::path::{Path, PathBuf};
use std::time::SystemTime;
pub fn to_snail_case(from: impl Into<String>) -> String {
from.into().replace(' ', "_").to_lowercase()
}
// pub fn to_title_case(from: &str) -> String {
// from.to_string().split(|c| c ==' ' || c == '_').map(|s| {if s.len() >= 1 {s[0 as usize] = s[0 as usize].to_uppercase()}}).collect()
// }
pub fn get_current_semester() -> String {
let today = DateTime::<Local>::from(SystemTime::now())
.date()
.naive_local();
let (part, year) = if today.month() >= 4 && today.month() < 10 {
("SS", today.year())
} else {
(
"WS",
if today.month() >= 10 {
today.year()
} else {
today.year() - 1
},
)
};
format!("{}{}", part, year - 2000)
}
pub fn glob_paths<P: AsRef<Path>, S: AsRef<str>>(path: P, pattern: S) -> Result<Vec<PathBuf>> {
let mut paths = vec![];
let pattern = [
(*(path.as_ref())).canonicalize()?.to_str().unwrap(),
pattern.as_ref(),
]
.concat();
for entry in glob::glob(pattern.as_str())? {
let path = entry?;
paths.push(path);
}
Ok(paths)
}