Skip to content

Conversation

@uklotzde
Copy link
Contributor

@uklotzde uklotzde commented Jan 18, 2023

Adds a new trait SplitAndRejoinTag with methods that are needed for safely and efficiently implementing read/modify/write round trips when synchronizing metadata:

  • fn split_tag(&mut self) -> Tag;
  • fn rejoin_tag(&mut self);

The central function from my code base that accomplishes the task demonstrates how they are supposed to be used:

fn split_export_rejoin_track_to_tag<T>(
    tag_repr: &mut T,
    config: &ExportTrackConfig,
    track: &mut Track,
    edit_embedded_artwork_image: Option<EditEmbeddedArtworkImage>,
) where
    T: SplitAndRejoinTag,
{
    // Split the generic tag contents from the underlying representation.
    // The remainder will remain untouched while modifying the generic tag.
    let mut tag = tag_repr.split_tag();
    // Export the metadata of the given track into the generic tag, i.e.
    // add, modify, or delete items and pictures accordingly.
    export_track_to_tag(&mut tag, config, track, edit_embedded_artwork_image);
    // Rejoin the generic tag contents back into the remainder of the underlying
    // representation.
    tag_repr.rejoin_tag(tag);
    // Depending on `T` some post-processing might be required in the outer context
    // to update contents in `tag_repr` that are not (yet) supported by the generic
    // `lofty::Tag` representation.
}

Ideally the task would be implemented using a type-state-pattern that consumes its arguments to prevent mutating the remainder between split and rejoin. I am considering this alternative, stay tuned... Didn't turn out as helpful. I left a comment in the docs in case anyone has the same idea.

@uklotzde uklotzde marked this pull request as ready for review January 19, 2023 00:55
@uklotzde uklotzde changed the title TagExt: Split and rejoin tags for read/modify/write round trips Split and rejoin tags for read/modify/write round trips Jan 19, 2023
Copy link
Owner

@Serial-ATA Serial-ATA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea! Thanks for implementing it. I just have some doc changes.

@Serial-ATA
Copy link
Owner

Having thought about it, I think rejoin_tag should actually be merge_tag. Since you could really use it to add any tag into another.

@Serial-ATA Serial-ATA merged commit 533cd7d into Serial-ATA:main Jan 20, 2023
@uklotzde uklotzde deleted the split-and-rejoin-tag branch January 30, 2023 23:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants