Compare commits
7 Commits
dev-v0.1.7
...
main-v0.1.
Author | SHA1 | Date | |
---|---|---|---|
fac33c7987 | |||
7f5f546f65 | |||
05537a4f6d | |||
64402a0626 | |||
a84d59a5f5 | |||
d1f63a04e4 | |||
842fe6302e |
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "icarus-models"
|
||||
version = "0.1.7"
|
||||
version = "0.1.10"
|
||||
edition = "2024"
|
||||
description = "models used for the icarus project"
|
||||
license = "MIT"
|
||||
|
125
src/song.rs
125
src/song.rs
@@ -90,15 +90,134 @@ impl Default for Song {
|
||||
}
|
||||
|
||||
impl Song {
|
||||
// TODO: Implement
|
||||
pub fn to_metadata_json(&self) -> Result<String, serde_json::Error> {
|
||||
return serde_json::to_string_pretty(&self);
|
||||
pub fn to_metadata_json(&self, pretty: bool) -> Result<String, serde_json::Error> {
|
||||
if pretty {
|
||||
return serde_json::to_string_pretty(&self);
|
||||
} else {
|
||||
return serde_json::to_string(&self);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn song_path(&self) -> Result<String, std::io::Error> {
|
||||
if self.directory.is_empty() {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"Directory does not exist",
|
||||
));
|
||||
}
|
||||
|
||||
let directory = &self.directory;
|
||||
let mut buffer: String = String::from(directory.clone());
|
||||
let lastIndex = directory.len() - 1;
|
||||
|
||||
if let Some(character) = directory.chars().nth(lastIndex) {
|
||||
if character != '/' {
|
||||
buffer += "/";
|
||||
}
|
||||
|
||||
buffer += &self.filename.clone();
|
||||
|
||||
return Ok(buffer);
|
||||
} else {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"Could not access last character of directory",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_data(&self) -> Result<Vec<u8>, std::io::Error> {
|
||||
let pathResult = self.song_path();
|
||||
|
||||
match pathResult {
|
||||
Ok(path) => {
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
file.read_to_end(&mut buffer)?;
|
||||
|
||||
if buffer.len() == 0 {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"File is empty",
|
||||
));
|
||||
} else {
|
||||
return Ok(buffer);
|
||||
}
|
||||
}
|
||||
Err(er) => {
|
||||
return Err(er);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod embedded {
|
||||
use std::io::Read;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
impl Song {
|
||||
pub fn to_metadata_json(&self, pretty: bool) -> Result<String, serde_json::Error> {
|
||||
if pretty {
|
||||
return serde_json::to_string_pretty(&self);
|
||||
} else {
|
||||
return serde_json::to_string(&self);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn song_path(&self) -> Result<String, std::io::Error> {
|
||||
if self.directory.is_empty() {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"Directory does not exist",
|
||||
));
|
||||
}
|
||||
|
||||
let directory = &self.directory;
|
||||
let mut buffer: String = String::from(directory.clone());
|
||||
let lastIndex = directory.len() - 1;
|
||||
|
||||
if let Some(character) = directory.chars().nth(lastIndex) {
|
||||
if character != '/' {
|
||||
buffer += "/";
|
||||
}
|
||||
|
||||
buffer += &self.filename.clone();
|
||||
|
||||
return Ok(buffer);
|
||||
} else {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"Could not access last character of directory",
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_data(&self) -> Result<Vec<u8>, std::io::Error> {
|
||||
let pathResult = self.song_path();
|
||||
|
||||
match pathResult {
|
||||
Ok(path) => {
|
||||
let mut file = std::fs::File::open(path)?;
|
||||
let mut buffer: Vec<u8> = Vec::new();
|
||||
file.read_to_end(&mut buffer)?;
|
||||
|
||||
if buffer.len() == 0 {
|
||||
return Err(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"File is empty",
|
||||
));
|
||||
} else {
|
||||
return Ok(buffer);
|
||||
}
|
||||
}
|
||||
Err(er) => {
|
||||
return Err(er);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The song's duration is a floating point in seconds
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub struct Song {
|
||||
|
Reference in New Issue
Block a user