Skip to content

Commit 7bfc61f

Browse files
committed
Deny unwrap
1 parent 1e7accb commit 7bfc61f

File tree

8 files changed

+137
-79
lines changed

8 files changed

+137
-79
lines changed

src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(let_chains)]
2+
#![deny(clippy::unwrap_used)]
23
pub mod manager;
34
pub mod mergers;
45
pub mod settings;
@@ -38,14 +39,14 @@ fn find_modified_files(py: Python, mod_dir: String) -> PyResult<Vec<String>> {
3839
let content = mod_dir.join(util::content());
3940
let dlc = mod_dir.join(util::dlc());
4041
let files: Vec<PathBuf> = py.allow_threads(|| {
41-
glob::glob(mod_dir.join("**/*").to_str().unwrap())
42-
.unwrap()
42+
glob::glob(&mod_dir.join("**/*").to_string_lossy())
43+
.expect("Bad glob?!?!?!")
4344
.filter_map(std::result::Result::ok)
4445
.par_bridge()
4546
.filter(|f| {
4647
f.is_file()
4748
&& (f.starts_with(&content) || f.starts_with(&dlc))
48-
&& util::get_canon_name(f.strip_prefix(mod_dir).unwrap())
49+
&& util::get_canon_name(unsafe { f.strip_prefix(mod_dir).unwrap_unchecked() })
4950
.and_then(|canon| {
5051
fs::read(f)
5152
.ok()
@@ -60,7 +61,7 @@ fn find_modified_files(py: Python, mod_dir: String) -> PyResult<Vec<String>> {
6061
Ok(files
6162
.par_iter()
6263
.filter(|f| {
63-
fs::metadata(f).unwrap().len() > 4
64+
fs::metadata(f).expect("No file metadata!?!?!?!").len() > 4
6465
&& f.extension()
6566
.and_then(|ext| ext.to_str())
6667
.map(|ext| botw_utils::extensions::SARC_EXTS.contains(&ext))
@@ -71,7 +72,7 @@ fn find_modified_files(py: Python, mod_dir: String) -> PyResult<Vec<String>> {
7172
find_modded_sarc_files(
7273
&sarc,
7374
file.starts_with(&dlc),
74-
&file.strip_prefix(mod_dir).unwrap().to_slash_lossy(),
75+
&unsafe { file.strip_prefix(mod_dir).unwrap_unchecked() }.to_slash_lossy(),
7576
)
7677
})
7778
.collect::<Result<Vec<_>>>()?
@@ -110,7 +111,10 @@ fn find_modded_sarc_files(sarc: &Sarc, aoc: bool, path: &str) -> Result<Vec<Stri
110111
modded_files.extend(find_modded_sarc_files(
111112
&sarc,
112113
aoc,
113-
modded_files.first().as_ref().unwrap(),
114+
modded_files
115+
.first()
116+
.as_ref()
117+
.expect("What a strange filename"),
114118
)?);
115119
}
116120
Ok(modded_files)

src/manager.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<'py, 'set> ModLinker<'py, 'set> {
9393
}
9494
let mod_folders: Vec<PathBuf> =
9595
glob::glob(&settings.mods_dir().join("*").to_string_lossy())
96-
.unwrap()
96+
.expect("Bad glob?!?!?")
9797
.filter_map(|p| p.ok())
9898
.filter(|p| p.is_dir() && !p.join(".disabled").exists())
9999
.collect::<std::collections::BTreeSet<PathBuf>>()
@@ -103,7 +103,7 @@ impl<'py, 'set> ModLinker<'py, 'set> {
103103
std::iter::once(p)
104104
.chain(
105105
glob::glob(&glob_str)
106-
.unwrap()
106+
.expect("Bad glob?!?!?")
107107
.filter_map(|p| p.ok())
108108
.filter(|p| p.is_dir()),
109109
)
@@ -118,10 +118,10 @@ impl<'py, 'set> ModLinker<'py, 'set> {
118118
.try_for_each(|folder| -> Result<()> {
119119
let mod_files: Vec<(PathBuf, PathBuf)> =
120120
glob::glob(&folder.join("**/*").to_string_lossy())
121-
.unwrap()
121+
.expect("Bad glob?!?!?!")
122122
.filter_map(|p| {
123123
p.ok().map(|p| {
124-
(p.clone(), p.strip_prefix(&folder).unwrap().to_owned())
124+
(p.clone(), unsafe {p.strip_prefix(&folder).unwrap_unchecked()}.to_owned())
125125
})
126126
})
127127
.filter(|(item, rel)| {
@@ -145,7 +145,7 @@ impl<'py, 'set> ModLinker<'py, 'set> {
145145
.map(fs::create_dir_all)
146146
.transpose()
147147
.with_context(|| jstr!("Failed to create parent folder for file {rel.to_str().unwrap()}"))?
148-
.unwrap();
148+
.expect("Whoa, why is there no parent folder?");
149149
fs::hard_link(&item, &out)
150150
.with_context(|| jstr!("Failed to hard link {rel.to_str().unwrap()} to {out.to_str().unwrap()}"))
151151
.or_else(|_| {
@@ -302,11 +302,11 @@ impl<'py, 'set> ModLinker<'py, 'set> {
302302
}
303303
}
304304
if glob::glob(&output.join("*").to_string_lossy())
305-
.unwrap()
305+
.expect("Bad glob?!?!?!")
306306
.filter_map(|p| p.ok())
307307
.count()
308308
== 0
309-
&& std::fs::read_dir(settings.mods_dir()).unwrap().count() > 1
309+
&& std::fs::read_dir(settings.mods_dir())?.count() > 1
310310
{
311311
Err(anyhow::anyhow!("Output folder is empty"))
312312
} else {

src/mergers/actorinfo.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static STOCK_ACTORINFO: Lazy<Result<Arc<ActorMap>>> = Lazy::new(|| {
2323
.iter()
2424
.map(|actor| -> Result<(u32, Byml)> {
2525
Ok((
26-
roead::aamp::hash_name(actor.as_hash().unwrap()["name"].as_string()?),
26+
roead::aamp::hash_name(actor.as_hash()?["name"].as_string()?),
2727
actor.clone(),
2828
))
2929
})
@@ -107,7 +107,7 @@ fn diff_actorinfo(py: Python, actorinfo_path: String) -> PyResult<PyObject> {
107107
})
108108
})
109109
.collect();
110-
Ok(Byml::Hash(diff).to_text().unwrap().as_bytes().to_vec())
110+
Ok(Byml::Hash(diff).to_text()?.as_bytes().to_vec())
111111
} else {
112112
anyhow::bail!("Modded actor info is not a hash???")
113113
}
@@ -120,11 +120,11 @@ fn merge_actorinfo(py: Python, modded_actors: Vec<u8>) -> PyResult<()> {
120120
let merge = || -> Result<()> {
121121
let modded_actor_root = Byml::from_binary(&modded_actors)?;
122122
let modded_actors: ActorMap = py.allow_threads(|| -> Result<ActorMap> {
123-
Ok(modded_actor_root
123+
modded_actor_root
124124
.as_hash()?
125125
.into_par_iter()
126-
.map(|(h, a)| (h.parse::<u32>().unwrap(), a.clone()))
127-
.collect())
126+
.map(|(h, a)| Ok((h.parse::<u32>()?, a.clone())))
127+
.collect()
128128
})?;
129129
let mut merged_actors = stock_actorinfo()?.as_ref().clone();
130130
merge_actormap(&mut merged_actors, &modded_actors);
@@ -152,8 +152,8 @@ fn merge_actorinfo(py: Python, modded_actors: Vec<u8>) -> PyResult<()> {
152152
let output = util::settings()
153153
.master_content_dir()
154154
.join("Actor/ActorInfo.product.sbyml");
155-
if !output.parent().unwrap().exists() {
156-
fs::create_dir_all(output.parent().unwrap())?;
155+
if !output.parent().expect("No parent folder?!?!?").exists() {
156+
fs::create_dir_all(output.parent().expect("No parent folder?!?!?"))?;
157157
}
158158
fs::write(
159159
output,

src/mergers/maps.rs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ impl Display for MapUnit {
6262
impl TryFrom<&Path> for MapUnit {
6363
type Error = anyhow::Error;
6464
fn try_from(value: &Path) -> Result<Self> {
65-
let mut split = value.file_stem().unwrap().to_str().unwrap().split('_');
65+
let mut split = value
66+
.file_stem()
67+
.unwrap_or_default()
68+
.to_str()
69+
.unwrap_or_default()
70+
.split('_');
6671
Ok(MapUnit {
6772
unit: split.next().context("Not a map unitt")?.into(),
6873
kind: split.next().context("Not a map unitt")?.into(),
69-
aocfield: value.to_str().unwrap().contains("AocField"),
74+
aocfield: value.to_str().unwrap_or_default().contains("AocField"),
7075
})
7176
}
7277
}
@@ -96,7 +101,10 @@ impl MapUnit {
96101
}
97102

98103
fn get_aoc_path(&self) -> PathBuf {
99-
util::settings().dlc_dir().unwrap().join(self.get_path())
104+
util::settings()
105+
.dlc_dir()
106+
.expect("There's no DLC folder")
107+
.join(self.get_path())
100108
}
101109

102110
fn get_resource_path(&self) -> String {
@@ -126,8 +134,7 @@ impl MapUnit {
126134
MapUnitType::Static => {
127135
let pack = util::get_stock_pack("TitleBG")?;
128136
Ok(Byml::from_binary(&decompress(
129-
pack
130-
.get_data(&self.get_path())
137+
pack.get_data(&self.get_path())
131138
.with_context(|| {
132139
jstr!("Failed to read {&self.get_path()} from TitleBG.pack")
133140
})?
@@ -146,8 +153,7 @@ impl MapUnit {
146153
MapUnitType::Static => {
147154
let pack = util::get_stock_pack("AocMainField")?;
148155
Ok(Byml::from_binary(&decompress(
149-
pack
150-
.get_data(&self.get_path())
156+
pack.get_data(&self.get_path())
151157
.with_context(|| {
152158
jstr!("Failed to read {&self.get_path()} from TitleBG.pack")
153159
})?
@@ -161,11 +167,11 @@ impl MapUnit {
161167
fn merge_entries(diff: &Hash, entries: &mut Vec<Byml>) -> Result<()> {
162168
let stock_hashes: Vec<u32> = entries
163169
.iter()
164-
.map(|e| e["HashId"].as_u32().unwrap())
165-
.collect();
170+
.map(|e| Ok(e["HashId"].as_u32()?))
171+
.collect::<Result<_>>()?;
166172
let mut orphans: Vec<Byml> = vec![];
167173
for (hash, entry) in diff["mod"].as_hash()? {
168-
let hash = hash.parse::<u32>().unwrap();
174+
let hash = hash.parse::<u32>()?;
169175
if let Some(idx) = stock_hashes.iter().position(|h| *h == hash) {
170176
entries[idx] = entry.clone();
171177
} else {
@@ -188,19 +194,18 @@ fn merge_entries(diff: &Hash, entries: &mut Vec<Byml>) -> Result<()> {
188194
.cloned()
189195
.chain(orphans.into_iter())
190196
.filter(|e| {
191-
!stock_hashes.contains(
192-
&(e["HashId"]
193-
.as_u32()
194-
.or_else(|_| e["HashId"].as_i32().map(|i| i as u32))
195-
.unwrap()),
196-
)
197+
e["HashId"]
198+
.as_u32()
199+
.or_else(|_| e["HashId"].as_i32().map(|i| i as u32))
200+
.map(|h| !stock_hashes.contains(&h))
201+
.unwrap_or(false)
197202
}),
198203
);
199204
entries.sort_by_cached_key(|e| {
200205
e["HashId"]
201206
.as_u32()
202207
.or_else(|_| e["HashId"].as_i32().map(|i| i as u32))
203-
.unwrap()
208+
.unwrap_or(0)
204209
});
205210
Ok(())
206211
}
@@ -218,16 +223,18 @@ fn merge_map(map_unit: MapUnit, diff: &Hash, settings: &Settings) -> Result<(Str
218223
merge_entries(diff["Rails"].as_hash()?, rails)?;
219224
}
220225
let data = new_map.to_binary(settings.endian());
221-
let size = rstb::calc::calc_from_size_and_name(
222-
data.len(),
223-
"dummy.mubin",
224-
if settings.wiiu {
225-
rstb::Endian::Big
226-
} else {
227-
rstb::Endian::Little
228-
},
229-
)
230-
.unwrap();
226+
let size = unsafe {
227+
rstb::calc::calc_from_size_and_name(
228+
data.len(),
229+
"dummy.mubin",
230+
if settings.wiiu {
231+
rstb::Endian::Big
232+
} else {
233+
rstb::Endian::Little
234+
},
235+
)
236+
.unwrap_unchecked()
237+
};
231238
let out = settings
232239
.master_mod_dir()
233240
.join(if util::settings().dlc_dir().is_some() {
@@ -236,8 +243,8 @@ fn merge_map(map_unit: MapUnit, diff: &Hash, settings: &Settings) -> Result<(Str
236243
util::content()
237244
})
238245
.join(map_unit.get_path());
239-
if !out.parent().unwrap().exists() {
240-
fs::create_dir_all(out.parent().unwrap())?;
246+
if !out.parent().expect("Folder has no parent?!?").exists() {
247+
fs::create_dir_all(out.parent().expect("Folder has no parent?!?"))?;
241248
}
242249
fs::write(out, compress(data))?;
243250
Ok((
@@ -252,7 +259,7 @@ fn merge_map(map_unit: MapUnit, diff: &Hash, settings: &Settings) -> Result<(Str
252259

253260
#[pyfunction]
254261
pub fn merge_maps(py: Python, diff_bytes: Vec<u8>) -> PyResult<PyObject> {
255-
let diffs = Byml::from_binary(&diff_bytes).unwrap();
262+
let diffs = Byml::from_binary(&diff_bytes).map_err(anyhow::Error::from)?;
256263
let rstb_values: HashMap<String, u32> = if let Byml::Hash(diffs) = diffs {
257264
py.allow_threads(|| -> Result<HashMap<String, u32>> {
258265
let settings = util::settings().clone();

src/mergers/pack.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn merge_sarc(sarcs: Vec<Sarc>, endian: Endian) -> Result<Vec<u8>> {
9494
let mut merged = merge_sarc(nest_sarcs, endian)?;
9595
if file_path
9696
.extension()
97-
.map(|e| e.to_str().unwrap().starts_with('s'))
97+
.map(|e| e.to_str().unwrap_or_default().starts_with('s'))
9898
.unwrap_or_default()
9999
{
100100
merged = compress(&merged);
@@ -133,10 +133,16 @@ pub fn merge_sarcs(py: Python, diffs: HashMap<PathBuf, Vec<PathBuf>>) -> PyResul
133133
})
134134
.collect::<Result<Vec<Sarc>>>()?;
135135
let mut merged = merge_sarc(sarcs, settings.endian())?;
136-
if out.extension().unwrap().to_str().unwrap().starts_with('s') {
136+
if out
137+
.extension()
138+
.unwrap_or_default()
139+
.to_str()
140+
.unwrap_or_default()
141+
.starts_with('s')
142+
{
137143
merged = compress(merged);
138144
}
139-
fs::create_dir_all(out.parent().unwrap())?;
145+
fs::create_dir_all(out.parent().expect("No parent folder??!?"))?;
140146
fs::write(out, merged)?;
141147
Ok(())
142148
})?;

src/mergers/texts.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ pub fn diff_language(
3131
let diff = py.allow_threads(|| -> Result<IndexMap<String, Diff>> {
3232
let language = &Path::new(&mod_bootup_path)
3333
.file_stem()
34-
.unwrap()
34+
.expect("Okay, how does this path have no name?")
3535
.to_str()
36-
.unwrap()[7..];
36+
.expect("And this should definitely work, too")[7..];
3737
let mod_bootup = Sarc::new(fs::read(&mod_bootup_path)?)?;
3838
let stock_bootup = Sarc::new(fs::read(&stock_bootup_path)?)?;
3939
let message_path = jstr!("Message/Msg_{&language}.product.ssarc");
@@ -67,7 +67,8 @@ pub fn diff_language(
6767
.with_context(|| jstr!("Invalid MSBT file: {&path}"))?;
6868
if let Some(stock_text) = stock_message
6969
.get_data(&path)
70-
.unwrap()
70+
.ok()
71+
.flatten()
7172
.and_then(|data| Msyt::from_msbt_bytes(data).ok())
7273
{
7374
if mod_text == stock_text {
@@ -80,8 +81,7 @@ pub fn diff_language(
8081
if only_new_keys {
8182
!stock_text.entries.contains_key(*e)
8283
} else {
83-
!stock_text.entries.contains_key(*e)
84-
|| *t != stock_text.entries.get(*e).unwrap()
84+
stock_text.entries.get(*e) != Some(t)
8585
}
8686
})
8787
.map(|(e, t)| (e.to_owned(), t.clone()))
@@ -105,7 +105,8 @@ pub fn diff_language(
105105
.collect();
106106
Ok(diffs)
107107
})?;
108-
let diff_text = serde_json::to_string(&diff).unwrap();
108+
let diff_text =
109+
serde_json::to_string(&diff).expect("It's whack if this diff doesn't serialize");
109110
let json = PyModule::import(py, "json")?;
110111
#[allow(deprecated)]
111112
let dict = json.call_method1("loads", (&diff_text,))?;
@@ -130,9 +131,9 @@ pub fn merge_language(
130131
py.allow_threads(|| -> Result<()> {
131132
let language = &Path::new(&stock_bootup_path)
132133
.file_stem()
133-
.unwrap()
134+
.expect("Okay, how does this path have no name?")
134135
.to_str()
135-
.unwrap()[7..];
136+
.expect("And this should definitely work, too")[7..];
136137
let stock_bootup = Sarc::new(fs::read(&stock_bootup_path)?)?;
137138
let message_path = format!("Message/Msg_{}.product.ssarc", &language);
138139
let stock_message = Sarc::new(decompress(

0 commit comments

Comments
 (0)