Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- **MP4**: `AtomIdent` now implements `TryFrom<ItemKey>`

## [0.10.0] - 2022-12-27

### Added
Expand Down
45 changes: 45 additions & 0 deletions src/mp4/atom_info.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::error::{ErrorKind, LoftyError, Result};
use crate::macros::{err, try_vec};
use crate::tag::item::ItemKey;
use crate::tag::TagType;

use std::borrow::Cow;
use std::io::{Read, Seek, SeekFrom};
Expand Down Expand Up @@ -54,6 +56,49 @@ impl<'a> AtomIdent<'a> {
}
}

impl<'a> TryFrom<&'a ItemKey> for AtomIdent<'a> {
type Error = LoftyError;

fn try_from(value: &'a ItemKey) -> std::result::Result<Self, Self::Error> {
if let Some(mapped_key) = value.map_key(TagType::MP4ilst, true) {
if mapped_key.starts_with("----") {
let mut split = mapped_key.split(':');

split.next();

let mean = split.next();
let name = split.next();

if let (Some(mean), Some(name)) = (mean, name) {
return Ok(AtomIdent::Freeform {
mean: Cow::Borrowed(mean),
name: Cow::Borrowed(name),
});
}
} else {
let fourcc = mapped_key.chars().map(|c| c as u8).collect::<Vec<_>>();

if let Ok(fourcc) = TryInto::<[u8; 4]>::try_into(fourcc) {
return Ok(AtomIdent::Fourcc(fourcc));
}
}
}

err!(TextDecode(
"ItemKey does not map to a freeform or fourcc identifier"
))
}
}

impl TryFrom<ItemKey> for AtomIdent<'static> {
type Error = LoftyError;

fn try_from(value: ItemKey) -> std::result::Result<Self, Self::Error> {
let ret: AtomIdent<'_> = (&value).try_into()?;
Ok(ret.into_owned())
}
}

pub(crate) struct AtomInfo {
pub(crate) start: u64,
pub(crate) len: u64,
Expand Down
32 changes: 1 addition & 31 deletions src/mp4/ilst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ impl From<Tag> for Ilst {
for item in input.items {
let key = item.item_key;

if let Some(ident) = item_key_to_ident(&key) {
if let Ok(ident) = TryInto::<AtomIdent<'_>>::try_into(&key) {
let data = match item.item_value {
ItemValue::Text(text) => text,
_ => continue,
Expand Down Expand Up @@ -491,36 +491,6 @@ impl From<Tag> for Ilst {
}
}

fn item_key_to_ident(key: &ItemKey) -> Option<AtomIdent<'_>> {
key.map_key(TagType::MP4ilst, true).and_then(|ident| {
if ident.starts_with("----") {
let mut split = ident.split(':');

split.next();

let mean = split.next();
let name = split.next();

if let (Some(mean), Some(name)) = (mean, name) {
Some(AtomIdent::Freeform {
mean: Cow::Borrowed(mean),
name: Cow::Borrowed(name),
})
} else {
None
}
} else {
let fourcc = ident.chars().map(|c| c as u8).collect::<Vec<_>>();

if let Ok(fourcc) = TryInto::<[u8; 4]>::try_into(fourcc) {
Some(AtomIdent::Fourcc(fourcc))
} else {
None
}
}
})
}

#[cfg(test)]
mod tests {
use crate::mp4::ilst::atom::AtomDataStorage;
Expand Down