musicutil/src/types.rs

73 lines
2 KiB
Rust
Raw Normal View History

2022-08-16 09:05:18 +01:00
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tags {
pub title: String,
pub artist: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct File {
pub filename: String,
pub extension: String,
pub path_to: String,
pub path_from_source: String,
pub tags: Tags,
}
impl File {
pub fn from_path(source_dir: String, full_path: String) -> File {
let full_file_path = PathBuf::from(&source_dir).join(PathBuf::from(full_path));
let filename = full_file_path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_string();
let extension = full_file_path
.extension()
.unwrap()
.to_str()
.unwrap()
.to_string();
let path_from_src = full_file_path.strip_prefix(&source_dir).unwrap();
let mut folder_path_from_src = path_from_src.to_path_buf();
folder_path_from_src.pop();
let path_to = PathBuf::from(&source_dir).join(&folder_path_from_src);
return File {
filename: filename,
extension: extension,
path_from_source: folder_path_from_src.into_os_string().into_string().unwrap(),
path_to: path_to.into_os_string().into_string().unwrap(),
tags: Tags {
title: "".to_string(),
artist: "".to_string(),
},
};
}
pub fn join_filename(&self) -> String {
return format!("{}.{}", self.filename, self.extension);
}
pub fn join_path_to(&self) -> String {
return PathBuf::from(&self.path_to)
.join(self.join_filename())
.into_os_string()
.into_string()
.unwrap();
}
pub fn join_path_from_source(&self) -> String {
return PathBuf::from(&self.path_from_source)
.join(self.join_filename())
.into_os_string()
.into_string()
.unwrap();
}
}