|
| 1 | +use crate::error::{Id3v2Error, Id3v2ErrorKind, LoftyError, Result}; |
| 2 | +use crate::id3::v2::frame::{ |
| 3 | + FrameRef, EMPTY_CONTENT_DESCRIPTOR, MUSICBRAINZ_UFID_OWNER, UNKNOWN_LANGUAGE, |
| 4 | +}; |
| 5 | +use crate::id3::v2::tag::{ |
| 6 | + new_binary_frame, new_comment_frame, new_text_frame, new_unsync_text_frame, new_url_frame, |
| 7 | + new_user_text_frame, new_user_url_frame, |
| 8 | +}; |
| 9 | +use crate::id3::v2::{ |
| 10 | + ExtendedTextFrame, ExtendedUrlFrame, Frame, FrameFlags, FrameId, PopularimeterFrame, |
| 11 | + UniqueFileIdentifierFrame, UnsynchronizedTextFrame, |
| 12 | +}; |
| 13 | +use crate::macros::err; |
| 14 | +use crate::tag::{ItemKey, ItemValue, TagItem, TagType}; |
| 15 | +use crate::TextEncoding; |
| 16 | + |
| 17 | +use std::borrow::Cow; |
| 18 | + |
| 19 | +fn frame_from_unknown_item(id: FrameId<'_>, item_value: ItemValue) -> Result<Frame<'_>> { |
| 20 | + match item_value { |
| 21 | + ItemValue::Text(text) => Ok(new_text_frame(id, text)), |
| 22 | + ItemValue::Locator(locator) => { |
| 23 | + if TextEncoding::verify_latin1(&locator) { |
| 24 | + Ok(new_url_frame(id, locator)) |
| 25 | + } else { |
| 26 | + err!(TextDecode("ID3v2 URL frames must be Latin-1")); |
| 27 | + } |
| 28 | + }, |
| 29 | + ItemValue::Binary(binary) => Ok(new_binary_frame(id, binary.clone())), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl From<TagItem> for Option<Frame<'static>> { |
| 34 | + fn from(input: TagItem) -> Self { |
| 35 | + let value; |
| 36 | + match input.key().try_into().map(FrameId::into_owned) { |
| 37 | + Ok(id) => { |
| 38 | + match (&id, input.item_value) { |
| 39 | + (FrameId::Valid(ref s), ItemValue::Text(text)) if s == "COMM" => { |
| 40 | + value = new_comment_frame(text); |
| 41 | + }, |
| 42 | + (FrameId::Valid(ref s), ItemValue::Text(text)) if s == "USLT" => { |
| 43 | + value = Frame::UnsynchronizedText(UnsynchronizedTextFrame::new( |
| 44 | + TextEncoding::UTF8, |
| 45 | + UNKNOWN_LANGUAGE, |
| 46 | + EMPTY_CONTENT_DESCRIPTOR, |
| 47 | + text, |
| 48 | + )); |
| 49 | + }, |
| 50 | + (FrameId::Valid(ref s), ItemValue::Locator(text) | ItemValue::Text(text)) |
| 51 | + if s == "WXXX" => |
| 52 | + { |
| 53 | + value = Frame::UserUrl(ExtendedUrlFrame::new( |
| 54 | + TextEncoding::UTF8, |
| 55 | + EMPTY_CONTENT_DESCRIPTOR, |
| 56 | + text, |
| 57 | + )); |
| 58 | + }, |
| 59 | + (FrameId::Valid(ref s), ItemValue::Text(text)) if s == "TXXX" => { |
| 60 | + value = new_user_text_frame(EMPTY_CONTENT_DESCRIPTOR, text); |
| 61 | + }, |
| 62 | + (FrameId::Valid(ref s), ItemValue::Binary(text)) if s == "POPM" => { |
| 63 | + value = Frame::Popularimeter( |
| 64 | + PopularimeterFrame::parse(&mut &text[..], FrameFlags::default()) |
| 65 | + .ok()?, |
| 66 | + ); |
| 67 | + }, |
| 68 | + (_, item_value) => value = frame_from_unknown_item(id, item_value).ok()?, |
| 69 | + }; |
| 70 | + }, |
| 71 | + Err(_) => match input.item_key.map_key(TagType::Id3v2, true) { |
| 72 | + Some(desc) => match input.item_value { |
| 73 | + ItemValue::Text(text) => { |
| 74 | + value = Frame::UserText(ExtendedTextFrame::new( |
| 75 | + TextEncoding::UTF8, |
| 76 | + String::from(desc), |
| 77 | + text, |
| 78 | + )) |
| 79 | + }, |
| 80 | + ItemValue::Locator(locator) => { |
| 81 | + value = Frame::UserUrl(ExtendedUrlFrame::new( |
| 82 | + TextEncoding::UTF8, |
| 83 | + String::from(desc), |
| 84 | + locator, |
| 85 | + )) |
| 86 | + }, |
| 87 | + ItemValue::Binary(_) => return None, |
| 88 | + }, |
| 89 | + None => match (input.item_key, input.item_value) { |
| 90 | + (ItemKey::MusicBrainzRecordingId, ItemValue::Text(recording_id)) => { |
| 91 | + if !recording_id.is_ascii() { |
| 92 | + return None; |
| 93 | + } |
| 94 | + let frame = UniqueFileIdentifierFrame::new( |
| 95 | + MUSICBRAINZ_UFID_OWNER.to_owned(), |
| 96 | + recording_id.into_bytes(), |
| 97 | + ); |
| 98 | + value = Frame::UniqueFileIdentifier(frame); |
| 99 | + }, |
| 100 | + _ => { |
| 101 | + return None; |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + Some(value) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +impl<'a> TryFrom<&'a TagItem> for FrameRef<'a> { |
| 112 | + type Error = LoftyError; |
| 113 | + |
| 114 | + fn try_from(tag_item: &'a TagItem) -> std::result::Result<Self, Self::Error> { |
| 115 | + let id: crate::error::Result<FrameId<'a>> = tag_item.key().try_into(); |
| 116 | + let value: Frame<'_>; |
| 117 | + match id { |
| 118 | + Ok(id) => { |
| 119 | + let id_str = id.as_str(); |
| 120 | + |
| 121 | + match (id_str, tag_item.value()) { |
| 122 | + ("COMM", ItemValue::Text(text)) => { |
| 123 | + value = new_comment_frame(text.clone()); |
| 124 | + }, |
| 125 | + ("USLT", ItemValue::Text(text)) => { |
| 126 | + value = new_unsync_text_frame(text.clone()); |
| 127 | + }, |
| 128 | + ("WXXX", ItemValue::Locator(text) | ItemValue::Text(text)) => { |
| 129 | + value = new_user_url_frame(EMPTY_CONTENT_DESCRIPTOR, text.clone()); |
| 130 | + }, |
| 131 | + (locator_id, ItemValue::Locator(text)) if locator_id.len() > 4 => { |
| 132 | + value = new_user_url_frame(String::from(locator_id), text.clone()); |
| 133 | + }, |
| 134 | + ("TXXX", ItemValue::Text(text)) => { |
| 135 | + value = new_user_text_frame(EMPTY_CONTENT_DESCRIPTOR, text.clone()); |
| 136 | + }, |
| 137 | + (text_id, ItemValue::Text(text)) if text_id.len() > 4 => { |
| 138 | + value = new_user_text_frame(String::from(text_id), text.clone()); |
| 139 | + }, |
| 140 | + ("POPM", ItemValue::Binary(contents)) => { |
| 141 | + value = Frame::Popularimeter(PopularimeterFrame::parse( |
| 142 | + &mut &contents[..], |
| 143 | + FrameFlags::default(), |
| 144 | + )?); |
| 145 | + }, |
| 146 | + (_, item_value) => value = frame_from_unknown_item(id, item_value.clone())?, |
| 147 | + }; |
| 148 | + }, |
| 149 | + Err(_) => { |
| 150 | + let item_key = tag_item.key(); |
| 151 | + let Some(desc) = item_key.map_key(TagType::Id3v2, true) else { |
| 152 | + return Err(Id3v2Error::new(Id3v2ErrorKind::UnsupportedFrameId( |
| 153 | + item_key.clone(), |
| 154 | + )) |
| 155 | + .into()); |
| 156 | + }; |
| 157 | + |
| 158 | + match tag_item.value() { |
| 159 | + ItemValue::Text(text) => { |
| 160 | + value = new_user_text_frame(String::from(desc), text.clone()); |
| 161 | + }, |
| 162 | + ItemValue::Locator(locator) => { |
| 163 | + value = new_user_url_frame(String::from(desc), locator.clone()); |
| 164 | + }, |
| 165 | + _ => { |
| 166 | + return Err(Id3v2Error::new(Id3v2ErrorKind::UnsupportedFrameId( |
| 167 | + item_key.clone(), |
| 168 | + )) |
| 169 | + .into()) |
| 170 | + }, |
| 171 | + } |
| 172 | + }, |
| 173 | + } |
| 174 | + |
| 175 | + Ok(FrameRef(Cow::Owned(value))) |
| 176 | + } |
| 177 | +} |
0 commit comments