Skip to content

Commit

Permalink
fix: constrain print_pretty_json with T: HasLotusJson to avoid breaka…
Browse files Browse the repository at this point in the history
…ge (#4249)
  • Loading branch information
hanabi1224 authored Apr 21, 2024
1 parent 0dc4ed6 commit 0ffc0fa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/cli/subcommands/chain_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// SPDX-License-Identifier: Apache-2.0, MIT

use crate::blocks::{Tipset, TipsetKey};
use crate::lotus_json::{HasLotusJson, LotusJson};
use crate::lotus_json::HasLotusJson;
use crate::message::ChainMessage;
use crate::rpc::{self, prelude::*};
use anyhow::bail;
use cid::Cid;
use clap::Subcommand;
use nonempty::NonEmpty;

use super::{print_pretty_json, print_rpc_res_cids};
use super::{print_pretty_lotus_json, print_rpc_res_cids};

#[derive(Debug, Subcommand)]
pub enum ChainCommands {
Expand Down Expand Up @@ -59,16 +59,22 @@ pub enum ChainCommands {
impl ChainCommands {
pub async fn run(self, client: rpc::Client) -> anyhow::Result<()> {
match self {
Self::Block { cid } => print_pretty_json(ChainGetBlock::call(&client, (cid,)).await?),
Self::Genesis => print_pretty_json(ChainGetGenesis::call_raw(&client, ()).await?),
Self::Block { cid } => {
print_pretty_lotus_json(ChainGetBlock::call(&client, (cid,)).await?)
}
Self::Genesis => print_pretty_lotus_json(ChainGetGenesis::call(&client, ()).await?),
Self::Head => print_rpc_res_cids(ChainHead::call(&client, ()).await?),
Self::Message { cid } => {
let bytes = ChainReadObj::call(&client, (cid,)).await?;
match fvm_ipld_encoding::from_slice::<ChainMessage>(&bytes)? {
ChainMessage::Unsigned(m) => print_pretty_json(LotusJson(m)),
ChainMessage::Unsigned(m) => print_pretty_lotus_json(m),
ChainMessage::Signed(m) => {
let cid = m.cid()?;
print_pretty_json(m.into_lotus_json().with_cid(cid))
println!(
"{}",
serde_json::to_string_pretty(&m.into_lotus_json().with_cid(cid))?
);
Ok(())
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/cli/subcommands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ mod sync_cmd;

use std::io::Write;

use crate::blocks::Tipset;
pub(crate) use crate::cli_shared::cli::Config;
use crate::cli_shared::cli::HELP_MESSAGE;
use crate::utils::version::FOREST_VERSION_STRING;
use crate::{blocks::Tipset, lotus_json::HasLotusJson};
use clap::Parser;
use serde::Serialize;
use tracing::error;

pub(super) use self::{
Expand Down Expand Up @@ -111,8 +110,8 @@ pub fn cli_error_and_die(msg: impl AsRef<str>, code: i32) -> ! {
}

/// Prints a pretty HTTP JSON-RPC response result
pub(super) fn print_pretty_json<T: Serialize>(obj: T) -> anyhow::Result<()> {
println!("{}", serde_json::to_string_pretty(&obj)?);
pub(super) fn print_pretty_lotus_json<T: HasLotusJson>(obj: T) -> anyhow::Result<()> {
println!("{}", serde_json::to_string_pretty(&obj.into_lotus_json())?);
Ok(())
}

Expand Down

0 comments on commit 0ffc0fa

Please sign in to comment.