add some error managment
This commit is contained in:
parent
53a30fad23
commit
bda74f55ac
5 changed files with 70 additions and 10 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -6,6 +6,7 @@ version = 3
|
|||
name = "androscalpel"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"androscalpel_serializer",
|
||||
"pyo3",
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -9,4 +9,5 @@ name = "androscalpel"
|
|||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
androscalpel_serializer = { version = "0.1.0", path = "../androscalpel_serializer" }
|
||||
pyo3 = "0.19.0"
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
//! Representation of an apk.
|
||||
|
||||
use crate::Class;
|
||||
|
||||
use pyo3::prelude::*;
|
||||
|
||||
use crate::{Class, Result};
|
||||
use androscalpel_serializer::DexFileReader;
|
||||
|
||||
/// Represent an apk.
|
||||
#[pyclass]
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -14,8 +14,9 @@ pub struct Apk {
|
|||
|
||||
impl Apk {
|
||||
/// Add the content of a dex file to the apk.
|
||||
pub fn add_dex_file(&mut self) {
|
||||
todo!()
|
||||
pub fn add_dex_file(&mut self, data: &[u8]) -> Result<()> {
|
||||
let dex = DexFileReader::new(data)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
use pyo3::exceptions::PyValueError;
|
||||
use pyo3::prelude::*;
|
||||
|
||||
pub mod apk;
|
||||
|
|
@ -6,6 +7,62 @@ pub mod class;
|
|||
pub use apk::*;
|
||||
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.
|
||||
#[pymodule]
|
||||
fn androscalpel(_py: Python, m: &PyModule) -> PyResult<()> {
|
||||
|
|
|
|||
|
|
@ -27,11 +27,11 @@ impl<T: Read + Seek> ReadSeek for T {}
|
|||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::InputTooSmall(msg) => write!(f, "Error: {}", msg),
|
||||
Self::SerializationError(msg) => write!(f, "Error: {}", msg),
|
||||
Self::DeserializationError(msg) => write!(f, "Error: {}", msg),
|
||||
Self::InvalidStringEncoding(msg) => write!(f, "Error: {}", msg),
|
||||
Self::InconsistantStruct(msg) => write!(f, "Error: {}", msg),
|
||||
Self::InputTooSmall(msg) => write!(f, "{}", msg),
|
||||
Self::SerializationError(msg) => write!(f, "{}", msg),
|
||||
Self::DeserializationError(msg) => write!(f, "{}", msg),
|
||||
Self::InvalidStringEncoding(msg) => write!(f, "{}", msg),
|
||||
Self::InconsistantStruct(msg) => write!(f, "{}", msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue