From 841a5b93f1d1c09121a1f3ebb136303eae69d50f Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 7 Dec 2024 10:05:37 +0000 Subject: [PATCH] feat: add kxio printer to context --- src/api_result.rs | 6 +++--- src/init.rs | 1 + src/lib.rs | 9 ++++++++- src/macros/print.rs | 24 ++++++++++++------------ src/main.rs | 8 +------- src/nextcloud/mod.rs | 10 +++++++++- src/nextcloud/tests.rs | 10 ++++++++-- src/tests/mod.rs | 16 +++++++++++----- src/trello/api/members.rs | 4 +++- src/trello/api/tests/given.rs | 6 +++++- src/trello/api/tests/mod.rs | 3 ++- 11 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/api_result.rs b/src/api_result.rs index c9e3bdb..970246e 100644 --- a/src/api_result.rs +++ b/src/api_result.rs @@ -1,5 +1,5 @@ // -use kxio::net::Response; +use kxio::{net::Response, print::Printer}; use crate::{e, s}; @@ -9,7 +9,7 @@ pub struct APIResult { } impl serde::Deserialize<'a>> APIResult { - pub async fn new(response: kxio::net::Result) -> Self { + pub async fn new(response: kxio::net::Result, prt: &Printer) -> Self { match response { Ok(response) => { let text = response.text().await.unwrap_or_default(); @@ -17,7 +17,7 @@ impl serde::Deserialize<'a>> APIResult { let result = serde_json::from_str::(&text) .map_err(|e| e.to_string()) .map_err(|e| { - e!("{e}: {text}"); + e!(prt, "{e}: {text}"); e }) .map_err(kxio::net::Error::from); diff --git a/src/init.rs b/src/init.rs index 418cb0e..fb3fd6d 100644 --- a/src/init.rs +++ b/src/init.rs @@ -14,6 +14,7 @@ pub(crate) fn run(ctx: &Ctx) -> Result<()> { } else { file.write(include_str!("default-config.toml"))?; p!( + ctx.prt, "{}", template::expand( include_str!("post-init-instructions.txt"), diff --git a/src/lib.rs b/src/lib.rs index cdd52ca..66ac8bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ use std::path::PathBuf; use clap::Parser; use color_eyre::eyre::eyre; pub use config::AppConfig; -use kxio::{fs::FileSystem, net::Net}; +use kxio::{fs::FileSystem, net::Net, print::Printer}; mod api_result; mod config; @@ -19,6 +19,9 @@ mod tests; pub const NAME: &str = "trello-to-deck"; +pub use kxio::kxeprintln as e; +pub use kxio::kxprintln as p; + #[derive(Parser, Debug)] #[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())] struct Commands { @@ -36,12 +39,14 @@ enum Command { pub struct Ctx { pub fs: FileSystem, pub net: Net, + pub prt: Printer, } impl Default for Ctx { fn default() -> Self { Self { fs: kxio::fs::new(PathBuf::default()), net: kxio::net::new(), + prt: kxio::print::standard(), } } } @@ -50,6 +55,7 @@ impl Default for Ctx { pub struct FullCtx { pub fs: FileSystem, pub net: Net, + pub prt: Printer, pub cfg: AppConfig, } @@ -71,6 +77,7 @@ pub async fn run(ctx: Ctx) -> color_eyre::Result<()> { let _ctx = FullCtx { fs: ctx.fs, net: ctx.net, + prt: ctx.prt, cfg, }; match commands.command { diff --git a/src/macros/print.rs b/src/macros/print.rs index 1a1ea95..10b424b 100644 --- a/src/macros/print.rs +++ b/src/macros/print.rs @@ -1,14 +1,14 @@ // -#[macro_export] -macro_rules! p { - ($($arg:tt)*) => {{ - println!($($arg)*); - }}; -} +// #[macro_export] +// macro_rules! p { +// ($printer:expr, $($arg:tt)*) => {{ +// kxio::kxprintln!($printer, $($arg)*); +// }}; +// } -#[macro_export] -macro_rules! e { - ($($arg:tt)*) => {{ - eprintln!($($arg)*); - }}; -} +// #[macro_export] +// macro_rules! e { +// ($printer:expr, $($arg:tt)*) => {{ +// kxio::kxeprintln!($($arg)*); +// }}; +// } diff --git a/src/main.rs b/src/main.rs index 0e1c72f..d253395 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,4 @@ // -use std::path::PathBuf; - use color_eyre::Result; use trello_to_deck::{run, Ctx}; @@ -8,9 +6,5 @@ use trello_to_deck::{run, Ctx}; #[tokio::main] #[cfg_attr(test, mutants::skip)] async fn main() -> Result<()> { - run(Ctx { - fs: kxio::fs::new(PathBuf::default()), - net: kxio::net::new(), - }) - .await + run(Ctx::default()).await } diff --git a/src/nextcloud/mod.rs b/src/nextcloud/mod.rs index 4a7eadf..6c535ae 100644 --- a/src/nextcloud/mod.rs +++ b/src/nextcloud/mod.rs @@ -1,5 +1,6 @@ // use kxio::net::Net; +use kxio::print::Printer; use crate::api_result::APIResult; use crate::{config::NextcloudConfig, f}; @@ -14,15 +15,17 @@ mod tests; pub struct DeckClient<'cfg> { net: Net, + prt: Printer, hostname: &'cfg NextcloudHostname, username: &'cfg NextcloudUsername, password: &'cfg NextcloudPassword, } impl<'cfg> DeckClient<'cfg> { - pub fn new(cfg: &'cfg NextcloudConfig, net: Net) -> Self { + pub fn new(cfg: &'cfg NextcloudConfig, net: Net, prt: Printer) -> Self { Self { net, + prt, hostname: &cfg.hostname, username: &cfg.username, password: &cfg.password, @@ -45,6 +48,7 @@ impl<'cfg> DeckClient<'cfg> { .header("accept", "application/json") .send() .await, + &self.prt, ) .await } @@ -57,6 +61,7 @@ impl<'cfg> DeckClient<'cfg> { .header("accept", "application/json") .send() .await, + &self.prt, ) .await } @@ -76,6 +81,7 @@ impl<'cfg> DeckClient<'cfg> { ) .send() .await, + &self.prt, ) .await } @@ -88,6 +94,7 @@ impl<'cfg> DeckClient<'cfg> { .header("accept", "application/json") .send() .await, + &self.prt, ) .await } @@ -120,6 +127,7 @@ impl<'cfg> DeckClient<'cfg> { .body(json.to_string()) .send() .await, + &self.prt, ) .await } diff --git a/src/nextcloud/tests.rs b/src/nextcloud/tests.rs index 5f1682d..8a33aa1 100644 --- a/src/nextcloud/tests.rs +++ b/src/nextcloud/tests.rs @@ -148,7 +148,7 @@ mod client { .expect("mock request"); let cfg = given::a_nextcloud_config(); - let deck_client = DeckClient::new(&cfg, mock_net.into()); + let deck_client = DeckClient::new(&cfg, mock_net.into(), given::a_printer()); //when let result = deck_client.get_boards().await.result.expect("get boards"); @@ -190,7 +190,7 @@ mod client { .expect("mock request"); let cfg = given::a_nextcloud_config(); - let deck_client = DeckClient::new(&cfg, mock_net.into()); + let deck_client = DeckClient::new(&cfg, mock_net.into(), given::a_printer()); //when let result = deck_client @@ -229,6 +229,8 @@ mod client { } mod given { + use kxio::print::Printer; + use super::*; pub fn a_nextcloud_config() -> NextcloudConfig { @@ -243,4 +245,8 @@ mod given { board_id, } } + + pub fn a_printer() -> Printer { + kxio::print::test() + } } diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 9b0d8c7..72269c1 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -17,7 +17,7 @@ mod init { fn when_file_does_not_exist_should_create() { //given let fs = given::a_filesystem(); - let ctx = given::a_context(fs.as_real(), given::a_network().into()); + let ctx = given::a_context(fs.as_real(), given::a_network().into(), given::a_printer()); //when let_assert!(Ok(_) = run(&ctx)); @@ -37,7 +37,7 @@ mod init { let file = fs.file(&path); file.write("").expect("create file"); - let ctx = given::a_context(fs.as_real(), given::a_network().into()); + let ctx = given::a_context(fs.as_real(), given::a_network().into(), given::a_printer()); //when let_assert!(Err(err) = run(&ctx)); @@ -59,12 +59,13 @@ mod config { //given let fs = given::a_filesystem(); let mock_net = given::a_network(); + let prt = given::a_printer(); let file = fs.file(&fs.base().join("trello-to-deck.toml")); file.write(include_str!("test-config.toml")) .expect("write test config"); - let ctx = given::a_context(fs.as_real(), mock_net.into()); + let ctx = given::a_context(fs.as_real(), mock_net.into(), prt); //when let cfg = AppConfig::load(&ctx); @@ -114,12 +115,13 @@ mod given { use kxio::{ fs::{FileSystem, TempFileSystem}, net::{MockNet, Net}, + print::Printer, }; use crate::Ctx; - pub fn a_context(fs: FileSystem, net: Net) -> Ctx { - Ctx { fs, net } + pub fn a_context(fs: FileSystem, net: Net, prt: Printer) -> Ctx { + Ctx { fs, net, prt } } pub fn a_filesystem() -> TempFileSystem { @@ -129,4 +131,8 @@ mod given { pub fn a_network() -> MockNet { kxio::net::mock() } + + pub fn a_printer() -> Printer { + kxio::print::test() + } } diff --git a/src/trello/api/members.rs b/src/trello/api/members.rs index 5217b14..86e3413 100644 --- a/src/trello/api/members.rs +++ b/src/trello/api/members.rs @@ -1,5 +1,5 @@ // -use kxio::net::Net; +use kxio::{net::Net, print::Printer}; use crate::api_result::APIResult; use crate::{ @@ -42,6 +42,7 @@ use crate::{ pub async fn get_boards_that_member_belongs_to( auth: &TrelloAuth, net: &Net, + prt: &Printer, ) -> APIResult> { APIResult::new( net.get(url(f!("/members/{}/boards?lists=open", **auth.user()))) @@ -49,6 +50,7 @@ pub async fn get_boards_that_member_belongs_to( .header("Accept", "application/json") .send() .await, + prt, ) .await } diff --git a/src/trello/api/tests/given.rs b/src/trello/api/tests/given.rs index 7701e20..414db31 100644 --- a/src/trello/api/tests/given.rs +++ b/src/trello/api/tests/given.rs @@ -1,5 +1,5 @@ // -use kxio::net::MockNet; +use kxio::{net::MockNet, print::Printer}; use crate::trello::types::auth::{TrelloApiKey, TrelloApiSecret, TrelloAuth, TrelloUser}; @@ -7,6 +7,10 @@ pub(crate) fn a_network() -> MockNet { kxio::net::mock() } +pub(crate) fn a_printer() -> Printer { + kxio::print::test() +} + pub(crate) fn an_auth() -> TrelloAuth { TrelloAuth::new( TrelloApiKey::new("foo"), diff --git a/src/trello/api/tests/mod.rs b/src/trello/api/tests/mod.rs index 6008673..a8c2742 100644 --- a/src/trello/api/tests/mod.rs +++ b/src/trello/api/tests/mod.rs @@ -26,6 +26,7 @@ mod members { async fn get_member_boards() -> TestResult { //given let net = given::a_network(); + let prt = given::a_printer(); let auth = given::an_auth(); net.on() @@ -43,7 +44,7 @@ mod members { ])))?; //when - let result = get_boards_that_member_belongs_to(&auth, &net.into()) + let result = get_boards_that_member_belongs_to(&auth, &net.into(), &prt) .await .result?;