risp/src/misc.rs
philipp 9803748953
All checks were successful
CI/CD Pipeline / test (push) Successful in 1m55s
add license
2024-02-15 13:50:58 +01:00

113 lines
3.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright (C) 2024 Philipp Hofer
//
// Licensed under the EUPL, Version 1.2 or as soon they will be approved by
// the European Commission - subsequent versions of the EUPL (the "Licence").
// You may not use this work except in compliance with the Licence.
//
// You should have received a copy of the European Union Public License along
// with this program. If not, you may obtain a copy of the Licence at:
// <https://joinup.ec.europa.eu/software/page/eupl>
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Licence is distributed on an "AS IS" basis,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the Licence for the specific language governing permissions and
// limitations under the Licence.
use std::{fs, io, path::Path};
use time::{format_description, OffsetDateTime};
#[derive(Debug)]
#[allow(dead_code)]
pub struct Error {
pub(crate) msg: String,
}
impl Error {
pub fn new(msg: &str) -> Self {
Self { msg: msg.into() }
}
}
impl From<ureq::Error> for Error {
fn from(value: ureq::Error) -> Self {
Self {
msg: value.to_string(),
}
}
}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
Self {
msg: value.to_string(),
}
}
}
impl From<serde_json::Error> for Error {
fn from(value: serde_json::Error) -> Self {
Self {
msg: value.to_string(),
}
}
}
impl From<toml::de::Error> for Error {
fn from(value: toml::de::Error) -> Self {
Self {
msg: value.to_string(),
}
}
}
impl From<roxmltree::Error> for Error {
fn from(value: roxmltree::Error) -> Self {
Self {
msg: value.to_string(),
}
}
}
/// Returns the current date in YYYY-MM-DD format. Needed for RIS API query to get current version of the overview.
pub(crate) fn current_date() -> String {
let local_date = OffsetDateTime::now_utc();
let format = format_description::parse("[year]-[month]-[day]").unwrap(); //unwrap okay, supplied format is fine
local_date.format(&format).expect("Failed to format date")
}
#[cfg(not(test))]
pub(crate) fn get_cache_dir() -> Result<String, Error> {
let cache_dir = directories::BaseDirs::new().ok_or(Error {
msg: "directories crate could not find basedirs.".into(),
})?;
let cache_dir = cache_dir.cache_dir();
Ok(format!("{}/risp/", cache_dir.to_str().unwrap()))
}
#[cfg(test)]
pub(crate) fn get_cache_dir() -> Result<String, Error> {
Ok("./data/cache/".into())
}
pub fn clear_cache() -> Result<(), Error> {
Ok(delete_all_in_dir(Path::new(&get_cache_dir()?))?)
}
fn delete_all_in_dir<P: AsRef<Path>>(dir_path: P) -> std::io::Result<()> {
let entries = fs::read_dir(dir_path)?;
for entry in entries {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
delete_all_in_dir(&path)?;
fs::remove_dir(&path)?;
} else {
// If it's a file, delete it
fs::remove_file(&path)?;
}
}
Ok(())
}