Skip to content

Commit

Permalink
feat: implement display for Dated Action to see formated list of actions
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored and gitbutler-client committed Mar 12, 2024
1 parent bba4b45 commit 9b4aa23
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
33 changes: 30 additions & 3 deletions cmd/soroban-cli/src/commands/config/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,37 @@ pub fn list_ulids() -> Result<Vec<ulid::Ulid>, Error> {
list.sort();
Ok(list
.iter()
.map(|s| ulid::Ulid::from_str(s))
.map(|s| ulid::Ulid::from_str(s.trim_end_matches(".json")))
.collect::<Result<Vec<_>, _>>()?)
}

pub fn list_actions() -> Result<Vec<(ulid::Ulid, Action, Uri)>, Error> {
pub fn list_actions() -> Result<Vec<DatedAction>, Error> {
list_ulids()?
.into_iter()
.rev()
.map(|id| {
let (action, uri) = read(&id)?;
Ok((id, action, uri))
Ok(DatedAction(id, action, uri))
})
.collect::<Result<Vec<_>, Error>>()
}

pub struct DatedAction(ulid::Ulid, Action, Uri);

impl std::fmt::Display for DatedAction {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let (id, a, uri) = (&self.0, &self.1, &self.2);
let datetime = to_datatime(id).format("%b %d %H:%M");
write!(f, "{datetime} {uri} {}", a.type_str(),)
}
}

impl DatedAction {}

fn to_datatime(id: &ulid::Ulid) -> chrono::DateTime<chrono::Utc> {
chrono::DateTime::from_timestamp_millis(id.timestamp_ms().try_into().unwrap()).unwrap()
}

#[derive(Serialize, Deserialize)]
struct Data {
action: Action,
Expand All @@ -117,6 +134,16 @@ pub enum Action {
Transaction(GetTransactionResponseRaw),
}

impl Action {
pub fn type_str(&self) -> String {
match self {
Action::Simulation(_) => "Sim",
Action::Transaction(_) => "Txn",
}
.to_string()
}
}

impl From<SimulateTransactionResponse> for Action {
fn from(res: SimulateTransactionResponse) -> Self {
Self::Simulation(res)
Expand Down
32 changes: 8 additions & 24 deletions cmd/soroban-cli/src/commands/data/ls.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::command;

use crate::commands::config::data::{self, Action};

use crate::commands::config::data;
use super::super::config::locator;

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -30,31 +29,16 @@ impl Cmd {
}

pub fn ls(&self) -> Result<Vec<String>, Error> {
data::list_actions()?
Ok(data::list_actions()?
.iter()
.map(|(id, action, uri)| {
Ok(format!(
"{} {} {uri}\n",
to_datatime(id),
action_type(action)
))
})
.collect()
.map(ToString::to_string)
.collect())
}

pub fn ls_l(&self) -> Result<Vec<String>, Error> {
todo!()
}
}

fn to_datatime(id: &ulid::Ulid) -> chrono::DateTime<chrono::Utc> {
chrono::DateTime::from_timestamp_millis(id.timestamp_ms().try_into().unwrap()).unwrap()
}

fn action_type(a: &Action) -> String {
match a {
Action::Simulation(_) => "Simulation",
Action::Transaction(_) => "Transaction",
Ok(data::list_actions()?
.iter()
.map(ToString::to_string)
.collect())
}
.to_string()
}
22 changes: 22 additions & 0 deletions cmd/soroban-cli/src/commands/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
use clap::Parser;
pub mod ls;

#[derive(Debug, Parser)]
pub enum Cmd {
/// List identities
Ls(ls::Cmd),
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Ls(#[from] ls::Error),
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
match self {
Cmd::Ls(cmd) => cmd.run()?,
};
Ok(())
}
}
6 changes: 6 additions & 0 deletions cmd/soroban-cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Root {
Cmd::Version(version) => version.run(),
Cmd::Keys(id) => id.run().await?,
Cmd::Config(c) => c.run().await?,
Cmd::Data(data) => data.run()?,
};
Ok(())
}
Expand Down Expand Up @@ -138,6 +139,9 @@ pub enum Cmd {
Network(network::Cmd),
/// Print version information
Version(version::Cmd),
/// Access cached data
#[command(subcommand)]
Data(data::Cmd),
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -159,6 +163,8 @@ pub enum Error {
Plugin(#[from] plugin::Error),
#[error(transparent)]
Network(#[from] network::Error),
#[error(transparent)]
Data(#[from] data::Error),
}

#[async_trait]
Expand Down

0 comments on commit 9b4aa23

Please sign in to comment.