musicutil/src/types.rs

135 lines
3.2 KiB
Rust
Raw Normal View History

2022-08-16 09:05:18 +01:00
use std::path::PathBuf;
use crate::utils::formats::AudioContainer;
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,
}
impl Default for Tags {
fn default() -> Self {
Tags {
title: "".to_string(),
artist: "".to_string(),
}
}
}
#[derive(Debug, Clone)]
pub struct ReplayGainData {
pub track_gain: String,
pub track_peak: String,
}
impl ReplayGainData {
pub fn to_raw(&self) -> ReplayGainRawData {
let track_gain = self.track_gain.split(' ').next().unwrap_or("0.0");
let track_gain = track_gain.parse::<f64>().unwrap();
let track_peak = self.track_peak.parse::<f64>().unwrap();
ReplayGainRawData {
track_gain,
track_peak,
}
}
}
#[derive(Debug, Clone)]
pub struct ReplayGainRawData {
pub track_gain: f64,
pub track_peak: f64,
}
impl ReplayGainRawData {
pub fn to_normal(&self) -> ReplayGainData {
ReplayGainData {
track_gain: format!("{:.2} dB", self.track_gain),
track_peak: format!("{:.6}", self.track_peak),
}
}
}
2022-10-22 21:01:36 +01:00
#[derive(Debug, Clone)]
pub struct AudioFileInfo {
pub tags: Tags,
pub replaygain: Option<ReplayGainData>,
2022-10-22 21:01:36 +01:00
pub supports_replaygain: bool,
pub container: AudioContainer,
}
impl Default for AudioFileInfo {
fn default() -> Self {
AudioFileInfo {
tags: Tags::default(),
replaygain: None,
supports_replaygain: false,
container: AudioContainer::Unknown,
}
}
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,
// relative path to file's folder
2022-10-22 14:16:02 +01:00
pub path_to: PathBuf,
// path to folder from source
2022-10-22 14:16:02 +01:00
pub path_from_source: PathBuf,
pub extra_files: Vec<File>,
pub info: AudioFileInfo,
2022-08-16 09:05:18 +01:00
}
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(),
info: AudioFileInfo::default(),
2022-10-22 14:16:02 +01:00
}
2022-08-16 09:05:18 +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
}
}