Skip to content

Commit 7d1c6a1

Browse files
Foundry UI (#3392)
<!-- Reference any GitHub issues resolved by this PR --> Towards #3022 **Stack**: - #3409 - #3405 - #3402 - #3396 - #3393 - #3392 ⬅ ## Introduced changes - Introduce `foundry-ui` crate - Add `UI`, `Message` ## Checklist <!-- Make sure all of these are complete --> - [x] Linked relevant issue - [x] Updated relevant documentation - [x] Added relevant tests - [x] Performed self-review of the code - [x] Added changes to `CHANGELOG.md`
1 parent f06654a commit 7d1c6a1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+769
-440
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ members = [
1616
"crates/docs",
1717
"crates/debugging",
1818
"crates/testing/packages_validation",
19+
"crates/foundry-ui",
1920
]
2021

2122
exclude = ["crates/snforge-scarb-plugin"]

crates/forge-runner/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub fn maybe_save_trace_and_profile(
7171
let name = sanitize_filename::sanitize(name.replace("::", "_"));
7272
let trace_path = save_trace_data(&name, trace_data)?;
7373
if execution_data_to_save.profile {
74+
// TODO(#3395): Use Ui spinner
7475
let _spinner = Spinner::create_with_message("Running cairo-profiler");
7576
run_profiler(&name, &trace_path, &execution_data_to_save.additional_args)?;
7677
}
@@ -88,6 +89,7 @@ pub fn maybe_generate_coverage(
8889
if saved_trace_data_paths.is_empty() {
8990
print_as_warning(&anyhow!("No trace data to generate coverage from"));
9091
} else {
92+
// TODO(#3395): Use Ui spinner
9193
let _spinner = Spinner::create_with_message("Running cairo-coverage");
9294
run_coverage(
9395
saved_trace_data_paths,

crates/foundry-ui/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "foundry-ui"
3+
version.workspace = true
4+
edition.workspace = true
5+
6+
[dependencies]
7+
serde.workspace = true
8+
serde_json.workspace = true
9+
anyhow.workspace = true
10+
starknet-types-core.workspace = true

crates/foundry-ui/src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
pub use message::*;
2+
3+
pub mod message;
4+
5+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
6+
pub enum OutputFormat {
7+
#[default]
8+
Human,
9+
Json,
10+
}
11+
12+
/// An abstraction around console output which stores preferences for output format (human vs JSON),
13+
/// colour, etc.
14+
///
15+
/// All messaging (basically all writes to `stdout`) must go through this object.
16+
#[derive(Debug, Default)]
17+
pub struct UI {
18+
output_format: OutputFormat,
19+
// TODO(#3395): Add state here, that can be used for spinner
20+
}
21+
22+
impl UI {
23+
/// Create a new [`UI`] instance configured with the given output format.
24+
#[must_use]
25+
pub fn new(output_format: OutputFormat) -> Self {
26+
Self { output_format }
27+
}
28+
29+
// TODO: Will be removed in 3022-3-use-foundry-ui-in-sncast
30+
/// Get the output format of this [`UI`] instance.
31+
#[must_use]
32+
pub fn output_format(&self) -> OutputFormat {
33+
self.output_format
34+
}
35+
36+
/// Create a [`String`] representation of the given message based on the configured output format.
37+
fn format_message(&self, message: &impl Message) -> String {
38+
match self.output_format {
39+
OutputFormat::Human => message.text(),
40+
OutputFormat::Json => message.json().to_string(),
41+
}
42+
}
43+
44+
/// Print the given message to stdout using the configured output format.
45+
pub fn println(&self, message: &impl Message) {
46+
println!("{}", self.format_message(message));
47+
}
48+
49+
/// Print the given message to stderr using the configured output format.
50+
pub fn eprintln(&self, message: &impl Message) {
51+
eprintln!("{}", self.format_message(message));
52+
}
53+
}

crates/foundry-ui/src/message.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use serde::Serialize;
2+
use serde_json::{Value, json};
3+
4+
/// A typed object that can be either printed as a human-readable message or serialized as JSON.
5+
pub trait Message: Serialize {
6+
/// Return textual (human) representation of this message.
7+
fn text(&self) -> String;
8+
9+
/// Return JSON representation of this message.
10+
fn json(&self) -> Value;
11+
}
12+
13+
impl<T: ToString> Message for T
14+
where
15+
T: Serialize,
16+
{
17+
fn text(&self) -> String {
18+
self.to_string()
19+
}
20+
21+
fn json(&self) -> Value {
22+
json!({ "message": self.to_string() })
23+
}
24+
}

crates/foundry-ui/src/output.rs

Whitespace-only changes.

crates/sncast/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dirs.workspace = true
5555
dialoguer.workspace = true
5656
toml_edit.workspace = true
5757
num-traits.workspace = true
58+
foundry-ui = { path = "../foundry-ui" }
5859

5960
[dev-dependencies]
6061
ctor.workspace = true

crates/sncast/src/helpers/block_explorer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ mod tests {
132132
use crate::{
133133
Network,
134134
helpers::block_explorer::Service,
135-
response::{explorer_link::OutputLink, structs::DeployResponse},
135+
response::{deploy::DeployResponse, explorer_link::OutputLink},
136136
};
137137
use conversions::padded_felt::PaddedFelt;
138138
use regex::Regex;

crates/sncast/src/helpers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ pub mod configuration;
66
pub mod constants;
77
pub mod fee;
88
pub mod interactive;
9+
pub mod output_format;
910
pub mod rpc;
1011
pub mod scarb_utils;

0 commit comments

Comments
 (0)