Skip to content

Commit

Permalink
[fix] improve the error resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
iqiziqi committed Jun 21, 2024
1 parent 72f1ab0 commit 2cc4ca0
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions crates/ncmdump/src/ncmdump.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use aes::Aes128;
use std::io::{Read, Seek, SeekFrom, Write};

use base64::engine::general_purpose::STANDARD;
use aes::Aes128;
use base64::Engine;
use cipher::block_padding::Pkcs7;
use base64::engine::general_purpose::STANDARD;
use cipher::{BlockDecryptMut, KeyInit};
use cipher::block_padding::Pkcs7;
use serde::{Deserialize, Serialize};

use crate::error::{Errors, Result};
Expand All @@ -17,7 +17,6 @@ const INFO_KEY: [u8; 16] = [
0x23, 0x31, 0x34, 0x6C, 0x6A, 0x6B, 0x5F, 0x21, 0x5C, 0x5D, 0x26, 0x30, 0x55, 0x3C, 0x27, 0x28,
];


#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(untagged)]
pub enum NcmId {
Expand Down Expand Up @@ -84,35 +83,41 @@ where
key_box: Vec<usize>,
}

impl NcmInfo {
pub fn from(raw_info: RawNcmInfo) -> Self {
Self {
impl TryFrom<RawNcmInfo> for NcmInfo {
type Error = Errors;

fn try_from(raw_info: RawNcmInfo) -> std::result::Result<Self, Self::Error> {
Ok(Self {
name: raw_info.name,
id: raw_info.id.get_id().unwrap(),
id: raw_info.id.get_id()?,
album: raw_info.album,
artist: raw_info.artist.into_iter().map(|(name, id)| (name, id.get_id().unwrap())).collect(),
bitrate: raw_info.bitrate.get_id().unwrap(),
duration: raw_info.duration.get_id().unwrap(),
artist: raw_info
.artist
.into_iter()
.map(|(name, id)| Ok((name, id.get_id()?)))
.collect::<Result<Vec<(String, u64)>>>()?,
bitrate: raw_info.bitrate.get_id()?,
duration: raw_info.duration.get_id()?,
format: raw_info.format,
mv_id: match raw_info.mv_id {
Some(id) => id.get_id(),
Some(id) => Some(id.get_id()?),
None => None,
},
alias: raw_info.alias,
}
})
}
}

impl NcmId {
pub fn get_id(self) -> Option<u64> {
pub fn get_id(self) -> Result<u64> {
match self {
NcmId::String(s) => {
if s.is_empty() {
return None;
return Err(Errors::InfoDecodeError);
}
s.parse().ok()
},
NcmId::Integer(num) => Some(num),
s.parse().map_err(|_| Errors::InfoDecodeError)
}
NcmId::Integer(num) => Ok(num),
}
}
}
Expand Down Expand Up @@ -308,7 +313,7 @@ where
String::from_utf8(info_data[6..].to_vec()).map_err(|_| Errors::InfoDecodeError)?;
let info =
serde_json::from_str::<RawNcmInfo>(&info_str).map_err(|_| Errors::InfoDecodeError)?;
Ok(NcmInfo::from(info))
NcmInfo::try_from(info)
}

/// Get the image bytes from ncmdump, if it's exists.
Expand Down Expand Up @@ -445,6 +450,22 @@ pub mod tests {
Ok(())
}

#[test]
fn test_ncm_info_convert_error() {
let result = NcmInfo::try_from(RawNcmInfo {
name: "".to_string(),
id: NcmId::String(String::from("")),
album: "".to_string(),
artist: vec![],
bitrate: NcmId::String(String::from("")),
duration: NcmId::String(String::from("")),
format: "".to_string(),
mv_id: None,
alias: None,
});
assert!(result.is_err());
}

#[test]
fn test_get_image_ok() -> Result<()> {
let reader = File::open("res/test.ncm")?;
Expand Down

0 comments on commit 2cc4ca0

Please sign in to comment.