From 2fa65d002511e8c9c3515df220e85387e9237e3f Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 17 Jan 2024 15:34:50 +0100 Subject: [PATCH] patch-list: Return JSON output --- Cargo.lock | 34 ++++++++++++----------- Cargo.toml | 2 ++ bin/commands/patch.rs | 46 ++++---------------------------- bin/commands/patch/list.rs | 21 +++++++++++---- bin/commands/patch/list/event.rs | 24 +++++++++-------- 5 files changed, 54 insertions(+), 73 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 339532d2..e208f022 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -987,7 +987,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.28", + "syn 2.0.48", ] [[package]] @@ -1495,18 +1495,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.32" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1660,6 +1660,8 @@ dependencies = [ "radicle", "radicle-surf", "radicle-term", + "serde", + "serde_json", "simple-logging", "textwrap 0.16.0", "thiserror", @@ -1855,22 +1857,22 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.181" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d3e73c93c3240c0bda063c239298e633114c69a888c3e37ca8bb33f343e9890" +checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.181" +version = "1.0.195" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be02f6cb0cd3a5ec20bbcfbcbd749f57daddb1a0882dc2e46a6c236c90b977ed" +checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.28", + "syn 2.0.48", ] [[package]] @@ -2111,7 +2113,7 @@ dependencies = [ "proc-macro2", "quote", "rustversion", - "syn 2.0.28", + "syn 2.0.48", ] [[package]] @@ -2133,9 +2135,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.28" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04361975b3f5e348b2189d8dc55bc942f278b2d482a6a0365de5bdd62d351567" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -2229,7 +2231,7 @@ checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96" dependencies = [ "proc-macro2", "quote", - "syn 2.0.28", + "syn 2.0.48", ] [[package]] @@ -2484,7 +2486,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.28", + "syn 2.0.48", "wasm-bindgen-shared", ] @@ -2506,7 +2508,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.28", + "syn 2.0.48", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/Cargo.toml b/Cargo.toml index 713cd7a6..1be337a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,8 @@ radicle = { git = "https://github.com/radicle-dev/heartwood" } radicle-term = { git = "https://github.com/radicle-dev/heartwood", package = "radicle-term" } radicle-surf = { version = "0.18.0" } simple-logging = { version = "2.0.2" } +serde = { version = "1.0", features = ["derive"] } +serde_json = { version = "1.0" } timeago = { version = "0.4.1" } textwrap = { version = "0.16.0" } thiserror = { version = "1" } diff --git a/bin/commands/patch.rs b/bin/commands/patch.rs index 41967e32..7cfe5c06 100644 --- a/bin/commands/patch.rs +++ b/bin/commands/patch.rs @@ -8,7 +8,6 @@ mod select; mod suite; use std::ffi::OsString; -use std::process::Command; use anyhow::anyhow; @@ -17,8 +16,6 @@ use radicle_tui::{context, log, Window}; use crate::terminal; use crate::terminal::args::{Args, Error, Help}; -use self::list::PatchCommand; - pub const FPS: u64 = 60; pub const HELP: Help = Help { name: "patch", @@ -92,45 +89,12 @@ pub fn run(options: Options, _ctx: impl terminal::Context) -> anyhow::Result<()> log::enable(context.profile(), "patch", "list")?; let mut app = list::App::new(context); - if let Some(command) = Window::default().run(&mut app, 1000 / FPS)? { - match command { - PatchCommand::Show(id) => { - match Command::new("rad") - .arg("patch") - .arg("show") - .arg(id.to_string()) - .spawn() - { - Ok(_) => {} - Err(_) => {} - } - }, - PatchCommand::Edit(id) => { - match Command::new("rad") - .arg("patch") - .arg("edit") - .arg(id.to_string()) - .spawn() - { - Ok(_) => {} - Err(_) => {} - } - } - PatchCommand::Checkout(id) => { - match Command::new("rad") - .arg("patch") - .arg("checkout") - .arg(id.to_string()) - .spawn() - { - Ok(_) => {} - Err(_) => {} - } - } - } - } + let command = Window::default().run(&mut app, 1000 / FPS)?; + let output = command + .map(|command| serde_json::to_string(&command).unwrap_or_default()) + .unwrap_or_default(); - // eprint!("{patch_id}"); + eprint!("{output}"); } Operation::Select => { let context = context::Context::new(id)?.with_patches(); diff --git a/bin/commands/patch/list.rs b/bin/commands/patch/list.rs index ef7c3061..79c95f13 100644 --- a/bin/commands/patch/list.rs +++ b/bin/commands/patch/list.rs @@ -8,8 +8,7 @@ mod ui; use std::hash::Hash; use anyhow::Result; - -use radicle::cob::patch::PatchId; +use serde::{Serialize, Serializer}; use tuirealm::application::PollStrategy; use tuirealm::event::Key; @@ -25,10 +24,22 @@ use tui::{Exit, PageStack, Tui}; use page::ListView; #[derive(Clone, Debug, Eq, PartialEq)] +pub struct PatchId(radicle::cob::patch::PatchId); + +impl Serialize for PatchId { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&format!("{}", *self.0)) + } +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] pub enum PatchCommand { - Show(PatchId), - Edit(PatchId), - Checkout(PatchId), + Show { id: PatchId }, + Edit { id: PatchId }, + Checkout { id: PatchId }, } #[derive(Debug, Eq, PartialEq, Clone, Hash)] diff --git a/bin/commands/patch/list/event.rs b/bin/commands/patch/list/event.rs index 9d309c12..8dcfc2a2 100644 --- a/bin/commands/patch/list/event.rs +++ b/bin/commands/patch/list/event.rs @@ -58,37 +58,39 @@ impl tuirealm::Component for Widget { let item = self.items().get(selected)?; - Some(Message::Quit(Some(PatchCommand::Show( - item.id().to_owned(), - )))) + Some(Message::Quit(Some(PatchCommand::Show { + id: super::PatchId(item.id().to_owned()), + }))) } _ => None, } } Event::Keyboard(KeyEvent { - code: Key::Char('e'), .. + code: Key::Char('e'), + .. }) => { let result = self.perform(Cmd::Submit); match result { CmdResult::Submit(State::One(StateValue::Usize(selected))) => { let item = self.items().get(selected)?; - Some(Message::Quit(Some(PatchCommand::Edit( - item.id().to_owned(), - )))) + Some(Message::Quit(Some(PatchCommand::Edit { + id: super::PatchId(item.id().to_owned()), + }))) } _ => None, } } Event::Keyboard(KeyEvent { - code: Key::Char('c'), .. + code: Key::Char('c'), + .. }) => { let result = self.perform(Cmd::Submit); match result { CmdResult::Submit(State::One(StateValue::Usize(selected))) => { let item = self.items().get(selected)?; - Some(Message::Quit(Some(PatchCommand::Checkout( - item.id().to_owned(), - )))) + Some(Message::Quit(Some(PatchCommand::Checkout { + id: super::PatchId(item.id().to_owned()), + }))) } _ => None, }