pile-audio refactor
Some checks failed
CI / Typos (push) Successful in 17s
CI / Clippy (push) Has been cancelled
CI / Build and test (push) Has been cancelled

This commit is contained in:
2026-02-21 19:14:52 -08:00
parent 5aab61bd1b
commit 4b41467cc2
35 changed files with 1968 additions and 3366 deletions

View File

@@ -1,11 +1,6 @@
use std::{
fmt::Debug,
fs::File,
io::{Read, Seek},
path::PathBuf,
};
use std::{fmt::Debug, fs::File, io::BufReader, path::PathBuf};
use pile_audio::flac::blockread::{FlacBlock, FlacBlockReader, FlacBlockSelector};
use pile_audio::{FlacBlock, FlacReader};
use pile_config::Label;
use serde_json::{Map, Value};
@@ -36,56 +31,34 @@ impl Item for FlacItem {
}
fn json(&self) -> Result<serde_json::Value, std::io::Error> {
let mut block_reader = FlacBlockReader::new(FlacBlockSelector {
pick_vorbiscomment: true,
..Default::default()
});
let mut file = File::open(&self.path)?;
// TODO: do not read the whole file
file.rewind()?;
let mut data = Vec::new();
file.read_to_end(&mut data)?;
block_reader
.push_data(&data)
.map_err(std::io::Error::other)?;
block_reader.finish().map_err(std::io::Error::other)?;
//
// Return tags
//
let file = File::open(&self.path)?;
let reader = FlacReader::new(BufReader::new(file));
let mut output = Map::new();
while let Some(block) = block_reader.pop_block() {
match block {
FlacBlock::VorbisComment(comment) => {
for (k, v) in comment.comment.comments {
let k = k.to_string();
let v = Value::String(v.into());
let e = output.get_mut(&k);
for block in reader {
if let FlacBlock::VorbisComment(comment) = block.unwrap() {
for (k, v) in comment.comment.comments {
let k = k.to_string();
let v = Value::String(v.into());
let e = output.get_mut(&k);
match e {
None => {
output.insert(k.clone(), Value::Array(vec![v]));
}
Some(e) => {
// We always insert an array
#[expect(clippy::unwrap_used)]
e.as_array_mut().unwrap().push(v);
}
match e {
None => {
output.insert(k.clone(), Value::Array(vec![v]));
}
Some(e) => {
// We always insert an array
#[expect(clippy::unwrap_used)]
e.as_array_mut().unwrap().push(v);
}
}
}
// `reader` filters blocks for us
_ => unreachable!(),
// We should only have one comment block,
// stop reading when we find it
break;
}
// We should only have one comment block
assert!(!block_reader.has_block());
}
return Ok(serde_json::Value::Object(output));