Add from_bytes method for PairingFile

This commit is contained in:
Jackson Coxson
2025-01-09 18:13:45 -08:00
parent 8a8e80e91f
commit 2c78f79c38

View File

@@ -44,14 +44,19 @@ struct RawPairingFile {
impl PairingFile { impl PairingFile {
pub fn read_from_file(path: impl AsRef<Path>) -> Result<Self, crate::IdeviceError> { pub fn read_from_file(path: impl AsRef<Path>) -> Result<Self, crate::IdeviceError> {
let f = std::fs::read_to_string(path)?; let f = std::fs::read(path)?;
let r = match plist::from_bytes::<RawPairingFile>(f.as_bytes()) { Self::from_bytes(&f)
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self, crate::IdeviceError> {
let r = match ::plist::from_bytes::<RawPairingFile>(bytes) {
Ok(r) => r, Ok(r) => r,
Err(e) => { Err(e) => {
warn!("Unable to read raw pairing file to memory: {e:?}"); warn!("Unable to convert bytes to raw pairing file: {e:?}");
return Err(crate::IdeviceError::UnexpectedResponse); return Err(crate::IdeviceError::UnexpectedResponse);
} }
}; };
match r.try_into() { match r.try_into() {
Ok(r) => Ok(r), Ok(r) => Ok(r),
Err(e) => { Err(e) => {
@@ -84,20 +89,3 @@ impl TryFrom<RawPairingFile> for PairingFile {
}) })
} }
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn f1() {
let f = std::fs::read_to_string(
"/Users/jacksoncoxson/Documents/00008140-0016243626F3001C.mobiledevicepairing",
)
.unwrap();
let p: plist::Dictionary = plist::from_bytes(f.as_bytes()).unwrap();
println!("{p:#?}");
let p: RawPairingFile = plist::from_bytes(f.as_bytes()).unwrap();
println!("{p:#?}");
}
}