Skip to content

Commit 3119b09

Browse files
committed
fix. test fix of android sdk error.
1 parent 31dd741 commit 3119b09

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

apk/src/compiler/attributes.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn compile_attr(table: &Table, name: &str, value: &str, strings: &Strings) -
1515
let id = table.entry_by_ref(Ref::parse(value)?)?.id();
1616
(u32::from(id), ResValueType::Reference)
1717
}
18-
ResAttributeType::String => (strings.id(value) as u32, ResValueType::String),
18+
ResAttributeType::String => (strings.id(value)? as u32, ResValueType::String),
1919
ResAttributeType::Integer => (value.parse()?, ResValueType::IntDec),
2020
ResAttributeType::Boolean => match value {
2121
"true" => (0xffff_ffff, ResValueType::IntBoolean),
@@ -107,11 +107,12 @@ pub struct Strings {
107107
}
108108

109109
impl Strings {
110-
pub fn id(&self, s2: &str) -> i32 {
111-
self.strings
112-
.iter()
113-
.position(|s| s == s2)
114-
.with_context(|| format!("all strings added to the string pool: {s2}"))
115-
.unwrap() as i32
110+
pub fn id(&self, s2: &str) -> Result<i32> {
111+
match self.strings.iter().position(|s| s == s2) {
112+
Some(pos) => Ok(pos as i32),
113+
None => {
114+
anyhow::bail!("String '{}' not found in string pool. Available strings: {:?}", s2, self.strings);
115+
}
116+
}
116117
}
117118
}

apk/src/compiler/xml.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub fn compile_xml(xml: &str, table: &Table) -> Result<Chunk> {
2020
chunks.push(Chunk::XmlStartNamespace(
2121
ResXmlNodeHeader::default(),
2222
ResXmlNamespace {
23-
prefix: ns.name().map(|ns| strings.id(ns)).unwrap_or(-1),
24-
uri: strings.id(ns.uri()),
23+
prefix: ns.name().map(|ns| strings.id(ns).unwrap_or(-1)).unwrap_or(-1),
24+
uri: strings.id(ns.uri())?,
2525
},
2626
));
2727
}
@@ -30,8 +30,8 @@ pub fn compile_xml(xml: &str, table: &Table) -> Result<Chunk> {
3030
chunks.push(Chunk::XmlEndNamespace(
3131
ResXmlNodeHeader::default(),
3232
ResXmlNamespace {
33-
prefix: ns.name().map(|ns| strings.id(ns)).unwrap_or(-1),
34-
uri: strings.id(ns.uri()),
33+
prefix: ns.name().map(|ns| strings.id(ns).unwrap_or(-1)).unwrap_or(-1),
34+
uri: strings.id(ns.uri())?,
3535
},
3636
));
3737
}
@@ -107,7 +107,7 @@ fn compile_node(
107107
size: 8,
108108
res0: 0,
109109
data_type: ResValueType::String as u8,
110-
data: strings.id(attr.value()) as u32,
110+
data: strings.id(attr.value())? as u32,
111111
}
112112
};
113113
let raw_value = if value.data_type == ResValueType::String as u8 {
@@ -116,8 +116,8 @@ fn compile_node(
116116
-1
117117
};
118118
let attr = ResXmlAttribute {
119-
namespace: attr.namespace().map(|ns| strings.id(ns)).unwrap_or(-1),
120-
name: strings.id(attr.name()),
119+
namespace: attr.namespace().map(|ns| strings.id(ns).unwrap_or(-1)).unwrap_or(-1),
120+
name: strings.id(attr.name())?,
121121
raw_value,
122122
typed_value: value,
123123
};
@@ -126,9 +126,9 @@ fn compile_node(
126126
let namespace = node
127127
.tag_name()
128128
.namespace()
129-
.map(|ns| strings.id(ns))
129+
.map(|ns| strings.id(ns).unwrap_or(-1))
130130
.unwrap_or(-1);
131-
let name = strings.id(node.tag_name().name());
131+
let name = strings.id(node.tag_name().name())?;
132132
chunks.push(Chunk::XmlStartElement(
133133
ResXmlNodeHeader::default(),
134134
ResXmlStartElement {
@@ -145,7 +145,7 @@ fn compile_node(
145145
));
146146
/*let mut children = BTreeMap::new();
147147
for node in node.children() {
148-
children.insert(strings.id(node.tag_name().name()), node);
148+
children.insert(strings.id(node.tag_name().name())?, node);
149149
}
150150
for (_, node) in children {
151151
compile_node(node, strings, chunks)?;

0 commit comments

Comments
 (0)