From 17ca9ffc8f72278a8c8aadf58a9fa1bd2e67a009 Mon Sep 17 00:00:00 2001 From: Mark Grey Date: Fri, 7 Jun 2024 12:09:31 -0400 Subject: [PATCH] Unnest inner error before serialize --- Cargo.lock | 2 +- src/cli.rs | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 36e0d0a..cb4101c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -364,7 +364,7 @@ checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" [[package]] name = "nektar" -version = "0.0.5" +version = "0.0.6" dependencies = [ "assert_cmd", "clap", diff --git a/src/cli.rs b/src/cli.rs index 55f8e7a..ac19de9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,3 +1,5 @@ +use std::collections::BTreeMap; + use clap::{Parser, Subcommand, ValueEnum}; use nektar::ThriftHiveMetastoreSyncClient; @@ -38,7 +40,7 @@ pub struct Cli { command: Commands, } -#[derive(ValueEnum, Debug, Clone)] +#[derive(ValueEnum, Debug, Copy, Clone)] pub enum Format { Json, #[cfg(feature = "yaml")] @@ -60,24 +62,34 @@ pub enum Commands { DropTable(DropTable), } +//TODO: refactor this, hopefully without type erasure fn serialize(f: Format, v: Result) -> Result { match f { - Format::Json => Ok(serde_json::to_string(&v)?), + Format::Json => match &v { + Ok(t) => Ok(serde_json::to_string(&t)?), + Err(e) => Ok(serde_json::to_string(&BTreeMap::from([("error", e)]))?), + }, #[cfg(feature = "yaml")] - Format::Yaml => Ok(serde_yaml::to_string(&v)?), + Format::Yaml => match &v { + Ok(t) => Ok(serde_yaml::to_string(&t)?), + Err(e) => Ok(serde_yaml::to_string(&BTreeMap::from([("error", e)]))?), + }, } } impl Cli { - pub fn run(self) -> Result { + fn client(&self) -> Result { let mut c = TTcpChannel::new(); c.open(&self.metastore_url)?; let (i_chan, o_chan) = c.split()?; let i_prot = TBinaryInputProtocol::new(TBufferedReadTransport::new(i_chan), true); let o_prot = TBinaryOutputProtocol::new(TBufferedWriteTransport::new(o_chan), true); - let client = ThriftHiveMetastoreSyncClient::new(i_prot, o_prot); + Ok(ThriftHiveMetastoreSyncClient::new(i_prot, o_prot)) + } + pub fn run(self) -> Result { + let client = self.client()?; match self.command { Commands::GetTable(get_table) => serialize(self.format, get_table.run(client)), Commands::GetCatalog(get_catalog) => serialize(self.format, get_catalog.run(client)),