Skip to content

Commit

Permalink
Minor cleanup.
Browse files Browse the repository at this point in the history
- Simplify some lifetime parameters.

- Unwrapping `Result<API>` inside each subcommand is not required.

- Fix doc comment mentioning old `reqwest` API.
  • Loading branch information
Arnavion committed Aug 16, 2020
1 parent 037f6a4 commit a2ec9f6
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 57 deletions.
6 changes: 1 addition & 5 deletions factorio-mods-web/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#![allow(
clippy::single_match_else,
)]

/// Wraps a `reqwest::unstable::r#async::Client` to only allow limited operations on it.
/// Wraps a `reqwest::Client` to only allow limited operations on it.
#[derive(Debug)]
pub(crate) struct Client {
inner: std::sync::Arc<reqwest::Client>,
Expand Down
10 changes: 4 additions & 6 deletions src/enable_disable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) struct EnableSubCommand {
impl EnableSubCommand {
pub(crate) async fn run(
self,
local_api: Result<&'_ factorio_mods_local::API, crate::Error>,
local_api: &factorio_mods_local::API,
prompt_override: Option<bool>,
) -> Result<(), crate::Error> {
enable_disable(self.names, local_api, prompt_override, true).await?;
Expand All @@ -24,24 +24,22 @@ pub(crate) struct DisableSubCommand {
impl DisableSubCommand {
pub(crate) async fn run(
self,
local_api: Result<&'_ factorio_mods_local::API, crate::Error>,
local_api: &factorio_mods_local::API,
prompt_override: Option<bool>,
) -> Result<(), crate::Error> {
enable_disable(self.names, local_api, prompt_override, false).await?;
Ok(())
}
}

pub(crate) async fn enable_disable<'a>(
pub(crate) async fn enable_disable(
mods: Vec<factorio_mods_common::ModName>,
local_api: Result<&'a factorio_mods_local::API, crate::Error>,
local_api: &factorio_mods_local::API,
prompt_override: Option<bool>,
enable: bool,
) -> Result<(), crate::Error> {
use crate::ResultExt;

let local_api = local_api?;

let mut all_installed_mods: std::collections::HashMap<_, Vec<_>> = Default::default();
for mod_ in local_api.installed_mods().context("could not enumerate installed mods")? {
let mod_ = mod_.context("could not process an installed mod")?;
Expand Down
9 changes: 3 additions & 6 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,13 @@ impl std::str::FromStr for Requirement {
}

impl SubCommand {
pub(crate) async fn run<'a>(
pub(crate) async fn run(
self,
local_api: Result<&'a factorio_mods_local::API, crate::Error>,
web_api: Result<&'a factorio_mods_web::API, crate::Error>,
local_api: &factorio_mods_local::API,
web_api: &factorio_mods_web::API,
mut config: crate::config::Config,
prompt_override: Option<bool>,
) -> Result<(), crate::Error> {
let local_api = local_api?;
let web_api = web_api?;

let mods = config.mods.as_mut().unwrap();
for requirement in self.requirements {
mods.insert(requirement.name, requirement.version);
Expand Down
4 changes: 1 addition & 3 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ pub(crate) struct SubCommand {
impl SubCommand {
pub(crate) async fn run(
self,
local_api: Result<&'_ factorio_mods_local::API, crate::Error>,
local_api: &factorio_mods_local::API,
) -> Result<(), crate::Error> {
use crate::ResultExt;

let local_api = local_api?;

let mods_status = local_api.mods_status().context("could not parse installed mods status")?;

let mut installed_mods: Vec<_> =
Expand Down
26 changes: 13 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,49 +124,49 @@ async fn main() -> Result<(), Error> {
}
}

let web_api = factorio_mods_web::API::new(client).context("could not initialize web API").map_err(Into::into);
let web_api = factorio_mods_web::API::new(client).context("could not initialize web API");


match options.subcommand {
SubCommand::Disable(parameters) => parameters.run(
match local_api { Ok(ref local_api) => Ok(local_api), Err(err) => Err(err) },
&local_api?,
prompt_override,
).await?,

SubCommand::Enable(parameters) => parameters.run(
match local_api { Ok(ref local_api) => Ok(local_api), Err(err) => Err(err) },
&local_api?,
prompt_override,
).await?,

SubCommand::Install(parameters) => parameters.run(
match local_api { Ok(ref local_api) => Ok(local_api), Err(err) => Err(err) },
match web_api { Ok(ref web_api) => Ok(web_api), Err(err) => Err(err) },
&local_api?,
&web_api?,
config,
prompt_override,
).await?,

SubCommand::List(parameters) => parameters.run(
match local_api { Ok(ref local_api) => Ok(local_api), Err(err) => Err(err) },
&local_api?,
).await?,

SubCommand::Remove(parameters) => parameters.run(
match local_api { Ok(ref local_api) => Ok(local_api), Err(err) => Err(err) },
match web_api { Ok(ref web_api) => Ok(web_api), Err(err) => Err(err) },
&local_api?,
&web_api?,
config,
prompt_override,
).await?,

SubCommand::Search(parameters) => parameters.run(
match web_api { Ok(ref web_api) => Ok(web_api), Err(err) => Err(err) },
&web_api?,
).await?,

SubCommand::Show(parameters) => parameters.run(
match web_api { Ok(ref web_api) => Ok(web_api), Err(err) => Err(err) },
&web_api?,
).await?,

SubCommand::Update(parameters) => parameters.run(
match local_api { Ok(ref local_api) => Ok(local_api), Err(err) => Err(err) },
match web_api { Ok(ref web_api) => Ok(web_api), Err(err) => Err(err) },
&local_api?,
&web_api?,
config,
prompt_override,
).await?,
Expand Down Expand Up @@ -204,7 +204,7 @@ impl std::error::Error for Error {
}

impl From<&'_ str> for Error {
fn from(err: &'_ str) -> Self {
fn from(err: &str) -> Self {
Error(err.into(), Default::default())
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ pub(crate) struct SubCommand {
}

impl SubCommand {
pub(crate) async fn run<'a>(
pub(crate) async fn run(
self,
local_api: Result<&'a factorio_mods_local::API, crate::Error>,
web_api: Result<&'a factorio_mods_web::API, crate::Error>,
local_api: &factorio_mods_local::API,
web_api: &factorio_mods_web::API,
mut config: crate::config::Config,
prompt_override: Option<bool>,
) -> Result<(), crate::Error> {
let local_api = local_api?;
let web_api = web_api?;

let mods = config.mods.as_mut().unwrap();

for mod_ in self.names {
Expand Down
4 changes: 1 addition & 3 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ pub(crate) struct SubCommand {
impl SubCommand {
pub(crate) async fn run(
self,
web_api: Result<&'_ factorio_mods_web::API, crate::Error>,
web_api: &factorio_mods_web::API,
) -> Result<(), crate::Error> {
use crate::ResultExt;

let web_api = web_api?;

let mut mods = web_api.search(&self.query);

while let Some(mod_) = futures_util::StreamExt::next(&mut mods).await {
Expand Down
4 changes: 1 addition & 3 deletions src/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ pub(crate) struct SubCommand {
impl SubCommand {
pub(crate) async fn run(
self,
web_api: Result<&'_ factorio_mods_web::API, crate::Error>,
web_api: &factorio_mods_web::API,
) -> Result<(), crate::Error> {
use crate::ResultExt;

let web_api = web_api?;

let mut mods: futures_util::stream::FuturesOrdered<_> =
self.names.into_iter().map(|name| async move {
web_api.get(&name).await
Expand Down
6 changes: 3 additions & 3 deletions src/solve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use crate::{ ErrorExt, ResultExt };
/// Asks the user for confirmation, then applies the diff.
///
/// Returns true if the diff was successfully applied or empty.
pub(crate) async fn compute_and_apply_diff<'a>(
local_api: &'a factorio_mods_local::API,
web_api: &'a factorio_mods_web::API,
pub(crate) async fn compute_and_apply_diff(
local_api: &factorio_mods_local::API,
web_api: &factorio_mods_web::API,
mut config: crate::config::Config,
prompt_override: Option<bool>,
) -> Result<(), crate::Error> {
Expand Down
9 changes: 3 additions & 6 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ pub(crate) struct SubCommand {
}

impl SubCommand {
pub(crate) async fn run<'a>(
pub(crate) async fn run(
self,
local_api: Result<&'a factorio_mods_local::API, crate::Error>,
web_api: Result<&'a factorio_mods_web::API, crate::Error>,
local_api: &factorio_mods_local::API,
web_api: &factorio_mods_web::API,
config: crate::config::Config,
prompt_override: Option<bool>,
) -> Result<(), crate::Error> {
let local_api = local_api?;
let web_api = web_api?;

crate::solve::compute_and_apply_diff(local_api, web_api, config, prompt_override).await?;

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub(crate) fn wrapping_println(s: &str, indent: &str) {
}
}

pub(crate) async fn ensure_user_credentials<'a>(
local_api: &'a factorio_mods_local::API,
web_api: &'a factorio_mods_web::API,
pub(crate) async fn ensure_user_credentials(
local_api: &factorio_mods_local::API,
web_api: &factorio_mods_web::API,
prompt_override: Option<bool>,
) -> Result<factorio_mods_common::UserCredentials, crate::Error> {
let mut existing_username = match local_api.user_credentials() {
Expand Down

0 comments on commit a2ec9f6

Please sign in to comment.