Cargo formatted and made query_uri() public

This commit is contained in:
MrDulfin 2024-01-09 13:38:54 -05:00
parent 72da619f8e
commit c78f77165e
3 changed files with 53 additions and 27 deletions

View file

@ -1,10 +1,10 @@
use std::collections::BTreeMap;
use std::{fs::File, io::Read, path::Path, time::Duration};
use super::utils::meta_offset;
use crate::music_storage::db_reader::common::{get_bytes, get_bytes_vec};
use crate::music_storage::db_reader::extern_library::ExternalLibrary;
use crate::music_storage::library::{Song, URI};
use super::utils::meta_offset;
const MAGIC: [u8; 16] = [
0xE1, 0xA0, 0x9C, 0x91, 0xF8, 0x3C, 0x77, 0x42, 0x85, 0x2C, 0x3B, 0xCC, 0x14, 0x01, 0xD3, 0xF2,
@ -87,7 +87,10 @@ impl ExternalLibrary for FoobarPlaylist {
for _ in 0..primary_count {
println!("{}", i32::from_le_bytes(get_bytes(&mut buf_iter)));
let key = meta_offset(metadata, i32::from_le_bytes(get_bytes(&mut buf_iter)) as usize);
let key = meta_offset(
metadata,
i32::from_le_bytes(get_bytes(&mut buf_iter)) as usize,
);
entries.push((key, String::new()));
}
@ -100,15 +103,24 @@ impl ExternalLibrary for FoobarPlaylist {
for i in 0..primary_count {
println!("primkey {i}");
let value = meta_offset(metadata, i32::from_le_bytes(get_bytes(&mut buf_iter)) as usize);
let value = meta_offset(
metadata,
i32::from_le_bytes(get_bytes(&mut buf_iter)) as usize,
);
entries[i as usize].1 = value;
}
// Get secondary Keys
for _ in 0..secondary_count {
let key = meta_offset(metadata, i32::from_le_bytes(get_bytes(&mut buf_iter)) as usize);
let value = meta_offset(metadata, i32::from_le_bytes(get_bytes(&mut buf_iter)) as usize);
let key = meta_offset(
metadata,
i32::from_le_bytes(get_bytes(&mut buf_iter)) as usize,
);
let value = meta_offset(
metadata,
i32::from_le_bytes(get_bytes(&mut buf_iter)) as usize,
);
entries.push((key, value));
}

View file

@ -368,7 +368,7 @@ impl MusicLibrary {
/// Queries for a [Song] by its [URI], returning a single `Song`
/// with the `URI` that matches
fn query_uri(&self, path: &URI) -> Option<(&Song, usize)> {
pub fn query_uri(&self, path: &URI) -> Option<(&Song, usize)> {
let result = self
.library
.par_iter()

View file

@ -1,21 +1,20 @@
use chrono::Duration;
use walkdir::Error;
use crate::music_controller::config::Config;
use super::{
db_reader::extern_library::ExternalLibrary,
library::{self, AlbumArt, Song, Tag},
music_collection::MusicCollection,
db_reader::extern_library::ExternalLibrary
};
use crate::music_storage::db_reader::xml::reader::{XmlLibrary};
use crate::music_controller::config::Config;
use crate::music_storage::db_reader::xml::reader::XmlLibrary;
use std::{default, path::Path, path::PathBuf, thread::AccessError};
use std::io::Read;
use std::{default, path::Path, path::PathBuf, thread::AccessError};
use m3u8_rs::{MediaPlaylist, MediaPlaylistType, MediaSegment};
// use nom::IResult;
#[derive(Debug, Clone)]
pub struct Playlist<'a> {
title: String,
@ -74,15 +73,26 @@ impl<'a> Playlist<'a> {
false
}
pub fn to_m3u8(&mut self) {
let seg = &self.tracks.iter().map({
|track|
MediaSegment {
let seg = &self
.tracks
.iter()
.map({
|track| MediaSegment {
uri: track.location.to_string().into(),
duration: track.duration.as_millis() as f32,
title: Some(track.tags.get_key_value(&Tag::Title).unwrap().1.to_string().into()),
title: Some(
track
.tags
.get_key_value(&Tag::Title)
.unwrap()
.1
.to_string()
.into(),
),
..Default::default()
}
}).collect::<Vec<MediaSegment>>();
})
.collect::<Vec<MediaSegment>>();
let m3u8 = MediaPlaylist {
version: Some(6),
@ -94,7 +104,12 @@ impl<'a> Playlist<'a> {
segments: seg.clone(),
..Default::default()
};
let mut file = std::fs::OpenOptions::new().read(true).create(true).write(true).open("F:\\Dango Music Player\\playlist.m3u8").unwrap();
let mut file = std::fs::OpenOptions::new()
.read(true)
.create(true)
.write(true)
.open("F:\\Dango Music Player\\playlist.m3u8")
.unwrap();
m3u8.write_to(&mut file).unwrap();
}
pub fn from_file(file: std::fs::File) -> Playlist<'a> {
@ -129,13 +144,12 @@ impl Default for Playlist<'_> {
#[test]
fn list_to_m3u8() {
let lib = XmlLibrary::from_file(Path::new("F:\\Music\\Mp3\\Music Main\\iTunes Music Library.xml"));
let lib = XmlLibrary::from_file(Path::new(
"F:\\Music\\Mp3\\Music Main\\iTunes Music Library.xml",
));
let mut a = Playlist::new();
let c = lib.to_songs();
let mut b = c.iter().map({
|song|
song
}).collect::<Vec<&Song>>();
let mut b = c.iter().map({ |song| song }).collect::<Vec<&Song>>();
a.tracks.append(&mut b);
a.to_m3u8()
}