musicutil/src/types.rs

73 lines
1.9 KiB
Rust
Raw Normal View History

2022-08-16 09:05:18 +01:00
use std::path::PathBuf;
2022-10-22 15:15:37 +01:00
#[derive(Debug, Clone)]
2022-08-16 09:05:18 +01:00
pub struct Tags {
pub title: String,
pub artist: String,
2022-10-22 21:01:36 +01:00
pub supports_replaygain: bool,
pub contains_replaygain_tags: bool,
2022-08-16 09:05:18 +01:00
}
2022-10-22 15:15:37 +01:00
#[derive(Debug, Clone)]
2022-08-16 09:05:18 +01:00
pub struct File {
pub filename: String,
pub extension: String,
2022-10-22 21:01:36 +01:00
pub extra_files: Vec<File>,
2022-10-22 14:16:02 +01:00
pub path_to: PathBuf,
pub path_from_source: PathBuf,
2022-08-16 09:05:18 +01:00
pub tags: Tags,
}
impl File {
2022-10-22 14:16:02 +01:00
pub fn from_path(source_dir: String, full_path: PathBuf) -> File {
let full_file_path = PathBuf::from(&source_dir).join(full_path);
2022-08-16 09:05:18 +01:00
let filename = full_file_path
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_string();
let extension = full_file_path.extension();
let extension = if let Some(extension) = extension {
extension.to_str().unwrap().to_string()
} else {
"".to_string()
};
2022-08-16 09:05:18 +01:00
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);
2022-10-22 14:16:02 +01:00
File {
filename,
extension,
path_from_source: folder_path_from_src,
path_to,
2022-10-22 21:01:36 +01:00
extra_files: Vec::new(),
2022-08-16 09:05:18 +01:00
tags: Tags {
title: "".to_string(),
artist: "".to_string(),
2022-10-22 21:01:36 +01:00
supports_replaygain: false,
contains_replaygain_tags: false,
2022-08-16 09:05:18 +01:00
},
2022-10-22 14:16:02 +01:00
}
2022-08-16 09:05:18 +01:00
}
pub fn join_filename(&self) -> String {
2022-10-22 14:16:02 +01:00
format!("{}.{}", self.filename, self.extension)
2022-08-16 09:05:18 +01:00
}
2022-10-22 14:16:02 +01:00
pub fn join_path_to(&self) -> PathBuf {
PathBuf::from(&self.path_to).join(self.join_filename())
2022-08-16 09:05:18 +01:00
}
2022-10-22 14:16:02 +01:00
pub fn join_path_from_source(&self) -> PathBuf {
PathBuf::from(&self.path_from_source).join(self.join_filename())
2022-08-16 09:05:18 +01:00
}
}