Skip to content

Commit

Permalink
refactor app (#335)
Browse files Browse the repository at this point in the history
* move app crate into bin
  • Loading branch information
Autoparallel authored Feb 15, 2024
1 parent 6dd0871 commit 1f4804c
Show file tree
Hide file tree
Showing 71 changed files with 570 additions and 565 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/book.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Install rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: 1.73.0
toolchain: 1.75.0
override: true

- name: Install mdbook-katex
Expand All @@ -46,4 +46,4 @@ jobs:
- name: Run tests
run: |
cd documentation
mdbook test
mdbook test
79 changes: 70 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[workspace]
members = ["crates/*", "documentation"]

[package]
name = "excalibur"
version = "0.2.0"
Expand All @@ -9,19 +12,17 @@ authors = ["Primitive Bits, Inc."]
name = "excalibur"
path = "bin/src/main.rs"

[workspace]
members = ["crates/*", "documentation", "bin"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[workspace.dependencies]
# Local
bindings = { path = "./crates/bindings", version = "0.1.0" }
analysis = { path = "./crates/analysis", version = "0.1.0" }
cfmm_math = { path = "./crates/cfmm_math", version = "0.1.0" }
clients = { path = "./crates/clients", version = "0.1.0" }
sim = { path = "./crates/sim", version = "0.1.0" }
datatypes = { path = "./crates/datatypes", version = "0.1.0" }
cfmm_math = { path = "./crates/cfmm_math", version = "0.1.0" }

# External
arbiter-bindings = { version = "0.1.0" }
Expand All @@ -48,10 +49,8 @@ alloy-rlp = "0.3"
alloy-pubsub = { git = "https://github.com/alloy-rs/alloy.git", rev = "0ba6b61" }
alloy-signer = { git = "https://github.com/alloy-rs/alloy.git", rev = "0ba6b61" }


reqwest = "=0.11.23"


## Config and parsing
serde = "=1.0.192"
serde_json = "=1.0.108"
Expand Down Expand Up @@ -93,14 +92,14 @@ criterion = "=0.5.1"
chrono = { version = "=0.4.31", features = ["serde"] }

[dependencies]
# main excalibur application
app = { path = "./crates/app", version = "0.1.0" }
sim = { path = "./crates/sim", version = "0.1.0" }
datatypes.workspace = true
clients.workspace = true
cfmm_math.workspace = true
sim.workspace = true

clap = { version = "=4.4.14", features = ["derive"] }

criterion.workspace = true
cfmm_math.workspace = true
analysis.workspace = true

tokio.workspace = true
Expand All @@ -112,6 +111,68 @@ dotenv.workspace = true
RustQuant.workspace = true
statrs.workspace = true

# crates


# deps
iced = { version = "=0.10.0", features = [
"system",
"tokio",
"lazy",
"image",
"palette",
"advanced",
"canvas",
"svg",
] }
iced_aw = { version = "=0.7.0", default-features = false, features = [
"menu",
"icon_text",
"icons",
"spinner",
"floating_element",
] }
plotters-backend = "=0.3.5"
plotters-iced = "=0.9.0"
iced-loading-indicator = { git = "https://github.com/BB-301/iced-loading-indicator.git", rev = "a09296f" }
url = "=2.5.0"

# workspace crates

# revm
revm.workspace = true
revm-primitives.workspace = true

# alloy
alloy-primitives.workspace = true
alloy-sol-types.workspace = true
alloy-providers.workspace = true
alloy-networks.workspace = true
alloy-rpc-client.workspace = true
alloy-rpc-types.workspace = true
alloy-transport-ws.workspace = true
alloy-rlp.workspace = true
alloy-pubsub.workspace = true
alloy-transport-http.workspace = true
alloy-signer.workspace = true
reqwest.workspace = true

# workspace deps
chrono.workspace = true
arbiter-bindings.workspace = true
bindings.workspace = true
async-trait.workspace = true
reikna.workspace = true
arbiter-core.workspace = true
ethers.workspace = true
directories-next.workspace = true
plotters.workspace = true
tokio-util.workspace = true
serde.workspace = true
serde_json.workspace = true
uuid.workspace = true


[dev-dependencies]
# For book
mdbook = "0.4.35"
Expand Down
1 change: 0 additions & 1 deletion bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ authors = ["Primitive Bits, Inc."]
[dependencies]

# main excalibur application
app = { path = "../crates/app", version = "0.1.0" }
sim = { path = "../crates/sim", version = "0.1.0" }

clap = { version = "=4.4.14", features = ["derive"] }
Expand Down
66 changes: 36 additions & 30 deletions crates/app/src/app.rs → bin/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub fn app_span() -> Span {

/// Root message for the Application.
#[derive(Debug, Default)]
pub enum Message {
pub enum AppMessage {
/// An empty message used as a default.
#[default]
Empty,
Expand All @@ -72,7 +72,7 @@ pub enum Message {
/// Exits the application immediately.
QuitReady,
/// All children controllers wrap their messages in View.
View(view::Message),
View(view::ViewMessage),
/// Modifications to the persistent user profile.
UpdateUser(UserProfileMessage),
/// Switches the active "app" to the target Route.
Expand Down Expand Up @@ -100,14 +100,14 @@ pub enum UserProfileMessage {
ClearRPCs,
}

pub type RootMessage = Message;
pub type RootViewMessage = view::Message;
pub type RootMessage = AppMessage;
pub type RootViewMessage = view::ViewMessage;

impl MessageWrapper for Message {
type ParentMessage = Message;
impl MessageWrapper for AppMessage {
type ParentMessage = AppMessage;
}

impl From<UserProfileMessage> for Message {
impl From<UserProfileMessage> for AppMessage {
fn from(message: UserProfileMessage) -> Self {
Self::UpdateUser(message)
}
Expand Down Expand Up @@ -196,7 +196,7 @@ impl App {
pub fn new(
model: Model,
client: Arc<ExcaliburMiddleware<Ws, LocalWallet>>,
) -> (Self, Command<Message>) {
) -> (Self, Command<AppMessage>) {
let dashboard = PortfolioRoot::new(Some(client.clone()), model.clone()).into();
let mut sidebar = Sidebar::new();
sidebar.page = view::sidebar::Page::Portfolio;
Expand All @@ -207,14 +207,14 @@ impl App {
windows: Windows::new(dashboard, sidebar),
app_clock: AppClock::new(),
},
Command::perform(async {}, |_| Message::Load),
Command::perform(async {}, |_| AppMessage::Load),
)
}

/// This function is responsible for loading the sidebar and the default
/// screen. It is called when a user starts the application, after the
/// new() function.
pub fn load(&mut self) -> Command<Message> {
pub fn load(&mut self) -> Command<AppMessage> {
// Load the sidebar and the current window.
let cmds = vec![
self.windows.sidebar.load().map(|x| x.into()),
Expand Down Expand Up @@ -245,36 +245,36 @@ impl App {
///
/// This function will panic if the `Message::ModelSyncResult` variant is
/// used and the result is an error.
pub fn update(&mut self, message: Message) -> Command<Message> {
pub fn update(&mut self, message: AppMessage) -> Command<AppMessage> {
// Handle the update clock first.
self.app_clock.update();

// Handle the update.
app_span().in_scope(|| match message {
Message::Load => self.load(),
Message::QuitReady => {
AppMessage::Load => self.load(),
AppMessage::QuitReady => {
// Caught by the lib.rs and exits the application.
Command::none()
}
Message::ModelSyncResult(Ok(model)) => {
AppMessage::ModelSyncResult(Ok(model)) => {
// Update the root model.
self.model = model.clone();

// Propagate the model to the active screen.
// todo: remove side effects, @alex what did you mean by this?
self.windows
.screen
.update(Message::ModelSyncResult(Ok(model)))
.update(AppMessage::ModelSyncResult(Ok(model)))
}
Message::ModelSyncResult(Err(e)) => {
AppMessage::ModelSyncResult(Err(e)) => {
tracing::error!(
"Critical failure - sync model threw an error while updating: {:?}",
e
);
Command::none()
}
Message::UpdateUser(msg) => self.update_user(msg),
Message::View(view::Message::Root(msg)) => match msg {
AppMessage::UpdateUser(msg) => self.update_user(msg),
AppMessage::View(view::ViewMessage::Root(msg)) => match msg {
view::RootMessage::ModelSyncRequest => self.sync_model(),
view::RootMessage::Route(route) => self.switch_window(&route),
view::RootMessage::CopyToClipboard(contents) => iced::clipboard::write(contents),
Expand All @@ -285,7 +285,7 @@ impl App {
tracing::debug!("Confirming exit");
self.windows
.screen
.update(Message::View(view::Message::Root(msg)))
.update(AppMessage::View(view::ViewMessage::Root(msg)))
.map(|x| x)
}
},
Expand All @@ -306,13 +306,13 @@ impl App {
///
/// # Returns
/// * `Element<Message>` - The view of the application.
pub fn view(&self) -> Element<Message> {
pub fn view(&self) -> Element<AppMessage> {
view::app_layout(
&self.app_clock,
&self.windows.sidebar,
self.windows.screen.view(),
)
.map(Message::View)
.map(AppMessage::View)
}

/// Returns the subscription of the current screen.
Expand All @@ -324,7 +324,7 @@ impl App {
///
/// # Returns
/// * `Subscription<Message>` - The subscription of the current screen.
pub fn subscription(&self) -> Subscription<Message> {
pub fn subscription(&self) -> Subscription<AppMessage> {
self.windows.screen.subscription()
}

Expand All @@ -340,7 +340,7 @@ impl App {
/// # Returns
/// * `Command<Message>` - A batch of commands to be executed during the
/// exit process.
pub fn exit(&mut self) -> Command<Message> {
pub fn exit(&mut self) -> Command<AppMessage> {
// Save the profile to disk.
let result = self.model.save();
match result {
Expand All @@ -360,7 +360,7 @@ impl App {
save_snapshot(self.client.clone()),
UserProfileMessage::SaveAnvilSnapshot,
)
.map(Message::UpdateUser);
.map(AppMessage::UpdateUser);
commands.push(cmd);
}

Expand All @@ -378,7 +378,7 @@ impl App {
/// # Returns
/// * `Command<Message>` - A command containing the result of the model
/// synchronization.
fn sync_model(&mut self) -> Command<Message> {
fn sync_model(&mut self) -> Command<AppMessage> {
let model = self.model.clone();
let provider = self.client.get_client();
Command::perform(
Expand All @@ -387,7 +387,7 @@ impl App {
model.update(provider).await?;
Ok(model)
},
Message::ModelSyncResult,
AppMessage::ModelSyncResult,
)
}

Expand All @@ -410,7 +410,7 @@ impl App {
/// * `Command<Message>` - A command to sync the RPCs after updating the
/// user profile.
// #[allow(unused_assignments)]
fn update_user(&mut self, message: UserProfileMessage) -> Command<Message> {
fn update_user(&mut self, message: UserProfileMessage) -> Command<AppMessage> {
let model = &mut self.model;
match message {
UserProfileMessage::SaveAnvilSnapshot(snapshot) => {
Expand All @@ -428,7 +428,7 @@ impl App {
}

// Exits the application after saving the anvil snapshot.
return Command::perform(async {}, |_| Message::QuitReady);
return Command::perform(async {}, |_| AppMessage::QuitReady);
}
UserProfileMessage::AddAddress(name, address, category) => {
model.user.contacts.add(
Expand Down Expand Up @@ -468,7 +468,7 @@ impl App {

let rpcs = model.user.rpcs.clone();
Command::perform(async {}, move |_| {
view::Message::Settings(settings::Message::Rpc(settings::rpc::Message::Sync(rpcs)))
view::ViewMessage::Settings(settings::Message::Rpc(settings::rpc::Message::Sync(rpcs)))
})
.map(|x| x.into())
}
Expand All @@ -484,7 +484,7 @@ impl App {
/// window and adds it to the command vector. Finally, it batches all
/// the commands in the vector and returns them.
#[allow(unreachable_patterns)]
fn switch_window(&mut self, navigate_to: &view::sidebar::Route) -> Command<Message> {
fn switch_window(&mut self, navigate_to: &view::sidebar::Route) -> Command<AppMessage> {
let mut cmds = Vec::new();

let exit_cmd = self.windows.screen.exit();
Expand Down Expand Up @@ -542,6 +542,12 @@ pub struct Update {
pub duration: Duration,
}

impl Default for AppClock {
fn default() -> Self {
Self::new()
}
}

impl AppClock {
/// Constructor for AppClock.
pub fn new() -> Self {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 1f4804c

Please sign in to comment.