From 72344e8b4cba546e9432d29dc7fe10661e6fc6b2 Mon Sep 17 00:00:00 2001 From: Daniel Vigovszky Date: Wed, 4 Dec 2024 14:12:50 +0100 Subject: [PATCH] Make it possible to satisfy Vec: PrintRes in external crates (#1124) * Make it possible to satisfy Vec: PrintRes in external crates * Clippy --- golem-cli/src/command/plugin.rs | 2 +- golem-cli/src/model/text.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/golem-cli/src/command/plugin.rs b/golem-cli/src/command/plugin.rs index 057da71f87..e6cf01ec34 100644 --- a/golem-cli/src/command/plugin.rs +++ b/golem-cli/src/command/plugin.rs @@ -107,10 +107,10 @@ impl PluginSubcommand { components: Arc + Send + Sync>, ) -> Result where - Vec: PrintRes, PluginScopeRef: PluginScopeArgs, ::ComponentRef: ComponentRefSplit + Send + Sync, + Vec: PrintRes, { match self { PluginSubcommand::List { scope } => { diff --git a/golem-cli/src/model/text.rs b/golem-cli/src/model/text.rs index 6de8e2004f..89a2188ab6 100644 --- a/golem-cli/src/model/text.rs +++ b/golem-cli/src/model/text.rs @@ -24,6 +24,18 @@ pub mod fmt { fn print(&self); } + pub trait TableWrapper: Sized { + type Table: TextFormat; + fn from_vec(vec: &[Self]) -> Self::Table; + } + + impl TextFormat for Vec { + fn print(&self) { + let table = T::from_vec(self); + table.print(); + } + } + pub trait MessageWithFields { fn message(&self) -> String; fn fields(&self) -> Vec<(String, String)>; @@ -1330,7 +1342,7 @@ pub mod worker { pub mod plugin { use crate::model::text::fmt::{ format_id, format_main_id, format_message_highlight, FieldsBuilder, MessageWithFields, - TextFormat, + TableWrapper, TextFormat, }; use cli_table::{print_stdout, Table, WithTitle}; use golem_client::model::{ @@ -1380,10 +1392,21 @@ pub mod plugin { } } - impl TextFormat for Vec { + pub struct PluginDefinitionTable(Vec); + + impl TableWrapper for PluginDefinitionDefaultPluginOwnerDefaultPluginScope { + type Table = PluginDefinitionTable; + + fn from_vec(vec: &[Self]) -> Self::Table { + PluginDefinitionTable(vec.to_vec()) + } + } + + impl TextFormat for PluginDefinitionTable { fn print(&self) { print_stdout( - self.iter() + self.0 + .iter() .map(PluginDefinitionTableView::from) .collect::>() .with_title(),