Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unnest inner error before serialize #38

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::BTreeMap;

use clap::{Parser, Subcommand, ValueEnum};

use nektar::ThriftHiveMetastoreSyncClient;
Expand Down Expand Up @@ -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")]
Expand All @@ -60,24 +62,34 @@ pub enum Commands {
DropTable(DropTable),
}

//TODO: refactor this, hopefully without type erasure
fn serialize<T: Serialize>(f: Format, v: Result<T, CliError>) -> Result<String, CliError> {
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<String, CliError> {
fn client(&self) -> Result<MetastoreClient, CliError> {
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<String, CliError> {
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)),
Expand Down
Loading