Skip to content

Commit e36dd74

Browse files
Refactor sncast messages json (#3822)
<!-- Reference any GitHub issues resolved by this PR --> Closes # ## Introduced changes <!-- A brief description of the changes --> - ## Checklist <!-- Make sure all of these are complete --> - [ ] Linked relevant issue - [ ] Updated relevant documentation - [ ] Added relevant tests - [ ] Performed self-review of the code - [ ] Added changes to `CHANGELOG.md`
1 parent ff9fda4 commit e36dd74

File tree

19 files changed

+70
-263
lines changed

19 files changed

+70
-263
lines changed

crates/sncast/src/response/account/create.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
use crate::response::cast_message::SncastMessage;
1+
use crate::response::cast_message::{SncastCommandMessage, SncastMessage};
22
use crate::{
33
helpers::block_explorer::LinkProvider,
44
response::{command::CommandResponse, explorer_link::OutputLink},
55
};
66
use conversions::padded_felt::PaddedFelt;
77
use conversions::string::IntoPaddedHexStr;
8-
use foundry_ui::Message;
98
use foundry_ui::styling;
109
use serde::{Serialize, Serializer};
11-
use serde_json::Value;
12-
use serde_json::json;
1310

1411
fn as_str<S>(value: &u128, serializer: S) -> Result<S::Ok, S::Error>
1512
where
@@ -29,7 +26,7 @@ pub struct AccountCreateResponse {
2926

3027
impl CommandResponse for AccountCreateResponse {}
3128

32-
impl Message for SncastMessage<AccountCreateResponse> {
29+
impl SncastCommandMessage for SncastMessage<AccountCreateResponse> {
3330
fn text(&self) -> String {
3431
styling::OutputBuilder::new()
3532
.success_message("Account created")
@@ -46,16 +43,6 @@ impl Message for SncastMessage<AccountCreateResponse> {
4643
.text_field(&self.command_response.message)
4744
.build()
4845
}
49-
50-
fn json(&self) -> Value {
51-
serde_json::to_value(&self.command_response).unwrap_or_else(|err| {
52-
json!({
53-
"error": "Failed to serialize response",
54-
"command": self.command,
55-
"details": err.to_string()
56-
})
57-
})
58-
}
5946
}
6047

6148
impl OutputLink for AccountCreateResponse {
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
use crate::response::cast_message::SncastCommandMessage;
12
use crate::response::cast_message::SncastMessage;
23
use crate::response::command::CommandResponse;
3-
use foundry_ui::Message;
44
use foundry_ui::styling;
55
use serde::Serialize;
6-
use serde_json::Value;
7-
use serde_json::json;
86

97
#[derive(Serialize, Clone)]
108
pub struct AccountDeleteResponse {
@@ -13,22 +11,12 @@ pub struct AccountDeleteResponse {
1311

1412
impl CommandResponse for AccountDeleteResponse {}
1513

16-
impl Message for SncastMessage<AccountDeleteResponse> {
14+
impl SncastCommandMessage for SncastMessage<AccountDeleteResponse> {
1715
fn text(&self) -> String {
1816
styling::OutputBuilder::new()
1917
.success_message("Account deleted")
2018
.blank_line()
2119
.text_field(&self.command_response.result)
2220
.build()
2321
}
24-
25-
fn json(&self) -> Value {
26-
serde_json::to_value(&self.command_response).unwrap_or_else(|err| {
27-
json!({
28-
"error": "Failed to serialize response",
29-
"command": self.command,
30-
"details": err.to_string()
31-
})
32-
})
33-
}
3422
}

crates/sncast/src/response/account/deploy.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
use crate::response::cast_message::SncastMessage;
1+
use crate::response::cast_message::{SncastCommandMessage, SncastMessage};
22
use crate::{
33
helpers::block_explorer::LinkProvider,
44
response::{command::CommandResponse, explorer_link::OutputLink, invoke::InvokeResponse},
55
};
66
use conversions::string::IntoHexStr;
77
use conversions::{padded_felt::PaddedFelt, serde::serialize::CairoSerialize};
8-
use foundry_ui::Message;
98
use foundry_ui::styling;
109
use serde::{Deserialize, Serialize};
11-
use serde_json::Value;
12-
use serde_json::json;
10+
1311
#[derive(Serialize, Deserialize, CairoSerialize, Clone, Debug, PartialEq)]
1412
pub struct AccountDeployResponse {
1513
pub transaction_hash: PaddedFelt,
1614
}
1715

1816
impl CommandResponse for AccountDeployResponse {}
17+
1918
impl From<InvokeResponse> for AccountDeployResponse {
2019
fn from(value: InvokeResponse) -> Self {
2120
Self {
@@ -24,7 +23,7 @@ impl From<InvokeResponse> for AccountDeployResponse {
2423
}
2524
}
2625

27-
impl Message for SncastMessage<AccountDeployResponse> {
26+
impl SncastCommandMessage for SncastMessage<AccountDeployResponse> {
2827
fn text(&self) -> String {
2928
styling::OutputBuilder::new()
3029
.success_message("Account deployed")
@@ -35,16 +34,6 @@ impl Message for SncastMessage<AccountDeployResponse> {
3534
)
3635
.build()
3736
}
38-
39-
fn json(&self) -> Value {
40-
serde_json::to_value(&self.command_response).unwrap_or_else(|err| {
41-
json!({
42-
"error": "Failed to serialize response",
43-
"command": self.command,
44-
"details": err.to_string()
45-
})
46-
})
47-
}
4837
}
4938

5039
impl OutputLink for AccountDeployResponse {
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1+
use crate::response::cast_message::SncastCommandMessage;
12
use crate::response::cast_message::SncastMessage;
23
use crate::response::command::CommandResponse;
3-
use foundry_ui::Message;
44
use foundry_ui::styling;
55
use serde::Serialize;
6-
use serde_json::Value;
7-
use serde_json::json;
86

97
#[derive(Serialize, Clone)]
108
pub struct AccountImportResponse {
@@ -14,7 +12,7 @@ pub struct AccountImportResponse {
1412

1513
impl CommandResponse for AccountImportResponse {}
1614

17-
impl Message for SncastMessage<AccountImportResponse> {
15+
impl SncastCommandMessage for SncastMessage<AccountImportResponse> {
1816
fn text(&self) -> String {
1917
styling::OutputBuilder::new()
2018
.success_message("Account imported successfully")
@@ -26,14 +24,4 @@ impl Message for SncastMessage<AccountImportResponse> {
2624
)
2725
.build()
2826
}
29-
30-
fn json(&self) -> Value {
31-
serde_json::to_value(&self.command_response).unwrap_or_else(|err| {
32-
json!({
33-
"error": "Failed to serialize response",
34-
"command": self.command,
35-
"details": err.to_string()
36-
})
37-
})
38-
}
3927
}

crates/sncast/src/response/call.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
use super::command::CommandResponse;
2+
use crate::response::cast_message::SncastCommandMessage;
23
use crate::response::cast_message::SncastMessage;
34
use conversions::serde::serialize::CairoSerialize;
45
use conversions::string::IntoHexStr;
5-
use foundry_ui::Message;
66
use foundry_ui::styling;
77
use serde::Serialize;
8-
use serde_json::Value;
9-
use serde_json::json;
108
use starknet_types_core::felt::Felt;
119

1210
#[derive(Serialize, CairoSerialize, Clone)]
@@ -16,7 +14,7 @@ pub struct CallResponse {
1614

1715
impl CommandResponse for CallResponse {}
1816

19-
impl Message for SncastMessage<CallResponse> {
17+
impl SncastCommandMessage for SncastMessage<CallResponse> {
2018
fn text(&self) -> String {
2119
let response_values = self
2220
.command_response
@@ -32,14 +30,4 @@ impl Message for SncastMessage<CallResponse> {
3230
.field("Response", &format!("[{response_values}]"))
3331
.build()
3432
}
35-
36-
fn json(&self) -> Value {
37-
serde_json::to_value(&self.command_response).unwrap_or_else(|err| {
38-
json!({
39-
"error": "Failed to serialize response",
40-
"command": self.command,
41-
"details": err.to_string()
42-
})
43-
})
44-
}
4533
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
1+
use foundry_ui::Message;
12
use serde::Serialize;
3+
use serde_json::Value;
24

35
use super::command::CommandResponse;
46
#[derive(Serialize)]
57
pub struct SncastMessage<T: CommandResponse> {
68
pub command: String,
79
pub command_response: T,
810
}
11+
12+
pub trait SncastCommandMessage {
13+
fn text(&self) -> String;
14+
}
15+
16+
impl<T: CommandResponse> Message for SncastMessage<T>
17+
where
18+
SncastMessage<T>: SncastCommandMessage,
19+
{
20+
fn text(&self) -> String {
21+
SncastCommandMessage::text(self)
22+
}
23+
24+
fn json(&self) -> Value {
25+
serde_json::to_value(&self.command_response).unwrap_or_else(|err| {
26+
serde_json::json!({
27+
"error": "Failed to serialize response",
28+
"command": self.command,
29+
"details": err.to_string()
30+
})
31+
})
32+
}
33+
}
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
use crate::response::cast_message::SncastCommandMessage;
2+
use crate::response::{cast_message::SncastMessage, command::CommandResponse};
13
use conversions::padded_felt::PaddedFelt;
24
use conversions::{serde::serialize::CairoSerialize, string::IntoPaddedHexStr};
3-
use foundry_ui::{Message, styling};
5+
use foundry_ui::styling;
46
use serde::{Deserialize, Serialize};
5-
use serde_json::json;
6-
7-
use crate::response::{cast_message::SncastMessage, command::CommandResponse};
87

98
#[derive(Clone, Serialize, Deserialize, CairoSerialize, Debug, PartialEq)]
109
pub struct ClassHashResponse {
@@ -13,7 +12,7 @@ pub struct ClassHashResponse {
1312

1413
impl CommandResponse for ClassHashResponse {}
1514

16-
impl Message for SncastMessage<ClassHashResponse> {
15+
impl SncastCommandMessage for SncastMessage<ClassHashResponse> {
1716
fn text(&self) -> String {
1817
styling::OutputBuilder::new()
1918
.field(
@@ -22,14 +21,4 @@ impl Message for SncastMessage<ClassHashResponse> {
2221
)
2322
.build()
2423
}
25-
26-
fn json(&self) -> serde_json::Value {
27-
serde_json::to_value(&self.command_response).unwrap_or_else(|err| {
28-
json!({
29-
"error": "Failed to serialize response",
30-
"command": self.command,
31-
"details": err.to_string()
32-
})
33-
})
34-
}
3524
}

crates/sncast/src/response/declare.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::{command::CommandResponse, explorer_link::OutputLink};
22
use crate::helpers::block_explorer::LinkProvider;
3-
use crate::response::cast_message::SncastMessage;
3+
use crate::response::cast_message::{SncastCommandMessage, SncastMessage};
44
use anyhow::Error;
55
use camino::Utf8PathBuf;
66
use conversions::string::IntoHexStr;
@@ -10,7 +10,6 @@ use foundry_ui::styling;
1010
use indoc::formatdoc;
1111
use serde::{Deserialize, Serialize};
1212
use serde_json::Value;
13-
use serde_json::json;
1413
use starknet::core::types::contract::{AbiConstructor, AbiEntry};
1514
use starknet_types_core::felt::Felt;
1615
use std::fmt::Write;
@@ -23,7 +22,7 @@ pub struct DeclareTransactionResponse {
2322

2423
impl CommandResponse for DeclareTransactionResponse {}
2524

26-
impl Message for SncastMessage<DeclareTransactionResponse> {
25+
impl SncastCommandMessage for SncastMessage<DeclareTransactionResponse> {
2726
fn text(&self) -> String {
2827
styling::OutputBuilder::new()
2928
.success_message("Declaration completed")
@@ -38,16 +37,6 @@ impl Message for SncastMessage<DeclareTransactionResponse> {
3837
)
3938
.build()
4039
}
41-
42-
fn json(&self) -> Value {
43-
serde_json::to_value(&self.command_response).unwrap_or_else(|err| {
44-
json!({
45-
"error": "Failed to serialize response",
46-
"command": self.command,
47-
"details": err.to_string()
48-
})
49-
})
50-
}
5140
}
5241

5342
#[derive(Clone, Serialize, Deserialize, CairoSerialize, Debug, PartialEq)]

crates/sncast/src/response/deploy.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
use super::command::CommandResponse;
22
use crate::helpers::block_explorer::LinkProvider;
3-
use crate::response::cast_message::SncastMessage;
3+
use crate::response::cast_message::{SncastCommandMessage, SncastMessage};
44
use crate::response::declare::DeclareTransactionResponse;
55
use crate::response::explorer_link::OutputLink;
66
use conversions::string::IntoPaddedHexStr;
77
use conversions::{padded_felt::PaddedFelt, serde::serialize::CairoSerialize};
8-
use foundry_ui::Message;
98
use foundry_ui::styling;
109
use indoc::formatdoc;
1110
use serde::{Deserialize, Serialize};
12-
use serde_json::{Value, json};
1311

1412
#[derive(Clone, Serialize, Deserialize, CairoSerialize, Debug, PartialEq)]
1513
#[serde(untagged)]
@@ -20,23 +18,13 @@ pub enum DeployResponse {
2018

2119
impl CommandResponse for DeployResponse {}
2220

23-
impl Message for SncastMessage<DeployResponse> {
21+
impl SncastCommandMessage for SncastMessage<DeployResponse> {
2422
fn text(&self) -> String {
2523
match &self.command_response {
2624
DeployResponse::Standard(response) => response.text(),
2725
DeployResponse::WithDeclare(response) => response.text(),
2826
}
2927
}
30-
31-
fn json(&self) -> Value {
32-
serde_json::to_value(&self.command_response).unwrap_or_else(|err| {
33-
json!({
34-
"error": "Failed to serialize response",
35-
"command": self.command,
36-
"details": err.to_string()
37-
})
38-
})
39-
}
4028
}
4129

4230
impl OutputLink for DeployResponse {

0 commit comments

Comments
 (0)