Skip to content

Commit 2132d01

Browse files
uklotzdeSerial-ATA
authored andcommitted
SplitAndRejoin: Add a read/modify/write usage example
1 parent 2b562c4 commit 2132d01

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/traits.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,42 @@ pub trait TagExt: Accessor + Into<Tag> + Sized {
235235

236236
/// Split and rejoin tags.
237237
///
238-
/// Useful and required for implementing read/modify/write round trips.
238+
/// Useful and required for implementing lossless read/modify/write round trips.
239+
///
240+
/// # Example
241+
///
242+
/// ```no_run
243+
/// use lofty::mpeg::MPEGFile;
244+
/// use lofty::{AudioFile, ItemKey, SplitAndRejoinTag as _};
245+
///
246+
/// // Read the tag from a file
247+
/// # let mut file = std::fs::OpenOptions::new().write(true).open("/path/to/file.mp3")?;
248+
/// # let parse_options = lofty::ParseOptions::default();
249+
/// let mut mpeg_file = <MPEGFile as AudioFile>::read_from(&mut file, parse_options)?;
250+
/// let mut id3v2 = if let Some(id3v2) = mpeg_file.id3v2_mut() {
251+
/// id3v2
252+
/// } else {
253+
/// // Add a new ID3v2 tag if missing
254+
/// mpeg_file.set_id3v2(Default::default());
255+
/// mpeg_file.id3v2_mut().expect("ID3v2")
256+
/// };
257+
///
258+
/// // Split: ID3v2 -> [`lofty::Tag`]
259+
/// let mut tag = id3v2.split_tag();
260+
///
261+
/// // Modify the metadata in the generic [`lofty::Tag`], independent
262+
/// // of the underlying tag and file format.
263+
/// tag.insert_text(ItemKey::TrackTitle, "Track Title".to_owned());
264+
/// tag.remove_key(&ItemKey::Composer);
265+
///
266+
/// // ID3v2 <- [`lofty::Tag`]
267+
/// id3v2.rejoin_tag(tag);
268+
///
269+
/// // Write the changes back into the file
270+
/// mpeg_file.save_to(&mut file)?;
271+
///
272+
/// # Ok::<(), lofty::LoftyError>(())
273+
/// ```
239274
pub trait SplitAndRejoinTag {
240275
/// Extract and split generic contents into a [`Tag`].
241276
///

0 commit comments

Comments
 (0)