Skip to content

Commit

Permalink
Merge pull request #9 from marcograss/fix_deps
Browse files Browse the repository at this point in the history
Fix dependencies problem #8 and clippy warnings
  • Loading branch information
Ayrx authored Nov 20, 2022
2 parents be885fe + 0b0259e commit 425e038
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ exclude = ["examples/**", "axmldecoder-printer/**"]

[dependencies]
byteorder = "1.4.3"
deku = "~0.13"
indexmap = "1.7.0"
thiserror = "1.0.30"
deku = "~0.15"
indexmap = "1.9.2"
thiserror = "1.0.37"
2 changes: 1 addition & 1 deletion axmldecoder-printer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.40"
anyhow = "1.0.66"
axmldecoder = { path = "../" }
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ pub enum ParseError {
Utf16StringParseError(std::string::FromUtf16Error),
}

///Parses an Android binary XML and returns a [XmlDocument] object.
///Parses an Android binary XML and returns a [`XmlDocument`] object.
///
/// # Errors
///
/// Will return `ParseError` if `input` cannot be parsed
///```rust
///use axmldecoder::parse;
///# use axmldecoder::ParseError;
Expand Down Expand Up @@ -76,7 +79,7 @@ mod tests {
let mut f = File::open(entry.path()).unwrap();
let mut buf = Vec::new();
f.read_to_end(&mut buf).unwrap();
parse(&buf).expect(&format!("{} failed to parse", entry.path().display()));
parse(&buf).unwrap_or_else(|_| panic!("{} failed to parse", entry.path().display()));
}
}
}
7 changes: 4 additions & 3 deletions src/stringpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@ pub(crate) struct StringPoolHeader {
#[derive(Debug, DekuRead)]
pub(crate) struct StringPool {
pub(crate) header: StringPoolHeader,
#[deku(reader = "StringPool::read_strings(&*header, deku::rest)")]
#[deku(reader = "StringPool::read_strings(header, deku::rest)")]
pub(crate) strings: Vec<Rc<String>>,
}

type DekuRest = BitSlice<Msb0, u8>;
type DekuRest = BitSlice<u8, Msb0>;
impl StringPool {
fn read_strings<'a>(
header: &StringPoolHeader,
mut rest: &'a DekuRest,
) -> Result<(&'a DekuRest, Vec<Rc<String>>), DekuError> {
const STRINGPOOL_HEADER_SIZE: usize = std::mem::size_of::<StringPoolHeader>();

assert_eq!(header.style_count, 0);

let flag_is_utf8 = (header.flags & (1 << 8)) != 0;

const STRINGPOOL_HEADER_SIZE: usize = std::mem::size_of::<StringPoolHeader>();
let s = usize::try_from(header.chunk_header.size).unwrap() - STRINGPOOL_HEADER_SIZE;

let mut string_pool_data = vec![0; s];
Expand Down
31 changes: 16 additions & 15 deletions src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl XmlDocument {
element_tracker
.last_mut()
.unwrap()
.insert_children(Node::Cdata(cdata))
.insert_children(Node::Cdata(cdata));
}
};
}
Expand All @@ -74,6 +74,7 @@ impl XmlDocument {
}

///Returns the root [Element] of the XML document.
#[must_use]
pub fn get_root(&self) -> &Option<Node> {
&self.root
}
Expand Down Expand Up @@ -130,7 +131,14 @@ impl XmlDocument {
let value = attr.typed_value.get_value(string_pool);

let mut final_name = String::new();
if !name.is_empty() {
if name.is_empty() {
let resource_id = resource_map
.get(usize::try_from(attr.name).unwrap())
.ok_or(ParseError::ResourceIdNotFound(attr.name))?;
let resource_str = get_resource_string(*resource_id)
.ok_or(ParseError::UnknownResourceString(*resource_id))?;
final_name.push_str(&resource_str);
} else {
if let Some(n) = ns {
// There are samples where the namespace value is the
// raw string instead of a URI found in a namespace chunk.
Expand All @@ -143,13 +151,6 @@ impl XmlDocument {
};
}
final_name.push_str(&name);
} else {
let resource_id = resource_map
.get(usize::try_from(attr.name).unwrap())
.ok_or(ParseError::ResourceIdNotFound(attr.name))?;
let resource_str = get_resource_string(*resource_id)
.ok_or(ParseError::UnknownResourceString(*resource_id))?;
final_name.push_str(&resource_str);
}

attributes.insert(final_name, value.to_string());
Expand Down Expand Up @@ -180,16 +181,19 @@ pub struct Element {

impl Element {
///Returns a map of attributes associated with the element.
#[must_use]
pub fn get_attributes(&self) -> &IndexMap<String, String> {
&self.attributes
}

///Returns the element tag.
#[must_use]
pub fn get_tag(&self) -> &str {
&self.tag
}

///Returns a list of child nodes.
#[must_use]
pub fn get_children(&self) -> &Vec<Node> {
&self.children
}
Expand All @@ -206,6 +210,7 @@ pub struct Cdata {
}

impl Cdata {
#[must_use]
pub fn get_data(&self) -> &str {
&self.data
}
Expand Down Expand Up @@ -1546,11 +1551,7 @@ fn get_resource_string(resource_id: u32) -> Option<String> {
"colorSecondary",
];

let i = resource_id - 0x1010000;
let i = resource_id - 0x0101_0000;

Some(
RESOURCE_STRINGS
.get(usize::try_from(i).unwrap())?
.to_string(),
)
Some((*RESOURCE_STRINGS.get(usize::try_from(i).unwrap())?).to_string())
}

0 comments on commit 425e038

Please sign in to comment.