Skip to content

Commit 9554295

Browse files
committed
ID3v2: Add Id3v2Tag::get_texts
1 parent be6eb07 commit 9554295

File tree

2 files changed

+44
-7
lines changed

2 files changed

+44
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
- Support for "RVA2", "OWNE", "ETCO", and "PRIV" frames through
1212
`id3::v2::{RelativeVolumeAdjustmentFrame, OwnershipFrame, EventTimingCodesFrame, PrivateFrame}`
1313
- `FrameId` now implements `Display`
14+
- `Id3v2Tag::get_texts` for multi-value text frames
1415
- **MP4**:
1516
- `Atom::into_data`
1617
- `Atom::merge`
@@ -22,7 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2223
in a tag once and remove them. Those frames are: "MCDI", "ETCO", "MLLT", "SYTC", "RVRB", "PCNT", "RBUF", "POSS", "OWNE", "SEEK", and "ASPI".
2324
- `Id3v2Tag::remove` will now take a `FrameId` rather than `&str`
2425
- `FrameId` now implements `Into<Cow<'_, str>>`, making it possible to use it in `Frame::new`
25-
- `ID3v2Tag` getters will now use `&FrameId` instead of `&str` for IDs
26+
- `Id3v2Tag` getters will now use `&FrameId` instead of `&str` for IDs
2627
- **MP4**:
2728
- `Ilst::remove` will now return all of the removed atoms
2829
- `Ilst::insert_picture` will now combine all pictures into a single `covr` atom

src/id3/v2/tag.rs

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,41 @@ impl Id3v2Tag {
190190
None
191191
}
192192

193+
/// Gets all of the values for a text frame
194+
///
195+
/// NOTE: Multiple values are only supported in ID3v2.4, this will not be
196+
/// very useful for ID3v2.2/3 tags.
197+
///
198+
/// # Examples
199+
///
200+
/// ```rust
201+
/// use lofty::id3::v2::{FrameId, Id3v2Tag};
202+
/// use lofty::Accessor;
203+
/// use std::borrow::Cow;
204+
///
205+
/// const TITLE_ID: FrameId<'_> = FrameId::Valid(Cow::Borrowed("TIT2"));
206+
///
207+
/// let mut tag = Id3v2Tag::new();
208+
///
209+
/// tag.set_title(String::from("Foo\0Bar"));
210+
///
211+
/// let mut titles = tag.get_texts(&TITLE_ID);
212+
///
213+
/// assert_eq!(titles.next(), Some("Foo"));
214+
/// assert_eq!(titles.next(), Some("Bar"));
215+
/// ```
216+
pub fn get_texts(&self, id: &FrameId<'_>) -> Option<impl Iterator<Item = &str>> {
217+
if let Some(Frame {
218+
value: FrameValue::Text(TextInformationFrame { value, .. }),
219+
..
220+
}) = self.get(id)
221+
{
222+
return Some(value.split('\0'));
223+
}
224+
225+
None
226+
}
227+
193228
/// Gets the text for a user-defined frame
194229
///
195230
/// NOTE: If the tag is [`Id3v2Version::V4`], there could be multiple values separated by null characters (`'\0'`).
@@ -2376,11 +2411,12 @@ mod tests {
23762411
fn read_multiple_composers_should_not_fail_with_bad_frame_length() {
23772412
// Issue #255
23782413
let tag = read_tag("tests/tags/assets/id3v2/multiple_composers.id3v24");
2379-
assert_eq!(
2380-
tag.get_text(&FrameId::Valid(Cow::Borrowed("TCOM")))
2381-
.as_deref()
2382-
.unwrap(),
2383-
"A/B"
2384-
);
2414+
let mut composers = tag
2415+
.get_texts(&FrameId::Valid(Cow::Borrowed("TCOM")))
2416+
.unwrap();
2417+
2418+
assert_eq!(composers.next(), Some("A"));
2419+
assert_eq!(composers.next(), Some("B"));
2420+
assert_eq!(composers.next(), None)
23852421
}
23862422
}

0 commit comments

Comments
 (0)