Skip to content

Commit e9f6003

Browse files
committed
Remove unnecessary Results from return types of infallible functions
1 parent be13600 commit e9f6003

File tree

4 files changed

+59
-69
lines changed

4 files changed

+59
-69
lines changed

src/bin/commands/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl Run {
6666
let image = match docker::get_image(&config, &target, false) {
6767
Ok(i) => i,
6868
Err(docker::GetImageError::NoCompatibleImages(..))
69-
if config.dockerfile(&target)?.is_some() =>
69+
if config.dockerfile(&target).is_some() =>
7070
{
7171
"scratch".into()
7272
}

src/config.rs

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ impl Config {
277277
env: impl for<'a> Fn(&'a Environment, &Target) -> (Option<Vec<String>>, Option<Vec<String>>),
278278
config: impl for<'a> Fn(&'a CrossToml, &Target) -> (Option<&'a [String]>, Option<&'a [String]>),
279279
sum: bool,
280-
) -> Result<Option<Vec<String>>> {
280+
) -> Option<Vec<String>> {
281281
if sum {
282282
let (mut env_build, env_target) = env(&self.env, target);
283283
env_build
@@ -294,14 +294,14 @@ impl Config {
294294
target: &Target,
295295
env: impl for<'a> Fn(&'a Environment, &Target) -> (Option<T>, Option<T>),
296296
config: impl for<'a> Fn(&'a CrossToml, &Target) -> (Option<&'a U>, Option<&'a U>),
297-
) -> Result<Option<T>>
297+
) -> Option<T>
298298
where
299299
U: ToOwned<Owned = T> + ?Sized,
300300
{
301301
let (env_build, env_target) = env(&self.env, target);
302302

303303
if let Some(env_target) = env_target {
304-
return Ok(Some(env_target));
304+
return Some(env_target);
305305
}
306306

307307
let (build, target) = self
@@ -312,13 +312,13 @@ impl Config {
312312

313313
// FIXME: let expression
314314
if target.is_none() && env_build.is_some() {
315-
return Ok(env_build);
315+
return env_build;
316316
}
317317

318318
if target.is_none() {
319-
Ok(build.map(ToOwned::to_owned))
319+
build.map(ToOwned::to_owned)
320320
} else {
321-
Ok(target.map(ToOwned::to_owned))
321+
target.map(ToOwned::to_owned)
322322
}
323323
}
324324

@@ -327,11 +327,11 @@ impl Config {
327327
target: &Target,
328328
env: impl Fn(&Environment, &Target) -> (Option<T>, Option<T>),
329329
config: impl Fn(&CrossToml, &Target) -> (Option<T>, Option<T>),
330-
) -> Result<Option<T>> {
330+
) -> Option<T> {
331331
let (env_build, env_target) = env(&self.env, target);
332332

333333
if let Some(env_target) = env_target {
334-
return Ok(Some(env_target));
334+
return Some(env_target);
335335
}
336336

337337
let (build, target) = self
@@ -342,13 +342,13 @@ impl Config {
342342

343343
// FIXME: let expression
344344
if target.is_none() && env_build.is_some() {
345-
return Ok(env_build);
345+
return env_build;
346346
}
347347

348348
if target.is_none() {
349-
Ok(build)
349+
build
350350
} else {
351-
Ok(target)
351+
target
352352
}
353353
}
354354

@@ -361,33 +361,33 @@ impl Config {
361361
self.bool_from_config(target, Environment::xargo, CrossToml::xargo)
362362
}
363363

364-
pub fn build_std(&self, target: &Target) -> Result<Option<BuildStd>> {
364+
pub fn build_std(&self, target: &Target) -> Option<BuildStd> {
365365
self.get_from_ref(target, Environment::build_std, CrossToml::build_std)
366366
}
367367

368368
pub fn zig(&self, target: &Target) -> Option<bool> {
369369
self.bool_from_config(target, Environment::zig, CrossToml::zig)
370370
}
371371

372-
pub fn zig_version(&self, target: &Target) -> Result<Option<String>> {
372+
pub fn zig_version(&self, target: &Target) -> Option<String> {
373373
self.get_from_value(target, Environment::zig_version, CrossToml::zig_version)
374374
}
375375

376376
pub fn zig_image(&self, target: &Target) -> Result<Option<PossibleImage>> {
377377
let (b, t) = self.env.zig_image(target)?;
378-
self.get_from_value(target, |_, _| (b.clone(), t.clone()), CrossToml::zig_image)
378+
Ok(self.get_from_value(target, |_, _| (b.clone(), t.clone()), CrossToml::zig_image))
379379
}
380380

381381
pub fn image(&self, target: &Target) -> Result<Option<PossibleImage>> {
382382
let env = self.env.image(target)?;
383-
self.get_from_ref(
383+
Ok(self.get_from_ref(
384384
target,
385385
move |_, _| (None, env.clone()),
386386
|toml, target| (None, toml.image(target)),
387-
)
387+
))
388388
}
389389

390-
pub fn runner(&self, target: &Target) -> Result<Option<String>> {
390+
pub fn runner(&self, target: &Target) -> Option<String> {
391391
self.get_from_ref(
392392
target,
393393
|env, target| (None, env.runner(target)),
@@ -411,7 +411,7 @@ impl Config {
411411
self.env.build_opts()
412412
}
413413

414-
pub fn env_passthrough(&self, target: &Target) -> Result<Option<Vec<String>>> {
414+
pub fn env_passthrough(&self, target: &Target) -> Option<Vec<String>> {
415415
self.vec_from_config(
416416
target,
417417
Environment::passthrough,
@@ -420,7 +420,7 @@ impl Config {
420420
)
421421
}
422422

423-
pub fn env_volumes(&self, target: &Target) -> Result<Option<Vec<String>>> {
423+
pub fn env_volumes(&self, target: &Target) -> Option<Vec<String>> {
424424
self.get_from_ref(target, Environment::volumes, CrossToml::env_volumes)
425425
}
426426

@@ -433,29 +433,26 @@ impl Config {
433433
.and_then(|t| t.default_target(target_list))
434434
}
435435

436-
pub fn dockerfile(&self, target: &Target) -> Result<Option<String>> {
436+
pub fn dockerfile(&self, target: &Target) -> Option<String> {
437437
self.get_from_ref(target, Environment::dockerfile, CrossToml::dockerfile)
438438
}
439439

440-
pub fn dockerfile_context(&self, target: &Target) -> Result<Option<String>> {
440+
pub fn dockerfile_context(&self, target: &Target) -> Option<String> {
441441
self.get_from_ref(
442442
target,
443443
Environment::dockerfile_context,
444444
CrossToml::dockerfile_context,
445445
)
446446
}
447447

448-
pub fn dockerfile_build_args(
449-
&self,
450-
target: &Target,
451-
) -> Result<Option<HashMap<String, String>>> {
448+
pub fn dockerfile_build_args(&self, target: &Target) -> Option<HashMap<String, String>> {
452449
// This value does not support env variables
453450
self.toml
454451
.as_ref()
455-
.map_or(Ok(None), |t| Ok(t.dockerfile_build_args(target)))
452+
.and_then(|t| t.dockerfile_build_args(target))
456453
}
457454

458-
pub fn pre_build(&self, target: &Target) -> Result<Option<PreBuild>> {
455+
pub fn pre_build(&self, target: &Target) -> Option<PreBuild> {
459456
self.get_from_ref(target, Environment::pre_build, CrossToml::pre_build)
460457
}
461458

@@ -464,7 +461,7 @@ impl Config {
464461
&'a self,
465462
env_values: Option<impl AsRef<[String]>>,
466463
toml_getter: impl FnOnce(&'a CrossToml) -> (Option<&'a [String]>, Option<&'a [String]>),
467-
) -> Result<Option<Vec<String>>> {
464+
) -> Option<Vec<String>> {
468465
let mut defined = false;
469466
let mut collect = vec![];
470467
if let Some(vars) = env_values {
@@ -481,9 +478,9 @@ impl Config {
481478
}
482479
}
483480
if !defined {
484-
Ok(None)
481+
None
485482
} else {
486-
Ok(Some(collect))
483+
Some(collect)
487484
}
488485
}
489486
}
@@ -635,9 +632,9 @@ mod tests {
635632
let env = Environment::new(Some(map));
636633
let config = Config::new_with(Some(toml(TOML_BUILD_XARGO_FALSE)?), env);
637634
assert_eq!(config.xargo(&target()), Some(true));
638-
assert_eq!(config.build_std(&target())?, None);
635+
assert_eq!(config.build_std(&target()), None);
639636
assert_eq!(
640-
config.pre_build(&target())?,
637+
config.pre_build(&target()),
641638
Some(PreBuild::Lines(vec![
642639
s!("apt-get update"),
643640
s!("apt-get install zlib-dev")
@@ -657,10 +654,10 @@ mod tests {
657654
let config = Config::new_with(Some(toml(TOML_TARGET_XARGO_FALSE)?), env);
658655
assert_eq!(config.xargo(&target()), Some(true));
659656
assert_eq!(
660-
config.build_std(&target())?,
657+
config.build_std(&target()),
661658
Some(BuildStd::Crates(vec!["core".to_owned()]))
662659
);
663-
assert_eq!(config.pre_build(&target())?, None);
660+
assert_eq!(config.pre_build(&target()), None);
664661

665662
Ok(())
666663
}
@@ -673,8 +670,8 @@ mod tests {
673670
let env = Environment::new(Some(map));
674671
let config = Config::new_with(Some(toml(TOML_BUILD_XARGO_FALSE)?), env);
675672
assert_eq!(config.xargo(&target()), Some(true));
676-
assert_eq!(config.build_std(&target())?, None);
677-
assert_eq!(config.pre_build(&target())?, None);
673+
assert_eq!(config.build_std(&target()), None);
674+
assert_eq!(config.pre_build(&target()), None);
678675

679676
Ok(())
680677
}
@@ -690,7 +687,7 @@ mod tests {
690687
let env = Environment::new(Some(map));
691688
let config = Config::new_with(Some(toml(TOML_BUILD_PRE_BUILD)?), env);
692689
assert_eq!(
693-
config.pre_build(&target())?,
690+
config.pre_build(&target()),
694691
Some(PreBuild::Single {
695692
line: s!("dpkg --add-architecture arm64"),
696693
env: true
@@ -711,14 +708,14 @@ mod tests {
711708

712709
let env = Environment::new(Some(map));
713710
let config = Config::new_with(Some(toml(TOML_BUILD_DOCKERFILE)?), env);
714-
assert_eq!(config.dockerfile(&target())?, Some(s!("Dockerfile4")));
715-
assert_eq!(config.dockerfile(&target2())?, Some(s!("Dockerfile3")));
711+
assert_eq!(config.dockerfile(&target()), Some(s!("Dockerfile4")));
712+
assert_eq!(config.dockerfile(&target2()), Some(s!("Dockerfile3")));
716713

717714
let map = HashMap::new();
718715
let env = Environment::new(Some(map));
719716
let config = Config::new_with(Some(toml(TOML_BUILD_DOCKERFILE)?), env);
720-
assert_eq!(config.dockerfile(&target())?, Some(s!("Dockerfile2")));
721-
assert_eq!(config.dockerfile(&target2())?, Some(s!("Dockerfile1")));
717+
assert_eq!(config.dockerfile(&target()), Some(s!("Dockerfile2")));
718+
assert_eq!(config.dockerfile(&target2()), Some(s!("Dockerfile1")));
722719

723720
Ok(())
724721
}
@@ -729,11 +726,11 @@ mod tests {
729726
let env = Environment::new(Some(map));
730727
let config = Config::new_with(Some(toml(TOML_ARRAYS_BOTH)?), env);
731728
assert_eq!(
732-
config.env_passthrough(&target())?,
729+
config.env_passthrough(&target()),
733730
Some(vec![s!("VAR1"), s!("VAR2"), s!("VAR3"), s!("VAR4")])
734731
);
735732
assert_eq!(
736-
config.env_volumes(&target())?,
733+
config.env_volumes(&target()),
737734
Some(vec![s!("VOLUME3"), s!("VOLUME4")])
738735
);
739736

@@ -746,11 +743,11 @@ mod tests {
746743
let env = Environment::new(Some(map));
747744
let config = Config::new_with(Some(toml(TOML_ARRAYS_BUILD)?), env);
748745
assert_eq!(
749-
config.env_passthrough(&target())?,
746+
config.env_passthrough(&target()),
750747
Some(vec![s!("VAR1"), s!("VAR2")])
751748
);
752749
assert_eq!(
753-
config.env_volumes(&target())?,
750+
config.env_volumes(&target()),
754751
Some(vec![s!("VOLUME1"), s!("VOLUME2")])
755752
);
756753

@@ -763,11 +760,11 @@ mod tests {
763760
let env = Environment::new(Some(map));
764761
let config = Config::new_with(Some(toml(TOML_ARRAYS_TARGET)?), env);
765762
assert_eq!(
766-
config.env_passthrough(&target())?,
763+
config.env_passthrough(&target()),
767764
Some(vec![s!("VAR3"), s!("VAR4")])
768765
);
769766
assert_eq!(
770-
config.env_volumes(&target())?,
767+
config.env_volumes(&target()),
771768
Some(vec![s!("VOLUME3"), s!("VOLUME4")])
772769
);
773770

@@ -782,7 +779,7 @@ mod tests {
782779
let config = Config::new_with(Some(toml(TOML_BUILD_VOLUMES)?), env);
783780
let expected = [s!("VOLUME1"), s!("VOLUME2")];
784781

785-
let result = config.env_volumes(&target()).unwrap().unwrap_or_default();
782+
let result = config.env_volumes(&target()).unwrap_or_default();
786783
dbg!(&result);
787784
assert!(result.len() == 2);
788785
assert!(result.contains(&expected[0]));
@@ -798,7 +795,7 @@ mod tests {
798795
let config = Config::new_with(Some(toml(TOML_BUILD_VOLUMES)?), env);
799796
let expected = [s!("VOLUME3"), s!("VOLUME4")];
800797

801-
let result = config.env_volumes(&target()).unwrap().unwrap_or_default();
798+
let result = config.env_volumes(&target()).unwrap_or_default();
802799
dbg!(&result);
803800
assert!(result.len() == 2);
804801
assert!(result.contains(&expected[0]));

src/docker/shared.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,8 @@ impl DockerOptions {
7272

7373
#[must_use]
7474
pub fn needs_custom_image(&self) -> bool {
75-
self.config
76-
.dockerfile(&self.target)
77-
.unwrap_or_default()
78-
.is_some()
79-
|| self
80-
.config
81-
.pre_build(&self.target)
82-
.unwrap_or_default()
83-
.is_some()
75+
self.config.dockerfile(&self.target).is_some()
76+
|| self.config.pre_build(&self.target).is_some()
8477
}
8578

8679
pub(crate) fn custom_image_build(
@@ -93,8 +86,8 @@ impl DockerOptions {
9386
msg_info.note("cannot install armhf system packages via apt for `arm-unknown-linux-gnueabihf`, since they are for ARMv7a targets but this target is ARMv6. installation of all packages for the armhf architecture has been blocked.")?;
9487
}
9588

96-
if let Some(path) = self.config.dockerfile(&self.target)? {
97-
let context = self.config.dockerfile_context(&self.target)?;
89+
if let Some(path) = self.config.dockerfile(&self.target) {
90+
let context = self.config.dockerfile_context(&self.target);
9891

9992
let is_custom_image = self.config.image(&self.target)?.is_some();
10093

@@ -114,13 +107,13 @@ impl DockerOptions {
114107
self,
115108
paths,
116109
self.config
117-
.dockerfile_build_args(&self.target)?
110+
.dockerfile_build_args(&self.target)
118111
.unwrap_or_default(),
119112
msg_info,
120113
)
121114
.wrap_err("when building dockerfile")?;
122115
}
123-
let pre_build = self.config.pre_build(&self.target)?;
116+
let pre_build = self.config.pre_build(&self.target);
124117

125118
if let Some(pre_build) = pre_build {
126119
match pre_build {
@@ -1014,7 +1007,7 @@ impl DockerCommandExt for Command {
10141007
let mut warned = false;
10151008
for ref var in options
10161009
.config
1017-
.env_passthrough(&options.target)?
1010+
.env_passthrough(&options.target)
10181011
.unwrap_or_default()
10191012
{
10201013
validate_env_var(
@@ -1030,7 +1023,7 @@ impl DockerCommandExt for Command {
10301023
self.args(["-e", var]);
10311024
}
10321025

1033-
let runner = options.config.runner(&options.target)?;
1026+
let runner = options.config.runner(&options.target);
10341027
let cross_runner = format!("CROSS_RUNNER={}", runner.unwrap_or_default());
10351028
self.args(["-e", &format!("XARGO_HOME={}", dirs.xargo_mount_path())])
10361029
.args(["-e", &format!("CARGO_HOME={}", dirs.cargo_mount_path())])
@@ -1164,7 +1157,7 @@ impl DockerCommandExt for Command {
11641157
let mut warned = false;
11651158
for ref var in options
11661159
.config
1167-
.env_volumes(&options.target)?
1160+
.env_volumes(&options.target)
11681161
.unwrap_or_default()
11691162
{
11701163
let (var, value) = validate_env_var(

0 commit comments

Comments
 (0)