mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2024-11-10 14:44:22 +00:00
Clippy and misc cleanup
Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
parent
0ec56ca015
commit
ae81ff21e9
12 changed files with 239 additions and 180 deletions
|
@ -2,7 +2,7 @@ use crate::Result;
|
|||
use ogg::PacketWriteEndInfo;
|
||||
use std::io::{Cursor, Read, Seek, SeekFrom};
|
||||
|
||||
pub(crate) fn ogg<T>(data: T, packet: Vec<u8>) -> Result<Cursor<Vec<u8>>>
|
||||
pub(crate) fn ogg<T>(data: T, packet: &[u8]) -> Result<Cursor<Vec<u8>>>
|
||||
where
|
||||
T: Read + Seek,
|
||||
{
|
||||
|
@ -28,12 +28,9 @@ where
|
|||
if !replaced {
|
||||
let comment_header = lewton::header::read_header_comment(&p.data);
|
||||
|
||||
match comment_header {
|
||||
Ok(_) => {
|
||||
p.data = packet.clone();
|
||||
replaced = true;
|
||||
},
|
||||
Err(_) => {},
|
||||
if comment_header.is_ok() {
|
||||
p.data = packet.to_vec();
|
||||
replaced = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -43,17 +43,34 @@ impl<'a> From<AnyTag<'a>> for Id3v2Tag {
|
|||
fn from(inp: AnyTag<'a>) -> Self {
|
||||
let mut tag = Id3v2Tag::new();
|
||||
|
||||
inp.title().map(|v| tag.set_title(v));
|
||||
inp.artists_as_string().map(|v| tag.set_artist(v.as_str()));
|
||||
inp.year.map(|v| tag.set_year(v as u16));
|
||||
inp.album().title.map(|v| tag.set_album_title(v));
|
||||
inp.album()
|
||||
.artists
|
||||
.map(|v| tag.set_album_artists(v.join(", ")));
|
||||
inp.track_number().map(|v| tag.set_track(v as u16));
|
||||
inp.total_tracks().map(|v| tag.set_total_tracks(v as u16));
|
||||
inp.disc_number().map(|v| tag.set_disc(v as u16));
|
||||
inp.total_discs().map(|v| tag.set_total_discs(v as u16));
|
||||
if let Some(v) = inp.title() {
|
||||
tag.set_title(v)
|
||||
}
|
||||
if let Some(v) = inp.artists_as_string() {
|
||||
tag.set_artist(v.as_str())
|
||||
}
|
||||
if let Some(v) = inp.year {
|
||||
tag.set_year(v)
|
||||
}
|
||||
if let Some(v) = inp.album().title {
|
||||
tag.set_album_title(v)
|
||||
}
|
||||
if let Some(v) = inp.album().artists {
|
||||
tag.set_album_artists(v.join(", "))
|
||||
}
|
||||
if let Some(v) = inp.track_number() {
|
||||
tag.set_track(v)
|
||||
}
|
||||
if let Some(v) = inp.total_tracks() {
|
||||
tag.set_total_tracks(v)
|
||||
}
|
||||
if let Some(v) = inp.disc_number() {
|
||||
tag.set_disc(v)
|
||||
}
|
||||
if let Some(v) = inp.total_discs() {
|
||||
tag.set_total_discs(v)
|
||||
}
|
||||
|
||||
tag
|
||||
}
|
||||
}
|
||||
|
@ -111,10 +128,10 @@ impl AudioTagEdit for Id3v2Tag {
|
|||
self.0.remove_artist()
|
||||
}
|
||||
|
||||
fn year(&self) -> Option<u16> {
|
||||
self.0.year().map(|y| y as u16)
|
||||
fn year(&self) -> Option<i32> {
|
||||
self.0.year()
|
||||
}
|
||||
fn set_year(&mut self, year: u16) {
|
||||
fn set_year(&mut self, year: i32) {
|
||||
self.0.set_year(year as i32)
|
||||
}
|
||||
fn remove_year(&mut self) {
|
||||
|
@ -139,7 +156,7 @@ impl AudioTagEdit for Id3v2Tag {
|
|||
self.0.set_album_artist(artists)
|
||||
}
|
||||
|
||||
fn add_album_artist(&mut self, artist: &str) {
|
||||
fn add_album_artist(&mut self, _artist: &str) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
@ -150,8 +167,7 @@ impl AudioTagEdit for Id3v2Tag {
|
|||
fn album_cover(&self) -> Option<Picture> {
|
||||
self.0
|
||||
.pictures()
|
||||
.filter(|&pic| matches!(pic.picture_type, id3::frame::PictureType::CoverFront))
|
||||
.next()
|
||||
.find(|&pic| matches!(pic.picture_type, id3::frame::PictureType::CoverFront))
|
||||
.and_then(|pic| {
|
||||
Some(Picture {
|
||||
data: &pic.data,
|
||||
|
@ -173,41 +189,41 @@ impl AudioTagEdit for Id3v2Tag {
|
|||
.remove_picture_by_type(id3::frame::PictureType::CoverFront);
|
||||
}
|
||||
|
||||
fn track_number(&self) -> Option<u16> {
|
||||
self.0.track().map(|x| x as u16)
|
||||
fn track_number(&self) -> Option<u32> {
|
||||
self.0.track()
|
||||
}
|
||||
fn set_track_number(&mut self, track: u16) {
|
||||
self.0.set_track(track as u32);
|
||||
fn set_track_number(&mut self, track: u32) {
|
||||
self.0.set_track(track);
|
||||
}
|
||||
fn remove_track_number(&mut self) {
|
||||
self.0.remove_track();
|
||||
}
|
||||
|
||||
fn total_tracks(&self) -> Option<u16> {
|
||||
self.0.total_tracks().map(|x| x as u16)
|
||||
fn total_tracks(&self) -> Option<u32> {
|
||||
self.0.total_tracks()
|
||||
}
|
||||
fn set_total_tracks(&mut self, total_track: u16) {
|
||||
fn set_total_tracks(&mut self, total_track: u32) {
|
||||
self.0.set_total_tracks(total_track as u32);
|
||||
}
|
||||
fn remove_total_tracks(&mut self) {
|
||||
self.0.remove_total_tracks();
|
||||
}
|
||||
|
||||
fn disc_number(&self) -> Option<u16> {
|
||||
self.0.disc().map(|x| x as u16)
|
||||
fn disc_number(&self) -> Option<u32> {
|
||||
self.0.disc()
|
||||
}
|
||||
fn set_disc_number(&mut self, disc_number: u16) {
|
||||
fn set_disc_number(&mut self, disc_number: u32) {
|
||||
self.0.set_disc(disc_number as u32)
|
||||
}
|
||||
fn remove_disc_number(&mut self) {
|
||||
self.0.remove_disc();
|
||||
}
|
||||
|
||||
fn total_discs(&self) -> Option<u16> {
|
||||
self.0.total_discs().map(|x| x as u16)
|
||||
fn total_discs(&self) -> Option<u32> {
|
||||
self.0.total_discs()
|
||||
}
|
||||
fn set_total_discs(&mut self, total_discs: u16) {
|
||||
self.0.set_total_discs(total_discs as u32)
|
||||
fn set_total_discs(&mut self, total_discs: u32) {
|
||||
self.0.set_total_discs(total_discs)
|
||||
}
|
||||
fn remove_total_discs(&mut self) {
|
||||
self.0.remove_total_discs();
|
||||
|
|
|
@ -50,18 +50,33 @@ impl<'a> From<AnyTag<'a>> for Mp4Tag {
|
|||
fn from(inp: AnyTag<'a>) -> Self {
|
||||
let mut tag = Mp4Tag::new();
|
||||
|
||||
inp.title().map(|v| tag.set_title(v));
|
||||
inp.artists()
|
||||
.map(|i| i.iter().for_each(|&a| tag.add_artist(a)));
|
||||
inp.year.map(|v| tag.set_year(v as u16));
|
||||
inp.album().title.map(|v| tag.set_album_title(v));
|
||||
inp.album()
|
||||
.artists
|
||||
.map(|i| i.iter().for_each(|&a| tag.add_album_artist(a)));
|
||||
inp.track_number().map(|v| tag.set_track_number(v));
|
||||
inp.total_tracks().map(|v| tag.set_total_tracks(v));
|
||||
inp.disc_number().map(|v| tag.set_disc_number(v));
|
||||
inp.total_discs().map(|v| tag.set_total_discs(v));
|
||||
if let Some(v) = inp.title() {
|
||||
tag.set_title(v)
|
||||
}
|
||||
if let Some(i) = inp.artists() {
|
||||
i.iter().for_each(|&a| tag.add_artist(a))
|
||||
}
|
||||
if let Some(v) = inp.year {
|
||||
tag.set_year(v)
|
||||
}
|
||||
if let Some(v) = inp.album().title {
|
||||
tag.set_album_title(v)
|
||||
}
|
||||
if let Some(i) = inp.album().artists {
|
||||
i.iter().for_each(|&a| tag.add_album_artist(a))
|
||||
}
|
||||
if let Some(v) = inp.track_number() {
|
||||
tag.set_track_number(v)
|
||||
}
|
||||
if let Some(v) = inp.total_tracks() {
|
||||
tag.set_total_tracks(v)
|
||||
}
|
||||
if let Some(v) = inp.disc_number() {
|
||||
tag.set_disc_number(v)
|
||||
}
|
||||
if let Some(v) = inp.total_discs() {
|
||||
tag.set_total_discs(v)
|
||||
}
|
||||
tag
|
||||
}
|
||||
}
|
||||
|
@ -110,20 +125,20 @@ impl AudioTagEdit for Mp4Tag {
|
|||
v.push(a);
|
||||
v
|
||||
});
|
||||
if v.len() > 0 {
|
||||
Some(v)
|
||||
} else {
|
||||
if v.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(v)
|
||||
}
|
||||
}
|
||||
fn remove_artist(&mut self) {
|
||||
self.0.remove_artists();
|
||||
}
|
||||
|
||||
fn year(&self) -> Option<u16> {
|
||||
fn year(&self) -> Option<i32> {
|
||||
self.0.year().and_then(|x| str::parse(x).ok())
|
||||
}
|
||||
fn set_year(&mut self, year: u16) {
|
||||
fn set_year(&mut self, year: i32) {
|
||||
self.0.set_year(year.to_string())
|
||||
}
|
||||
|
||||
|
@ -164,7 +179,8 @@ impl AudioTagEdit for Mp4Tag {
|
|||
self.0.remove_album_artists();
|
||||
}
|
||||
fn album_cover(&self) -> Option<Picture> {
|
||||
use mp4ameta::Data::*;
|
||||
use mp4ameta::Data::{Jpeg, Png};
|
||||
|
||||
self.0.artwork().and_then(|data| match data {
|
||||
Jpeg(d) => Some(Picture {
|
||||
data: d,
|
||||
|
@ -192,21 +208,21 @@ impl AudioTagEdit for Mp4Tag {
|
|||
fn remove_track(&mut self) {
|
||||
self.0.remove_track(); // faster than removing separately
|
||||
}
|
||||
fn track_number(&self) -> Option<u16> {
|
||||
self.0.track_number()
|
||||
fn track_number(&self) -> Option<u32> {
|
||||
self.0.track_number().map(u32::from)
|
||||
}
|
||||
|
||||
fn set_track_number(&mut self, track: u16) {
|
||||
self.0.set_track_number(track);
|
||||
fn set_track_number(&mut self, track: u32) {
|
||||
self.0.set_track_number(track as u16);
|
||||
}
|
||||
fn remove_track_number(&mut self) {
|
||||
self.0.remove_track_number();
|
||||
}
|
||||
fn total_tracks(&self) -> Option<u16> {
|
||||
self.0.total_tracks()
|
||||
fn total_tracks(&self) -> Option<u32> {
|
||||
self.0.total_tracks().map(u32::from)
|
||||
}
|
||||
fn set_total_tracks(&mut self, total_track: u16) {
|
||||
self.0.set_total_tracks(total_track);
|
||||
fn set_total_tracks(&mut self, total_track: u32) {
|
||||
self.0.set_total_tracks(total_track as u16);
|
||||
}
|
||||
fn remove_total_tracks(&mut self) {
|
||||
self.0.remove_total_tracks();
|
||||
|
@ -214,20 +230,20 @@ impl AudioTagEdit for Mp4Tag {
|
|||
fn remove_disc(&mut self) {
|
||||
self.0.remove_disc();
|
||||
}
|
||||
fn disc_number(&self) -> Option<u16> {
|
||||
self.0.disc_number()
|
||||
fn disc_number(&self) -> Option<u32> {
|
||||
self.0.disc_number().map(u32::from)
|
||||
}
|
||||
fn set_disc_number(&mut self, disc_number: u16) {
|
||||
self.0.set_disc_number(disc_number)
|
||||
fn set_disc_number(&mut self, disc_number: u32) {
|
||||
self.0.set_disc_number(disc_number as u16)
|
||||
}
|
||||
fn remove_disc_number(&mut self) {
|
||||
self.0.remove_disc_number();
|
||||
}
|
||||
fn total_discs(&self) -> Option<u16> {
|
||||
self.0.total_discs()
|
||||
fn total_discs(&self) -> Option<u32> {
|
||||
self.0.total_discs().map(u32::from)
|
||||
}
|
||||
fn set_total_discs(&mut self, total_discs: u16) {
|
||||
self.0.set_total_discs(total_discs)
|
||||
fn set_total_discs(&mut self, total_discs: u32) {
|
||||
self.0.set_total_discs(total_discs as u16)
|
||||
}
|
||||
fn remove_total_discs(&mut self) {
|
||||
self.0.remove_total_discs();
|
||||
|
|
|
@ -4,11 +4,15 @@ use crate::{
|
|||
components::logic, impl_tag, Album, AnyTag, AudioTag, AudioTagEdit, AudioTagWrite, Picture,
|
||||
Result, TagType, ToAny, ToAnyTag,
|
||||
};
|
||||
use metaflac::Tag;
|
||||
use std::borrow::BorrowMut;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::{Cursor, Seek, SeekFrom};
|
||||
use std::{collections::HashMap, fs::File, io::Write, path::Path};
|
||||
use std::{
|
||||
borrow::BorrowMut,
|
||||
collections::HashMap,
|
||||
fs::File,
|
||||
fs::OpenOptions,
|
||||
io::Write,
|
||||
io::{Cursor, Seek, SeekFrom},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
const START_SIGNATURE: [u8; 7] = [3, 118, 111, 114, 98, 105, 115];
|
||||
const END_BYTE: u8 = 1;
|
||||
|
@ -24,22 +28,20 @@ impl Default for VorbisInnerTag {
|
|||
Self {
|
||||
tag_type: None,
|
||||
vendor: "".to_string(),
|
||||
comments: Default::default(),
|
||||
comments: std::collections::HashMap::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl VorbisInnerTag {
|
||||
fn get_value(&self, key: &str) -> Option<&str> {
|
||||
if let Some(pair) = self.comments.get_key_value(key) {
|
||||
if !pair.1.is_empty() {
|
||||
Some(pair.1.as_str())
|
||||
} else {
|
||||
self.comments.get_key_value(key).and_then(|pair| {
|
||||
if pair.1.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(pair.1.as_str())
|
||||
}
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
fn set_value<V>(&mut self, key: &str, val: V)
|
||||
|
@ -117,34 +119,53 @@ impl_tag!(VorbisTag, VorbisInnerTag, TagType::Ogg);
|
|||
|
||||
impl<'a> From<AnyTag<'a>> for VorbisTag {
|
||||
fn from(inp: AnyTag<'a>) -> Self {
|
||||
let mut t = VorbisTag::default();
|
||||
inp.title().map(|v| t.set_title(v));
|
||||
inp.artists_as_string().map(|v| t.set_artist(&v));
|
||||
inp.year.map(|v| t.set_year(v as u16));
|
||||
inp.album().title.map(|v| t.set_album_title(v));
|
||||
inp.album()
|
||||
.artists
|
||||
.map(|v| t.set_album_artists(v.join(", ")));
|
||||
inp.track_number().map(|v| t.set_track_number(v));
|
||||
inp.total_tracks().map(|v| t.set_total_tracks(v));
|
||||
inp.disc_number().map(|v| t.set_disc_number(v));
|
||||
inp.total_discs().map(|v| t.set_total_discs(v));
|
||||
t
|
||||
let mut tag = VorbisTag::default();
|
||||
|
||||
if let Some(v) = inp.title() {
|
||||
tag.set_title(v)
|
||||
}
|
||||
if let Some(v) = inp.artists_as_string() {
|
||||
tag.set_artist(&v)
|
||||
}
|
||||
if let Some(v) = inp.year {
|
||||
tag.set_year(v)
|
||||
}
|
||||
if let Some(v) = inp.album().title {
|
||||
tag.set_album_title(v)
|
||||
}
|
||||
if let Some(v) = inp.album().artists {
|
||||
tag.set_album_artists(v.join(", "))
|
||||
}
|
||||
if let Some(v) = inp.track_number() {
|
||||
tag.set_track_number(v)
|
||||
}
|
||||
if let Some(v) = inp.total_tracks() {
|
||||
tag.set_total_tracks(v)
|
||||
}
|
||||
if let Some(v) = inp.disc_number() {
|
||||
tag.set_disc_number(v)
|
||||
}
|
||||
if let Some(v) = inp.total_discs() {
|
||||
tag.set_total_discs(v)
|
||||
}
|
||||
|
||||
tag
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a VorbisTag> for AnyTag<'a> {
|
||||
fn from(inp: &'a VorbisTag) -> Self {
|
||||
let mut t = Self::default();
|
||||
t.title = inp.title();
|
||||
t.artists = inp.artists();
|
||||
t.year = inp.year().map(|y| y as i32);
|
||||
t.album = Album::new(inp.album_title(), inp.album_artists(), inp.album_cover());
|
||||
t.track_number = inp.track_number();
|
||||
t.total_tracks = inp.total_tracks();
|
||||
t.disc_number = inp.disc_number();
|
||||
t.total_discs = inp.total_discs();
|
||||
t
|
||||
Self {
|
||||
title: inp.title(),
|
||||
artists: inp.artists(),
|
||||
year: inp.year().map(|y| y as i32),
|
||||
album: Album::new(inp.album_title(), inp.album_artists(), inp.album_cover()),
|
||||
track_number: inp.track_number(),
|
||||
total_tracks: inp.total_tracks(),
|
||||
disc_number: inp.disc_number(),
|
||||
total_discs: inp.total_discs(),
|
||||
..AnyTag::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -193,7 +214,7 @@ impl From<&VorbisTag> for metaflac::Tag {
|
|||
let vendor = inp.0.vendor.clone();
|
||||
let mut comment_collection: HashMap<String, Vec<String>> = HashMap::new();
|
||||
|
||||
for (k, v) in inp.0.comments.clone().into_iter() {
|
||||
for (k, v) in inp.0.comments.clone() {
|
||||
comment_collection.insert(k, vec![v]);
|
||||
}
|
||||
|
||||
|
@ -227,7 +248,7 @@ impl AudioTagEdit for VorbisTag {
|
|||
self.0.set_value("ARTIST", artist)
|
||||
}
|
||||
|
||||
fn add_artist(&mut self, artist: &str) {
|
||||
fn add_artist(&mut self, _artist: &str) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
@ -239,20 +260,20 @@ impl AudioTagEdit for VorbisTag {
|
|||
self.0.remove_key("ARTIST");
|
||||
}
|
||||
|
||||
fn year(&self) -> Option<u16> {
|
||||
fn year(&self) -> Option<i32> {
|
||||
if let Some(Ok(y)) = self
|
||||
.0
|
||||
.get_value("DATE")
|
||||
.map(|s| s.chars().take(4).collect::<String>().parse::<i32>())
|
||||
{
|
||||
Some(y as u16)
|
||||
} else if let Some(Ok(y)) = self.0.get_value("YEAR").map(|s| s.parse::<i32>()) {
|
||||
Some(y as u16)
|
||||
Some(y)
|
||||
} else if let Some(Ok(y)) = self.0.get_value("YEAR").map(str::parse::<i32>) {
|
||||
Some(y)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn set_year(&mut self, year: u16) {
|
||||
fn set_year(&mut self, year: i32) {
|
||||
self.0.set_value("DATE", &year.to_string());
|
||||
self.0.set_value("YEAR", &year.to_string());
|
||||
}
|
||||
|
@ -278,7 +299,7 @@ impl AudioTagEdit for VorbisTag {
|
|||
self.0.set_value("ALBUMARTIST", artists)
|
||||
}
|
||||
|
||||
fn add_album_artist(&mut self, artist: &str) {
|
||||
fn add_album_artist(&mut self, _artist: &str) {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
@ -300,7 +321,7 @@ impl AudioTagEdit for VorbisTag {
|
|||
// })
|
||||
None
|
||||
}
|
||||
fn set_album_cover(&mut self, cover: Picture) {
|
||||
fn set_album_cover(&mut self, _cover: Picture) {
|
||||
// TODO
|
||||
// self.remove_album_cover();
|
||||
// let mime = String::from(cover.mime_type);
|
||||
|
@ -314,14 +335,14 @@ impl AudioTagEdit for VorbisTag {
|
|||
// .remove_picture_type(metaflac::block::PictureType::CoverFront)
|
||||
}
|
||||
|
||||
fn track_number(&self) -> Option<u16> {
|
||||
if let Some(Ok(n)) = self.0.get_value("TRACKNUMBER").map(|x| x.parse::<u16>()) {
|
||||
fn track_number(&self) -> Option<u32> {
|
||||
if let Some(Ok(n)) = self.0.get_value("TRACKNUMBER").map(str::parse::<u32>) {
|
||||
Some(n)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn set_track_number(&mut self, v: u16) {
|
||||
fn set_track_number(&mut self, v: u32) {
|
||||
self.0.set_value("TRACKNUMBER", &v.to_string())
|
||||
}
|
||||
fn remove_track_number(&mut self) {
|
||||
|
@ -329,28 +350,28 @@ impl AudioTagEdit for VorbisTag {
|
|||
}
|
||||
|
||||
// ! not standard
|
||||
fn total_tracks(&self) -> Option<u16> {
|
||||
if let Some(Ok(n)) = self.0.get_value("TOTALTRACKS").map(|x| x.parse::<u16>()) {
|
||||
fn total_tracks(&self) -> Option<u32> {
|
||||
if let Some(Ok(n)) = self.0.get_value("TOTALTRACKS").map(str::parse::<u32>) {
|
||||
Some(n)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn set_total_tracks(&mut self, v: u16) {
|
||||
fn set_total_tracks(&mut self, v: u32) {
|
||||
self.0.set_value("TOTALTRACKS", &v.to_string())
|
||||
}
|
||||
fn remove_total_tracks(&mut self) {
|
||||
self.0.remove_key("TOTALTRACKS");
|
||||
}
|
||||
|
||||
fn disc_number(&self) -> Option<u16> {
|
||||
if let Some(Ok(n)) = self.0.get_value("DISCNUMBER").map(|x| x.parse::<u16>()) {
|
||||
fn disc_number(&self) -> Option<u32> {
|
||||
if let Some(Ok(n)) = self.0.get_value("DISCNUMBER").map(str::parse::<u32>) {
|
||||
Some(n)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn set_disc_number(&mut self, v: u16) {
|
||||
fn set_disc_number(&mut self, v: u32) {
|
||||
self.0.set_value("DISCNUMBER", &v.to_string())
|
||||
}
|
||||
fn remove_disc_number(&mut self) {
|
||||
|
@ -358,14 +379,14 @@ impl AudioTagEdit for VorbisTag {
|
|||
}
|
||||
|
||||
// ! not standard
|
||||
fn total_discs(&self) -> Option<u16> {
|
||||
if let Some(Ok(n)) = self.0.get_value("TOTALDISCS").map(|x| x.parse::<u16>()) {
|
||||
fn total_discs(&self) -> Option<u32> {
|
||||
if let Some(Ok(n)) = self.0.get_value("TOTALDISCS").map(str::parse::<u32>) {
|
||||
Some(n)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
fn set_total_discs(&mut self, v: u16) {
|
||||
fn set_total_discs(&mut self, v: u32) {
|
||||
self.0.set_value("TOTALDISCS", &v.to_string())
|
||||
}
|
||||
fn remove_total_discs(&mut self) {
|
||||
|
@ -402,7 +423,7 @@ impl AudioTagWrite for VorbisTag {
|
|||
|
||||
let mut comment_str = Vec::new();
|
||||
|
||||
for (a, b) in comments.into_iter() {
|
||||
for (a, b) in comments {
|
||||
comment_str.push(format!("{}={}", a, b));
|
||||
let last = comment_str.last().unwrap();
|
||||
let len = last.as_bytes().len() as u32;
|
||||
|
@ -415,8 +436,7 @@ impl AudioTagWrite for VorbisTag {
|
|||
let mut file_bytes = Vec::new();
|
||||
std::io::copy(file.borrow_mut(), &mut file_bytes)?;
|
||||
|
||||
let data =
|
||||
logic::write::ogg(Cursor::new(file_bytes.clone()), packet)?.into_inner();
|
||||
let data = logic::write::ogg(Cursor::new(file_bytes), &*packet)?.into_inner();
|
||||
|
||||
file.seek(SeekFrom::Start(0))?;
|
||||
file.set_len(0)?;
|
||||
|
@ -426,7 +446,7 @@ impl AudioTagWrite for VorbisTag {
|
|||
TagType::Flac => {
|
||||
let mut flac_tag: metaflac::Tag = self.into();
|
||||
|
||||
flac_tag.write_to(file);
|
||||
flac_tag.write_to(file)?;
|
||||
},
|
||||
TagType::Mp4 => {},
|
||||
_ => unreachable!(),
|
||||
|
|
16
src/error.rs
16
src/error.rs
|
@ -3,7 +3,7 @@
|
|||
pub enum Error {
|
||||
/// Unknown file extension.
|
||||
#[error("Failed to guess the metadata format based on the file extension.")]
|
||||
UnknownFileExtension(String),
|
||||
UnknownFileExtension,
|
||||
|
||||
/// Unsupported file extension
|
||||
#[error("Unsupported format: {0}")]
|
||||
|
@ -12,24 +12,24 @@ pub enum Error {
|
|||
UnsupportedMimeType(String),
|
||||
|
||||
#[error(transparent)]
|
||||
FlacTagError(#[from] metaflac::Error),
|
||||
FlacTag(#[from] metaflac::Error),
|
||||
#[error(transparent)]
|
||||
Id3TagError(#[from] id3::Error),
|
||||
Id3Tag(#[from] id3::Error),
|
||||
#[error(transparent)]
|
||||
Mp4TagError(#[from] mp4ameta::Error),
|
||||
Mp4Tag(#[from] mp4ameta::Error),
|
||||
#[error(transparent)]
|
||||
OpusTagError(#[from] opus_headers::ParseError),
|
||||
OpusTag(#[from] opus_headers::ParseError),
|
||||
#[error(transparent)]
|
||||
LewtonError(#[from] lewton::VorbisError),
|
||||
Lewton(#[from] lewton::VorbisError),
|
||||
#[error(transparent)]
|
||||
OggError(#[from] ogg::OggReadError),
|
||||
Ogg(#[from] ogg::OggReadError),
|
||||
|
||||
#[error("")]
|
||||
NotAPicture,
|
||||
|
||||
/// Represents all cases of `std::io::Error`.
|
||||
#[error(transparent)]
|
||||
IOError(#[from] std::io::Error),
|
||||
IO(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
/// Type alias for the result of tag operations.
|
||||
|
|
|
@ -45,9 +45,9 @@
|
|||
//!
|
||||
//! By default, `full` (`all_tags` and `duration`) are enabled.
|
||||
//!
|
||||
//! `all_tags` provides all the track metadata (`artists`, `album`, etc.) in [AnyTag].
|
||||
//! `all_tags` provides all the track metadata (`artists`, `album`, etc.) in [`AnyTag`].
|
||||
//!
|
||||
//! `duration` provides the `duration` field in [AnyTag].
|
||||
//! `duration` provides the `duration` field in [`AnyTag`].
|
||||
//!
|
||||
//! Either one can be disabled if it doesn't fit your use case.
|
||||
//!
|
||||
|
@ -75,7 +75,10 @@
|
|||
clippy::cast_sign_loss,
|
||||
clippy::cast_possible_wrap,
|
||||
clippy::clippy::cast_possible_truncation,
|
||||
clippy::module_name_repetitions
|
||||
clippy::module_name_repetitions,
|
||||
clippy::must_use_candidate,
|
||||
clippy::doc_markdown,
|
||||
clippy::let_underscore_drop
|
||||
)]
|
||||
|
||||
#[doc(hidden)]
|
||||
|
|
22
src/tag.rs
22
src/tag.rs
|
@ -1,3 +1,4 @@
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use super::{components::tags::*, AudioTag, Error, Result};
|
||||
use std::path::Path;
|
||||
|
||||
|
@ -10,28 +11,37 @@ impl Tag {
|
|||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// This function can be used to specify a `TagType`, so there's no guessing
|
||||
#[allow(clippy::unused_self)]
|
||||
pub fn with_tag_type(self, tag_type: TagType) -> Self {
|
||||
Self(Some(tag_type))
|
||||
}
|
||||
|
||||
/// Path of the file to read
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// This function will return `Err` if `path` either has no extension, or the extension is
|
||||
/// not valid unicode
|
||||
pub fn read_from_path(&self, path: impl AsRef<Path>) -> Result<Box<dyn AudioTag>> {
|
||||
let extension = path.as_ref().extension().unwrap().to_str().unwrap();
|
||||
let extension = path
|
||||
.as_ref()
|
||||
.extension()
|
||||
.ok_or(Error::UnknownFileExtension)?;
|
||||
let extension_str = extension.to_str().ok_or(Error::UnknownFileExtension)?;
|
||||
|
||||
match self
|
||||
.0
|
||||
.as_ref()
|
||||
.unwrap_or(&TagType::try_from_ext(extension)?)
|
||||
.unwrap_or(&TagType::try_from_ext(extension_str)?)
|
||||
{
|
||||
#[cfg(feature = "mp3")]
|
||||
TagType::Id3v2 => Ok(Box::new(Id3v2Tag::read_from_path(path, None)?)),
|
||||
#[cfg(feature = "mp4")]
|
||||
TagType::Mp4 => Ok(Box::new(Mp4Tag::read_from_path(path, None)?)),
|
||||
#[cfg(feature = "vorbis")]
|
||||
id @ _ => Ok(Box::new(VorbisTag::read_from_path(
|
||||
path,
|
||||
Some(id.to_owned()),
|
||||
)?)),
|
||||
id => Ok(Box::new(VorbisTag::read_from_path(path, Some(id.clone()))?)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#[allow(clippy::wildcard_imports)]
|
||||
use crate::{components::tags::*, Album, AnyTag, Picture, Result, TagType};
|
||||
use std::fs::File;
|
||||
|
||||
|
@ -23,8 +24,8 @@ pub trait AudioTagEdit {
|
|||
fn artists(&self) -> Option<Vec<&str>>;
|
||||
fn remove_artist(&mut self);
|
||||
|
||||
fn year(&self) -> Option<u16>;
|
||||
fn set_year(&mut self, year: u16);
|
||||
fn year(&self) -> Option<i32>;
|
||||
fn set_year(&mut self, year: i32);
|
||||
fn remove_year(&mut self);
|
||||
|
||||
fn album(&self) -> Album<'_> {
|
||||
|
@ -48,10 +49,10 @@ pub trait AudioTagEdit {
|
|||
fn set_album_cover(&mut self, cover: Picture);
|
||||
fn remove_album_cover(&mut self);
|
||||
|
||||
fn track(&self) -> (Option<u16>, Option<u16>) {
|
||||
fn track(&self) -> (Option<u32>, Option<u32>) {
|
||||
(self.track_number(), self.total_tracks())
|
||||
}
|
||||
fn set_track(&mut self, track: u16) {
|
||||
fn set_track(&mut self, track: u32) {
|
||||
self.set_track_number(track);
|
||||
}
|
||||
fn remove_track(&mut self) {
|
||||
|
@ -59,18 +60,18 @@ pub trait AudioTagEdit {
|
|||
self.remove_total_tracks();
|
||||
}
|
||||
|
||||
fn track_number(&self) -> Option<u16>;
|
||||
fn set_track_number(&mut self, track_number: u16);
|
||||
fn track_number(&self) -> Option<u32>;
|
||||
fn set_track_number(&mut self, track_number: u32);
|
||||
fn remove_track_number(&mut self);
|
||||
|
||||
fn total_tracks(&self) -> Option<u16>;
|
||||
fn set_total_tracks(&mut self, total_track: u16);
|
||||
fn total_tracks(&self) -> Option<u32>;
|
||||
fn set_total_tracks(&mut self, total_track: u32);
|
||||
fn remove_total_tracks(&mut self);
|
||||
|
||||
fn disc(&self) -> (Option<u16>, Option<u16>) {
|
||||
fn disc(&self) -> (Option<u32>, Option<u32>) {
|
||||
(self.disc_number(), self.total_discs())
|
||||
}
|
||||
fn set_disc(&mut self, disc: u16) {
|
||||
fn set_disc(&mut self, disc: u32) {
|
||||
self.set_disc_number(disc);
|
||||
}
|
||||
fn remove_disc(&mut self) {
|
||||
|
@ -78,12 +79,12 @@ pub trait AudioTagEdit {
|
|||
self.remove_total_discs();
|
||||
}
|
||||
|
||||
fn disc_number(&self) -> Option<u16>;
|
||||
fn set_disc_number(&mut self, disc_number: u16);
|
||||
fn disc_number(&self) -> Option<u32>;
|
||||
fn set_disc_number(&mut self, disc_number: u32);
|
||||
fn remove_disc_number(&mut self);
|
||||
|
||||
fn total_discs(&self) -> Option<u16>;
|
||||
fn set_total_discs(&mut self, total_discs: u16);
|
||||
fn total_discs(&self) -> Option<u32>;
|
||||
fn set_total_discs(&mut self, total_discs: u32);
|
||||
fn remove_total_discs(&mut self);
|
||||
}
|
||||
|
||||
|
@ -104,11 +105,7 @@ pub trait ToAnyTag: ToAny {
|
|||
#[cfg(feature = "mp3")]
|
||||
TagType::Id3v2 => Box::new(Id3v2Tag::from(self.to_anytag())),
|
||||
#[cfg(feature = "vorbis")]
|
||||
TagType::Ogg => Box::new(VorbisTag::from(self.to_anytag())),
|
||||
#[cfg(feature = "vorbis")]
|
||||
TagType::Opus => Box::new(VorbisTag::from(self.to_anytag())),
|
||||
#[cfg(feature = "vorbis")]
|
||||
TagType::Flac => Box::new(VorbisTag::from(self.to_anytag())),
|
||||
TagType::Ogg | TagType::Opus | TagType::Flac => Box::new(VorbisTag::from(self.to_anytag())),
|
||||
#[cfg(feature = "mp4")]
|
||||
TagType::Mp4 => Box::new(Mp4Tag::from(self.to_anytag())),
|
||||
}
|
||||
|
|
|
@ -9,10 +9,10 @@ pub struct AnyTag<'a> {
|
|||
pub comments: Option<Vec<&'a str>>,
|
||||
pub year: Option<i32>,
|
||||
pub date: Option<&'a str>,
|
||||
pub track_number: Option<u16>,
|
||||
pub total_tracks: Option<u16>,
|
||||
pub disc_number: Option<u16>,
|
||||
pub total_discs: Option<u16>,
|
||||
pub track_number: Option<u32>,
|
||||
pub total_tracks: Option<u32>,
|
||||
pub disc_number: Option<u32>,
|
||||
pub total_discs: Option<u32>,
|
||||
#[cfg(feature = "duration")]
|
||||
pub duration_ms: Option<u32>,
|
||||
}
|
||||
|
@ -58,19 +58,19 @@ impl<'a> AnyTag<'a> {
|
|||
self.year = Some(year);
|
||||
}
|
||||
/// Returns `track number`
|
||||
pub fn track_number(&self) -> Option<u16> {
|
||||
pub fn track_number(&self) -> Option<u32> {
|
||||
self.track_number
|
||||
}
|
||||
/// Returns `total_tracks`
|
||||
pub fn total_tracks(&self) -> Option<u16> {
|
||||
pub fn total_tracks(&self) -> Option<u32> {
|
||||
self.total_tracks
|
||||
}
|
||||
/// Returns `disc_number`
|
||||
pub fn disc_number(&self) -> Option<u16> {
|
||||
self.track_number
|
||||
pub fn disc_number(&self) -> Option<u32> {
|
||||
self.disc_number
|
||||
}
|
||||
/// Returns `total_discs`
|
||||
pub fn total_discs(&self) -> Option<u16> {
|
||||
pub fn total_discs(&self) -> Option<u32> {
|
||||
self.total_tracks
|
||||
}
|
||||
#[cfg(feature = "duration")]
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -15,7 +15,7 @@ fn test_inner() {
|
|||
let tag: VorbisTag = innertag.into();
|
||||
|
||||
// Turn the VorbisTag into a Box<dyn AudioTag>
|
||||
let mut id3tag = tag.to_dyn_tag(TagType::Id3v2);
|
||||
let id3tag = tag.to_dyn_tag(TagType::Id3v2);
|
||||
|
||||
// Write Box<dyn AudioTag> to `a.mp3`
|
||||
id3tag
|
||||
|
@ -42,7 +42,7 @@ fn test_inner() {
|
|||
minute: None,
|
||||
second: None,
|
||||
};
|
||||
id3tag_inner.set_date_recorded(timestamp.clone());
|
||||
id3tag_inner.set_date_recorded(timestamp);
|
||||
|
||||
// Write id3::Tag to `a.mp3`
|
||||
id3tag_inner
|
||||
|
|
Loading…
Reference in a new issue