Skip to content

Commit c851f3f

Browse files
Move process_command_result to separate file (#3777)
<!-- Reference any GitHub issues resolved by this PR --> Closes # ## Introduced changes Move `process_command_result` to unclutter `main.rs` 🙏 ## Checklist <!-- Make sure all of these are complete --> - [ ] Linked relevant issue - [ ] Updated relevant documentation - [ ] Added relevant tests - [x] Performed self-review of the code - [ ] Added changes to `CHANGELOG.md`
1 parent 42d7c85 commit c851f3f

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
use crate::response::{
2+
cast_message::SncastMessage, command::CommandResponse, errors::ResponseError,
3+
explorer_link::ExplorerLinksMessage,
4+
};
5+
use anyhow::Result;
6+
use foundry_ui::{Message, UI};
7+
8+
use std::process::ExitCode;
9+
10+
pub fn process_command_result<T>(
11+
command: &str,
12+
result: Result<T>,
13+
ui: &UI,
14+
block_explorer_link: Option<ExplorerLinksMessage>,
15+
) -> ExitCode
16+
where
17+
T: CommandResponse,
18+
SncastMessage<T>: Message,
19+
{
20+
let cast_msg = result.map(|command_response| SncastMessage {
21+
command: command.to_string(),
22+
command_response,
23+
});
24+
25+
match cast_msg {
26+
Ok(response) => {
27+
ui.println(&response);
28+
if let Some(link) = block_explorer_link {
29+
ui.println(&link);
30+
}
31+
ExitCode::SUCCESS
32+
}
33+
Err(err) => {
34+
let err = ResponseError::new(command.to_string(), format!("{err:#}"));
35+
ui.eprintln(&err);
36+
ExitCode::FAILURE
37+
}
38+
}
39+
}

crates/sncast/src/helpers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
pub mod account;
22
pub mod block_explorer;
33
pub mod braavos;
4+
pub mod command;
45
pub mod config;
56
pub mod configuration;
67
pub mod constants;

crates/sncast/src/main.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,17 @@ use data_transformer::transform;
1515
use foundry_ui::components::warning::WarningMessage;
1616
use foundry_ui::{Message, UI};
1717
use shared::auto_completions::{Completions, generate_completions};
18+
use sncast::helpers::command::process_command_result;
1819
use sncast::helpers::config::{combine_cast_configs, get_global_config_path};
1920
use sncast::helpers::configuration::CastConfig;
2021
use sncast::helpers::constants::DEFAULT_ACCOUNTS_FILE;
2122
use sncast::helpers::output_format::output_format_from_json_flag;
2223
use sncast::helpers::scarb_utils::{
2324
BuildConfig, assert_manifest_path_exists, build_and_load_artifacts, get_package_metadata,
2425
};
25-
use sncast::response::cast_message::SncastMessage;
26-
use sncast::response::command::CommandResponse;
2726
use sncast::response::declare::DeclareResponse;
28-
use sncast::response::errors::ResponseError;
2927
use sncast::response::errors::handle_starknet_command_error;
30-
use sncast::response::explorer_link::{ExplorerLinksMessage, block_explorer_link_if_allowed};
28+
use sncast::response::explorer_link::block_explorer_link_if_allowed;
3129
use sncast::response::transformed_call::transform_response;
3230
use sncast::{
3331
ValidatedWaitParams, WaitForTx, get_account, get_block_id, get_class_hash_by_address,
@@ -631,31 +629,3 @@ fn get_cast_config(cli: &Cli, ui: &UI) -> Result<CastConfig> {
631629
config_with_cli(&mut combined_config, cli);
632630
Ok(combined_config)
633631
}
634-
635-
fn process_command_result<T>(
636-
command: &str,
637-
result: Result<T>,
638-
ui: &UI,
639-
block_explorer_link: Option<ExplorerLinksMessage>,
640-
) where
641-
T: CommandResponse,
642-
SncastMessage<T>: Message,
643-
{
644-
let cast_msg = result.map(|command_response| SncastMessage {
645-
command: command.to_string(),
646-
command_response,
647-
});
648-
649-
match cast_msg {
650-
Ok(response) => {
651-
ui.println(&response);
652-
if let Some(link) = block_explorer_link {
653-
ui.println(&link);
654-
}
655-
}
656-
Err(err) => {
657-
let err = ResponseError::new(command.to_string(), format!("{err:#}"));
658-
ui.eprintln(&err);
659-
}
660-
}
661-
}

0 commit comments

Comments
 (0)