mirror of
https://github.com/Serial-ATA/lofty-rs
synced 2025-03-04 14:57:17 +00:00
Picture: Remove MimeType::None
variant
This is better expressed by making `Picture` store an `Option<MimeType>` instead. Signed-off-by: Serial <69764315+Serial-ATA@users.noreply.github.com>
This commit is contained in:
parent
b89ca2a30d
commit
beb82f6416
6 changed files with 52 additions and 37 deletions
|
@ -42,13 +42,14 @@ impl AttachedPictureFrame {
|
|||
None => err!(NotAPicture),
|
||||
};
|
||||
|
||||
let mime_type = if version == Id3v2Version::V2 {
|
||||
let mime_type;
|
||||
if version == Id3v2Version::V2 {
|
||||
let mut format = [0; 3];
|
||||
reader.read_exact(&mut format)?;
|
||||
|
||||
match format {
|
||||
[b'P', b'N', b'G'] => MimeType::Png,
|
||||
[b'J', b'P', b'G'] => MimeType::Jpeg,
|
||||
[b'P', b'N', b'G'] => mime_type = Some(MimeType::Png),
|
||||
[b'J', b'P', b'G'] => mime_type = Some(MimeType::Jpeg),
|
||||
_ => {
|
||||
return Err(Id3v2Error::new(Id3v2ErrorKind::BadPictureFormat(
|
||||
String::from_utf8_lossy(&format).into_owned(),
|
||||
|
@ -57,8 +58,9 @@ impl AttachedPictureFrame {
|
|||
},
|
||||
}
|
||||
} else {
|
||||
(crate::util::text::decode_text(reader, TextEncoding::UTF8, true)?.text_or_none())
|
||||
.map_or(MimeType::None, |mime_type| MimeType::from_str(&mime_type))
|
||||
let mime_type_str =
|
||||
crate::util::text::decode_text(reader, TextEncoding::UTF8, true)?.text_or_none();
|
||||
mime_type = mime_type_str.map(|mime_type_str| MimeType::from_str(&mime_type_str));
|
||||
};
|
||||
|
||||
let pic_type = PictureType::from_u8(reader.read_u8()?);
|
||||
|
@ -103,19 +105,22 @@ impl AttachedPictureFrame {
|
|||
if version == Id3v2Version::V2 {
|
||||
// ID3v2.2 PIC is pretty limited with formats
|
||||
let format = match self.picture.mime_type {
|
||||
MimeType::Png => "PNG",
|
||||
MimeType::Jpeg => "JPG",
|
||||
Some(MimeType::Png) => "PNG",
|
||||
Some(MimeType::Jpeg) => "JPG",
|
||||
_ => {
|
||||
let mime_str = self.picture.mime_str();
|
||||
return Err(Id3v2Error::new(Id3v2ErrorKind::BadPictureFormat(
|
||||
self.picture.mime_type.to_string(),
|
||||
mime_str.to_string(),
|
||||
))
|
||||
.into())
|
||||
.into());
|
||||
},
|
||||
};
|
||||
|
||||
data.write_all(format.as_bytes())?;
|
||||
} else {
|
||||
data.write_all(self.picture.mime_type.as_str().as_bytes())?;
|
||||
if let Some(mime_type) = &self.picture.mime_type {
|
||||
data.write_all(mime_type.as_str().as_bytes())?;
|
||||
}
|
||||
data.write_u8(0)?;
|
||||
};
|
||||
|
||||
|
|
|
@ -1784,7 +1784,7 @@ mod tests {
|
|||
encoding: TextEncoding::Latin1,
|
||||
picture: Picture {
|
||||
pic_type: PictureType::CoverFront,
|
||||
mime_type: MimeType::Png,
|
||||
mime_type: Some(MimeType::Png),
|
||||
description: None,
|
||||
data: read_path("tests/tags/assets/id3v2/test_full_cover.png").into(),
|
||||
},
|
||||
|
@ -1841,7 +1841,7 @@ mod tests {
|
|||
|
||||
let picture = Picture::new_unchecked(
|
||||
PictureType::CoverFront,
|
||||
MimeType::Jpeg,
|
||||
Some(MimeType::Jpeg),
|
||||
Some(String::from("cover")),
|
||||
picture_data,
|
||||
);
|
||||
|
|
|
@ -294,12 +294,12 @@ where
|
|||
for (flags, value) in atom_data {
|
||||
let mime_type = match flags {
|
||||
// Type 0 is implicit
|
||||
RESERVED => MimeType::None,
|
||||
RESERVED => None,
|
||||
// GIF is deprecated
|
||||
12 => MimeType::Gif,
|
||||
JPEG => MimeType::Jpeg,
|
||||
PNG => MimeType::Png,
|
||||
BMP => MimeType::Bmp,
|
||||
12 => Some(MimeType::Gif),
|
||||
JPEG => Some(MimeType::Jpeg),
|
||||
PNG => Some(MimeType::Png),
|
||||
BMP => Some(MimeType::Bmp),
|
||||
_ => err!(BadAtom("\"covr\" atom has an unknown type")),
|
||||
};
|
||||
|
||||
|
|
|
@ -446,12 +446,12 @@ fn write_int(
|
|||
fn write_picture(picture: &Picture, writer: &mut Cursor<Vec<u8>>) -> Result<()> {
|
||||
match picture.mime_type {
|
||||
// GIF is deprecated
|
||||
MimeType::Gif => write_data(12, &picture.data, writer),
|
||||
MimeType::Jpeg => write_data(13, &picture.data, writer),
|
||||
MimeType::Png => write_data(14, &picture.data, writer),
|
||||
MimeType::Bmp => write_data(27, &picture.data, writer),
|
||||
Some(MimeType::Gif) => write_data(12, &picture.data, writer),
|
||||
Some(MimeType::Jpeg) => write_data(13, &picture.data, writer),
|
||||
Some(MimeType::Png) => write_data(14, &picture.data, writer),
|
||||
Some(MimeType::Bmp) => write_data(27, &picture.data, writer),
|
||||
// We'll assume implicit (0) was the intended type
|
||||
MimeType::None => write_data(0, &picture.data, writer),
|
||||
None => write_data(0, &picture.data, writer),
|
||||
_ => Err(FileEncodingError::new(
|
||||
FileType::Mp4,
|
||||
"Attempted to write an unsupported picture format",
|
||||
|
|
|
@ -127,7 +127,7 @@ where
|
|||
|
||||
let picture = Picture {
|
||||
pic_type: PictureType::Other,
|
||||
mime_type,
|
||||
mime_type: Some(mime_type),
|
||||
description: None,
|
||||
data: Cow::from(picture_data),
|
||||
};
|
||||
|
|
|
@ -51,8 +51,6 @@ pub enum MimeType {
|
|||
Gif,
|
||||
/// Some unknown mimetype
|
||||
Unknown(String),
|
||||
/// No mimetype
|
||||
None,
|
||||
}
|
||||
|
||||
impl ToString for MimeType {
|
||||
|
@ -64,7 +62,6 @@ impl ToString for MimeType {
|
|||
MimeType::Bmp => "image/bmp".to_string(),
|
||||
MimeType::Gif => "image/gif".to_string(),
|
||||
MimeType::Unknown(unknown) => unknown.clone(),
|
||||
MimeType::None => String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -91,7 +88,6 @@ impl MimeType {
|
|||
"image/tiff" => Self::Tiff,
|
||||
"image/bmp" => Self::Bmp,
|
||||
"image/gif" => Self::Gif,
|
||||
"" => Self::None,
|
||||
_ => Self::Unknown(mime_type.to_string()),
|
||||
}
|
||||
}
|
||||
|
@ -116,7 +112,6 @@ impl MimeType {
|
|||
MimeType::Bmp => "image/bmp",
|
||||
MimeType::Gif => "image/gif",
|
||||
MimeType::Unknown(unknown) => unknown,
|
||||
MimeType::None => "",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -457,7 +452,7 @@ pub struct Picture {
|
|||
/// The picture type according to ID3v2 APIC
|
||||
pub(crate) pic_type: PictureType,
|
||||
/// The picture's mimetype
|
||||
pub(crate) mime_type: MimeType,
|
||||
pub(crate) mime_type: Option<MimeType>,
|
||||
/// The picture's description
|
||||
pub(crate) description: Option<Cow<'static, str>>,
|
||||
/// The binary data of the picture
|
||||
|
@ -505,7 +500,7 @@ impl Picture {
|
|||
|
||||
Ok(Self {
|
||||
pic_type: PictureType::Other,
|
||||
mime_type,
|
||||
mime_type: Some(mime_type),
|
||||
description: None,
|
||||
data: data.into(),
|
||||
})
|
||||
|
@ -518,7 +513,7 @@ impl Picture {
|
|||
/// beforehand.
|
||||
pub fn new_unchecked(
|
||||
pic_type: PictureType,
|
||||
mime_type: MimeType,
|
||||
mime_type: Option<MimeType>,
|
||||
description: Option<String>,
|
||||
data: Vec<u8>,
|
||||
) -> Self {
|
||||
|
@ -544,8 +539,16 @@ impl Picture {
|
|||
///
|
||||
/// The `mime_type` is determined from the `data`, and
|
||||
/// is immutable.
|
||||
pub fn mime_type(&self) -> &MimeType {
|
||||
&self.mime_type
|
||||
pub fn mime_type(&self) -> Option<&MimeType> {
|
||||
self.mime_type.as_ref()
|
||||
}
|
||||
|
||||
// Used commonly internally
|
||||
pub(crate) fn mime_str(&self) -> &str {
|
||||
match self.mime_type.as_ref() {
|
||||
Some(mime_type) => mime_type.as_str(),
|
||||
None => "",
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the description
|
||||
|
@ -582,7 +585,7 @@ impl Picture {
|
|||
|
||||
let picture_type = u32::from(self.pic_type.as_u8()).to_be_bytes();
|
||||
|
||||
let mime_str = self.mime_type.to_string();
|
||||
let mime_str = self.mime_str();
|
||||
let mime_len = mime_str.len() as u32;
|
||||
|
||||
data.extend(picture_type);
|
||||
|
@ -703,10 +706,17 @@ impl Picture {
|
|||
let mut data = try_vec![0; data_len];
|
||||
|
||||
if let Ok(()) = reader.read_exact(&mut data) {
|
||||
let mime_type;
|
||||
if mime_type_str.is_empty() {
|
||||
mime_type = None;
|
||||
} else {
|
||||
mime_type = Some(MimeType::from_str(&mime_type_str));
|
||||
}
|
||||
|
||||
return Ok((
|
||||
Self {
|
||||
pic_type: PictureType::from_u8(pic_ty as u8),
|
||||
mime_type: MimeType::from_str(mime_type_str),
|
||||
mime_type,
|
||||
description,
|
||||
data: Cow::from(data),
|
||||
},
|
||||
|
@ -787,7 +797,7 @@ impl Picture {
|
|||
|
||||
Ok(Picture {
|
||||
pic_type,
|
||||
mime_type,
|
||||
mime_type: Some(mime_type),
|
||||
description,
|
||||
data,
|
||||
})
|
||||
|
@ -808,7 +818,7 @@ impl Picture {
|
|||
// A placeholder that is needed during conversions.
|
||||
pub(crate) const TOMBSTONE_PICTURE: Picture = Picture {
|
||||
pic_type: PictureType::Other,
|
||||
mime_type: MimeType::Unknown(String::new()),
|
||||
mime_type: None,
|
||||
description: None,
|
||||
data: Cow::Owned(Vec::new()),
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue