add some error managment

This commit is contained in:
Jean-Marie Mineau 2023-08-31 11:39:03 +02:00
parent 53a30fad23
commit bda74f55ac
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
5 changed files with 70 additions and 10 deletions

1
Cargo.lock generated
View file

@ -6,6 +6,7 @@ version = 3
name = "androscalpel" name = "androscalpel"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"androscalpel_serializer",
"pyo3", "pyo3",
] ]

View file

@ -9,4 +9,5 @@ name = "androscalpel"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
androscalpel_serializer = { version = "0.1.0", path = "../androscalpel_serializer" }
pyo3 = "0.19.0" pyo3 = "0.19.0"

View file

@ -1,9 +1,9 @@
//! Representation of an apk. //! Representation of an apk.
use crate::Class;
use pyo3::prelude::*; use pyo3::prelude::*;
use crate::{Class, Result};
use androscalpel_serializer::DexFileReader;
/// Represent an apk. /// Represent an apk.
#[pyclass] #[pyclass]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -14,8 +14,9 @@ pub struct Apk {
impl Apk { impl Apk {
/// Add the content of a dex file to the apk. /// Add the content of a dex file to the apk.
pub fn add_dex_file(&mut self) { pub fn add_dex_file(&mut self, data: &[u8]) -> Result<()> {
todo!() let dex = DexFileReader::new(data)?;
Ok(())
} }
} }

View file

@ -1,3 +1,4 @@
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*; use pyo3::prelude::*;
pub mod apk; pub mod apk;
@ -6,6 +7,62 @@ pub mod class;
pub use apk::*; pub use apk::*;
pub use class::*; pub use class::*;
#[derive(Debug)]
pub enum Error {
InputTooSmall(String),
SerializationError(String),
DeserializationError(String),
InvalidStringEncoding(String),
InconsistantStruct(String),
}
impl From<androscalpel_serializer::Error> for Error {
fn from(error: androscalpel_serializer::Error) -> Self {
match error {
androscalpel_serializer::Error::InputTooSmall(err) => Self::InputTooSmall(err),
androscalpel_serializer::Error::SerializationError(err) => {
Self::SerializationError(err)
}
androscalpel_serializer::Error::DeserializationError(err) => {
Self::DeserializationError(err)
}
androscalpel_serializer::Error::InvalidStringEncoding(err) => {
Self::InvalidStringEncoding(err)
}
androscalpel_serializer::Error::InconsistantStruct(err) => {
Self::InconsistantStruct(err)
}
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::InputTooSmall(msg) => write!(f, "InputTooSmall: {}", msg),
Self::SerializationError(msg) => write!(f, "SerializationError: {}", msg),
Self::DeserializationError(msg) => write!(f, "DeserializationError: {}", msg),
Self::InvalidStringEncoding(msg) => write!(f, "InvalidStringEncoding: {}", msg),
Self::InconsistantStruct(msg) => write!(f, "InconsistantStruct: {}", msg),
}
}
}
impl std::error::Error for Error {}
impl From<Error> for PyErr {
fn from(error: Error) -> Self {
match error {
Error::InputTooSmall(err) => PyValueError::new_err(format!("{err}")),
Error::SerializationError(err) => PyValueError::new_err(format!("{err}")),
Error::DeserializationError(err) => PyValueError::new_err(format!("{err}")),
Error::InvalidStringEncoding(err) => PyValueError::new_err(format!("{err}")),
Error::InconsistantStruct(err) => PyValueError::new_err(format!("{err}")),
}
}
}
pub type Result<T> = core::result::Result<T, Error>;
/// Androscalpel. /// Androscalpel.
#[pymodule] #[pymodule]
fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> { fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> {

View file

@ -27,11 +27,11 @@ impl<T: Read + Seek> ReadSeek for T {}
impl std::fmt::Display for Error { impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self { match self {
Self::InputTooSmall(msg) => write!(f, "Error: {}", msg), Self::InputTooSmall(msg) => write!(f, "{}", msg),
Self::SerializationError(msg) => write!(f, "Error: {}", msg), Self::SerializationError(msg) => write!(f, "{}", msg),
Self::DeserializationError(msg) => write!(f, "Error: {}", msg), Self::DeserializationError(msg) => write!(f, "{}", msg),
Self::InvalidStringEncoding(msg) => write!(f, "Error: {}", msg), Self::InvalidStringEncoding(msg) => write!(f, "{}", msg),
Self::InconsistantStruct(msg) => write!(f, "Error: {}", msg), Self::InconsistantStruct(msg) => write!(f, "{}", msg),
} }
} }
} }