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
4 changes: 4 additions & 0 deletions lofty/src/id3/v2/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,10 @@ pub(super) fn new_text_frame<'a>(id: FrameId<'a>, value: impl Into<Cow<'a, str>>
Frame::Text(TextInformationFrame::new(id, TextEncoding::UTF8, value))
}

pub(super) fn new_url_frame<'a>(id: FrameId<'a>, value: impl Into<Cow<'a, str>>) -> Frame<'a> {
Frame::Url(UrlLinkFrame::new(id, value))
}

pub(super) fn new_user_text_frame<'a>(
description: impl Into<Cow<'a, str>>,
content: impl Into<Cow<'a, str>>,
Expand Down
25 changes: 23 additions & 2 deletions lofty/src/id3/v2/tag/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use crate::tag::companion_tag::CompanionTag;
use crate::tag::{Tag, TagItem, TagType};

use super::V4_MULTI_VALUE_SEPARATOR;
use crate::id3::v2::tag::{new_text_frame, new_timestamp_frame, new_user_text_frame};
use crate::id3::v2::tag::{
new_text_frame, new_timestamp_frame, new_url_frame, new_user_text_frame,
};
use crate::id3::v2::util::mappings::TIPL_MAPPINGS;
use crate::mp4::AdvisoryRating;
use crate::tag::items::{Lang, Timestamp};
Expand Down Expand Up @@ -282,7 +284,7 @@ pub(crate) fn from_tag<'a>(
},

// TIPL key-value mappings
_ => {
_ if TIPL_MAPPINGS.iter().any(|(k, _)| *k == item_key) => {
let (_, tipl_key) = TIPL_MAPPINGS.iter().find(|(k, _)| *k == item_key)?;

let (value, _) = take_item_text_and_description(item)?;
Expand All @@ -293,6 +295,25 @@ pub(crate) fn from_tag<'a>(
// TIPL is collected at the end
None
},

// Anything else
_ => {
let Ok(id) = FrameId::try_from(item_key) else {
return None;
};

if id.as_str().starts_with('T') {
let (value, _) = take_item_text_and_description(item)?;
return Some(new_text_frame(id, value));
}

if id.as_str().starts_with('W') {
let (value, _) = take_item_text_and_description(item)?;
return Some(new_url_frame(id, value));
}

None
},
}
}

Expand Down
33 changes: 33 additions & 0 deletions lofty/src/id3/v2/tag/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,3 +1536,36 @@ fn multi_item_tag_dump() {
let artist_tag = tag.get_text(&FrameId::new("TPE1").unwrap()).unwrap();
assert_eq!(artist_tag, "Foo\0Bar");
}

#[test_log::test]
fn single_value_frame() {
let mut tag = Tag::new(TagType::Id3v2);

// TBPM should be deduplicated during the conversion, taking whatever happens to be first
tag.push(TagItem::new(
ItemKey::IntegerBpm,
ItemValue::Text(String::from("120")),
));
tag.push(TagItem::new(
ItemKey::IntegerBpm,
ItemValue::Text(String::from("130")),
));
tag.push(TagItem::new(
ItemKey::IntegerBpm,
ItemValue::Text(String::from("140")),
));

let mut id3v2 = Vec::new();
tag.dump_to(&mut id3v2, WriteOptions::default()).unwrap();

let tag = read_tag_with_options(
&id3v2,
ParseOptions::new().parsing_mode(ParsingMode::Strict),
);

// The other BPM values were discarded, **NOT** merged
assert_eq!(tag.len(), 1);

let artist_tag = tag.get_text(&FrameId::new("TBPM").unwrap()).unwrap();
assert_eq!(artist_tag, "120");
}