refactor: reshuffling and extracting Executor trait
Some checks failed
Test / build (map[name:stable]) (push) Successful in 1m43s
Test / build (map[name:nightly]) (push) Successful in 2m8s
Release Please / Release-plz (push) Failing after 58s

This commit is contained in:
Paul Campbell 2024-12-08 17:23:54 +00:00
parent b6831e6de0
commit 6552a413da
22 changed files with 508 additions and 259 deletions

View file

@ -3,7 +3,7 @@ use kxio::{net::Response, print::Printer};
use crate::{e, s};
pub struct APIResult<T> {
pub(crate) struct APIResult<T> {
pub(crate) text: String,
pub(crate) result: Result<T, kxio::net::Error>,
}

View file

@ -1,41 +1,17 @@
//
use color_eyre::Result;
use crate::{
f,
nextcloud::model::{NextcloudBoardId, NextcloudHostname, NextcloudPassword, NextcloudUsername},
s,
trello::types::{
auth::{TrelloApiKey, TrelloApiSecret},
TrelloBoardName,
},
Ctx, NAME,
};
#[derive(Clone, Debug, derive_more::From, PartialEq, Eq, serde::Deserialize)]
pub(crate) struct TrelloConfig {
pub(crate) api_key: TrelloApiKey,
pub(crate) api_secret: TrelloApiSecret,
pub(crate) board_name: TrelloBoardName,
}
#[derive(Clone, Debug, derive_more::From, PartialEq, Eq, serde::Deserialize)]
pub(crate) struct NextcloudConfig {
pub(crate) hostname: NextcloudHostname,
pub(crate) username: NextcloudUsername,
pub(crate) password: NextcloudPassword,
pub(crate) board_id: NextcloudBoardId,
}
use crate::{f, nextcloud::NextcloudConfig, s, trello::TrelloConfig, Ctx, NAME};
#[derive(
Clone, Debug, derive_more::From, PartialEq, Eq, derive_more::AsRef, serde::Deserialize,
)]
pub struct AppConfig {
pub(crate) struct AppConfig {
pub(crate) trello: TrelloConfig,
pub(crate) nextcloud: NextcloudConfig,
}
impl AppConfig {
pub fn load(ctx: &Ctx) -> Result<Self> {
pub(crate) fn load(ctx: &Ctx) -> Result<Self> {
let file = ctx.fs.base().join(f!("{NAME}.toml"));
let str = ctx.fs.file(&file).reader()?;
Ok(toml::from_str(s!(str).as_str())?)

21
src/execute.rs Normal file
View file

@ -0,0 +1,21 @@
//
use color_eyre::eyre::eyre;
use color_eyre::Result;
use crate::FullCtx;
pub(crate) trait Execute {
async fn execute(self, ctx: FullCtx) -> Result<()>;
}
impl Execute for crate::Command {
async fn execute(self, ctx: FullCtx) -> Result<()> {
match self {
Self::Init => Err(eyre!("Config file already exists. Not overwriting it.")),
Self::Check => crate::check::run(ctx).await,
Self::Import => todo!(), //crate::import::run(ctx).await,
Self::Trello(cmd) => cmd.execute(ctx).await,
Self::Nextcloud(cmd) => cmd.execute(ctx).await,
}
}
}

View file

@ -3,26 +3,30 @@ use std::path::PathBuf;
use clap::Parser;
use color_eyre::eyre::eyre;
pub use config::AppConfig;
use config::AppConfig;
use kxio::{fs::FileSystem, net::Net, print::Printer};
mod api_result;
mod check;
mod config;
mod execute;
mod init;
mod macros;
pub mod nextcloud;
mod nextcloud;
mod template;
mod trello;
#[cfg(test)]
mod tests;
pub const NAME: &str = "trello-to-deck";
const NAME: &str = "trello-to-deck";
pub use kxio::kxeprintln as e;
pub use kxio::kxprintln as p;
use nextcloud::DeckClient;
use crate::nextcloud::client::DeckClient;
use crate::trello::client::TrelloClient;
use execute::Execute;
use kxio::kxeprintln as e;
use kxio::kxprintln as p;
#[derive(Parser, Debug)]
#[clap(version = clap::crate_version!(), author = clap::crate_authors!(), about = clap::crate_description!())]
@ -34,77 +38,27 @@ struct Commands {
}
#[derive(Parser, Debug)]
enum Command {
/// Initialize the configuration
#[command(about = "Initialize configuration")]
Init,
/// Check the configuration and connection
#[command(about = "Check configuration and connection")]
Check,
/// Import boards from Trello to Nextcloud Deck
#[command(about = "Import boards from Trello to Nextcloud Deck")]
Import,
#[clap(subcommand)]
Trello(TrelloCommand),
#[clap(subcommand)]
Nextcloud(NextcloudCommand),
}
#[derive(Parser, Debug)]
enum NextcloudCommand {
/// Trello-specific commands
#[command(about = "Trello-specific commands")]
#[clap(subcommand)]
Board(NextcloudBoardCommand),
Trello(trello::TrelloCommand),
/// Nextcloud-specific commands
#[command(about = "Nextcloud-specific commands")]
#[clap(subcommand)]
Stack(NextcloudStackCommand),
#[clap(subcommand)]
Card(NextcloudCardCommand),
}
#[derive(Parser, Debug)]
enum NextcloudBoardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
#[derive(Parser, Debug)]
enum NextcloudStackCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
#[derive(Parser, Debug)]
enum NextcloudCardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
},
Get {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
card_id: i64,
},
Create {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
#[clap(long)]
title: String,
#[clap(long)]
description: Option<String>,
},
}
#[derive(Parser, Debug)]
enum TrelloCommand {
#[clap(subcommand)]
Board(TrelloBoardCommand),
}
#[derive(Parser, Debug)]
enum TrelloBoardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
Nextcloud(nextcloud::NextcloudCommand),
}
#[derive(Clone)]
@ -124,8 +78,8 @@ impl Default for Ctx {
}
#[derive(Clone)]
pub struct FullCtx {
pub fs: FileSystem,
pub(crate) struct FullCtx {
// pub fs: FileSystem,
pub net: Net,
pub prt: Printer,
pub cfg: AppConfig,
@ -134,6 +88,10 @@ impl FullCtx {
pub(crate) fn deck_client(&self) -> DeckClient {
DeckClient::new(self)
}
pub(crate) fn trello_client(&self) -> TrelloClient {
TrelloClient::new(self)
}
}
#[cfg_attr(test, mutants::skip)]
@ -160,63 +118,15 @@ pub async fn run(ctx: Ctx) -> color_eyre::Result<()> {
}
}
Ok(cfg) => {
let ctx = FullCtx {
fs: ctx.fs,
net: ctx.net,
prt: ctx.prt,
cfg,
};
match commands.command {
Command::Init => Err(eyre!("Config file already exists. Not overwriting it.")),
Command::Check => check::run(ctx).await,
Command::Import => todo!("import"),
Command::Trello(trello) => match trello {
TrelloCommand::Board(board) => match board {
TrelloBoardCommand::List { dump } => {
nextcloud::board::list(ctx, dump).await
}
},
},
Command::Nextcloud(nextcloud) => match nextcloud {
NextcloudCommand::Board(board) => match board {
NextcloudBoardCommand::List { dump } => {
nextcloud::board::list(ctx, dump).await
}
},
NextcloudCommand::Stack(stack) => match stack {
NextcloudStackCommand::List { dump } => {
nextcloud::stack::list(ctx, dump).await
}
},
NextcloudCommand::Card(card) => match card {
NextcloudCardCommand::List { dump, stack_id } => {
nextcloud::card::list(ctx, dump, stack_id.into()).await
}
NextcloudCardCommand::Get {
dump,
stack_id,
card_id,
} => nextcloud::card::get(ctx, dump, stack_id.into(), card_id.into()).await,
NextcloudCardCommand::Create {
dump,
stack_id,
title,
description,
} => {
nextcloud::card::create(
ctx,
nextcloud::card::Create {
dump,
stack_id,
title,
description,
},
)
.await
}
},
},
}
commands
.command
.execute(FullCtx {
// fs: ctx.fs,
net: ctx.net,
prt: ctx.prt,
cfg,
})
.await
}
}
}

View file

@ -1,7 +1,26 @@
//
use clap::Parser;
use crate::execute::Execute;
use crate::{p, FullCtx};
pub async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
#[derive(Parser, Debug)]
pub(crate) enum NextcloudBoardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
impl Execute for NextcloudBoardCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::List { dump } => list(&ctx, dump).await,
}
}
}
pub(crate) async fn list(ctx: &FullCtx, dump: bool) -> color_eyre::Result<()> {
let api_result = ctx.deck_client().get_boards().await;
if dump {
p!(ctx.prt, "{}", api_result.text);

View file

@ -1,11 +1,78 @@
//
use clap::Parser;
use crate::execute::Execute;
use crate::{
nextcloud::model::{NextcloudCardId, NextcloudStackId},
p, FullCtx,
};
#[derive(Parser, Debug)]
pub(crate) enum NextcloudCardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
},
Get {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
card_id: i64,
},
Create {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
stack_id: i64,
#[clap(long)]
title: String,
#[clap(long)]
description: Option<String>,
},
}
impl Execute for NextcloudCardCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::List { dump, stack_id } => {
list(&ctx, dump, NextcloudStackId::from(stack_id)).await
}
Self::Get {
dump,
stack_id,
card_id,
} => {
get(
&ctx,
dump,
NextcloudStackId::from(stack_id),
NextcloudCardId::from(card_id),
)
.await
}
Self::Create {
dump,
stack_id,
title,
description,
} => {
create(
ctx,
Create {
dump,
stack_id,
title,
description,
},
)
.await
}
}
}
}
pub(crate) async fn list(
ctx: FullCtx,
ctx: &FullCtx,
dump: bool,
stack_id: NextcloudStackId,
) -> color_eyre::Result<()> {
@ -26,7 +93,7 @@ pub(crate) async fn list(
}
pub(crate) async fn get(
ctx: FullCtx,
ctx: &FullCtx,
dump: bool,
stack_id: NextcloudStackId,
card_id: NextcloudCardId,
@ -45,10 +112,10 @@ pub(crate) async fn get(
}
pub(crate) struct Create {
pub dump: bool,
pub stack_id: i64,
pub title: String,
pub description: Option<String>,
pub(crate) dump: bool,
pub(crate) stack_id: i64,
pub(crate) title: String,
pub(crate) description: Option<String>,
}
pub(crate) async fn create(ctx: FullCtx, create: Create) -> color_eyre::Result<()> {
let dc = ctx.deck_client();

143
src/nextcloud/client.rs Normal file
View file

@ -0,0 +1,143 @@
use crate::api_result::APIResult;
use crate::nextcloud::card::Create;
use crate::nextcloud::model::{
Board, Card, NextcloudBoardId, NextcloudCardId, NextcloudHostname, NextcloudPassword,
NextcloudStackId, NextcloudUsername, Stack,
};
use crate::{f, FullCtx};
use bytes::Bytes;
use kxio::net::{Net, ReqBuilder};
pub(crate) struct DeckClient<'ctx> {
ctx: &'ctx FullCtx,
hostname: &'ctx NextcloudHostname,
username: &'ctx NextcloudUsername,
password: &'ctx NextcloudPassword,
}
// Uses the API described here: https://deck.readthedocs.io/en/stable/API/#cards
impl<'ctx> DeckClient<'ctx> {
pub fn new(ctx: &'ctx FullCtx) -> Self {
Self {
ctx,
hostname: &ctx.cfg.nextcloud.hostname,
username: &ctx.cfg.nextcloud.username,
password: &ctx.cfg.nextcloud.password,
}
}
fn url(&self, path: impl Into<String>) -> String {
f!(
"https://{}/index.php/apps/deck/api/v1.0/{}",
self.hostname,
path.into()
)
}
async fn request<T: for<'a> serde::Deserialize<'a>>(
&self,
url: impl Into<String>,
custom: fn(&Net, String) -> ReqBuilder,
) -> APIResult<T> {
APIResult::new(
custom(&self.ctx.net, self.url(url))
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json")
.header("content-type", "application/json")
.send()
.await,
&self.ctx.prt,
)
.await
}
async fn request_with_body<T: for<'a> serde::Deserialize<'a>>(
&self,
url: impl Into<String>,
body: impl Into<Bytes>,
custom: fn(&Net, String) -> ReqBuilder,
) -> APIResult<T> {
APIResult::new(
custom(&self.ctx.net, self.url(url))
.basic_auth(self.username.as_str(), Some(self.password.as_str()))
.header("accept", "application/json")
.header("content-type", "application/json")
.body(body)
.send()
.await,
&self.ctx.prt,
)
.await
}
pub(crate) async fn get_boards(&self) -> APIResult<Vec<Board>> {
self.request("boards", |net, url| net.get(url)).await
}
pub(crate) async fn get_board(&self, board_id: NextcloudBoardId) -> APIResult<Board> {
self.request(f!("boards/{board_id}"), |net, url| net.get(url))
.await
}
// pub(crate) async fn create_board(&self, title: &str, color: &str) -> APIResult<Board> {
// self.request_with_body(
// "boards",
// serde_json::json!({
// "title": title,
// "color": color
// })
// .to_string(),
// |net, url| net.post(url),
// )
// .await
// }
pub(crate) async fn get_stacks(&self, board_id: NextcloudBoardId) -> APIResult<Vec<Stack>> {
self.request(f!("boards/{board_id}/stacks"), |net, url| net.get(url))
.await
}
pub(crate) async fn get_stack(
&self,
board_id: NextcloudBoardId,
stack_id: NextcloudStackId,
) -> APIResult<Stack> {
self.request(f!("boards/{board_id}/stacks/{stack_id}"), |net, url| {
net.get(url)
})
.await
}
pub(crate) async fn create_card(&self, create: &Create) -> APIResult<Card> {
let mut body = serde_json::json!({
"title": create.title,
});
if let Some(desc) = &create.description {
body["description"] = serde_json::Value::String(desc.to_string());
}
self.request_with_body(
format!(
"boards/{}/stacks/{}/cards",
self.ctx.cfg.nextcloud.board_id, create.stack_id
),
body.to_string(),
|net, url| net.post(url),
)
.await
}
pub(crate) async fn get_card(
&self,
board_id: NextcloudBoardId,
stack_id: NextcloudStackId,
card_id: NextcloudCardId,
) -> APIResult<Card> {
self.request(
f!("boards/{board_id}/stacks/{stack_id}/cards/{card_id}"),
|net, url| net.get(url),
)
.await
}
}

View file

@ -1,19 +1,30 @@
//
use bytes::Bytes;
//
use clap::Parser;
use kxio::net::{Net, ReqBuilder};
use crate::{api_result::APIResult, f, FullCtx};
use card::Create;
use model::{
Board, Card, NextcloudBoardId, NextcloudHostname, NextcloudPassword, NextcloudStackId,
NextcloudUsername, Stack,
use crate::{
api_result::APIResult,
execute::Execute,
f,
nextcloud::{
board::NextcloudBoardCommand,
card::Create,
card::NextcloudCardCommand,
model::{
Board, Card, NextcloudBoardId, NextcloudHostname, NextcloudPassword, NextcloudStackId,
NextcloudUsername, Stack,
},
stack::NextcloudStackCommand,
},
FullCtx,
};
pub mod board;
pub mod card;
pub mod model;
pub mod stack;
mod board;
mod card;
pub(crate) mod client;
pub(crate) mod model;
mod stack;
// #[cfg(test)]
// mod tests;
@ -151,3 +162,30 @@ impl<'ctx> DeckClient<'ctx> {
.await
}
}
#[derive(Parser, Debug)]
pub(crate) enum NextcloudCommand {
#[clap(subcommand)]
Board(NextcloudBoardCommand),
#[clap(subcommand)]
Stack(NextcloudStackCommand),
#[clap(subcommand)]
Card(NextcloudCardCommand),
}
impl Execute for NextcloudCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
NextcloudCommand::Board(cmd) => cmd.execute(ctx).await,
NextcloudCommand::Stack(cmd) => cmd.execute(ctx).await,
NextcloudCommand::Card(cmd) => cmd.execute(ctx).await,
}
}
}
#[derive(Clone, Debug, derive_more::From, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize)]
pub struct NextcloudConfig {
pub(crate) hostname: NextcloudHostname,
pub(crate) username: NextcloudUsername,
pub(crate) password: NextcloudPassword,
pub(crate) board_id: NextcloudBoardId,
}

View file

@ -113,64 +113,64 @@ newtype!(
);
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct NextcloudBoardOwner {
pub(crate) struct NextcloudBoardOwner {
#[serde(rename = "primaryKey")]
pub primary_key: String,
pub uid: String,
pub(crate) primary_key: String,
pub(crate) uid: String,
#[serde(rename = "displayname")]
pub display_name: String,
pub(crate) display_name: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Board {
pub id: NextcloudBoardId,
pub title: NextcloudBoardTitle,
pub owner: NextcloudBoardOwner,
pub color: NextcloudBoardColour,
pub archived: bool,
pub labels: Vec<Label>,
pub acl: Vec<Acl>,
pub(crate) struct Board {
pub(crate) id: NextcloudBoardId,
pub(crate) title: NextcloudBoardTitle,
pub(crate) owner: NextcloudBoardOwner,
pub(crate) color: NextcloudBoardColour,
pub(crate) archived: bool,
pub(crate) labels: Vec<Label>,
pub(crate) acl: Vec<Acl>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Stack {
pub id: NextcloudStackId,
pub title: NextcloudStackTitle,
pub order: NextcloudOrder,
pub(crate) struct Stack {
pub(crate) id: NextcloudStackId,
pub(crate) title: NextcloudStackTitle,
pub(crate) order: NextcloudOrder,
#[serde(rename = "boardId")]
pub board_id: NextcloudBoardId,
pub(crate) board_id: NextcloudBoardId,
#[serde(rename = "ETag")]
pub etag: NextcloudETag,
pub(crate) etag: NextcloudETag,
#[serde(default)]
pub cards: Vec<Card>,
pub(crate) cards: Vec<Card>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Card {
pub id: NextcloudCardId,
pub title: NextcloudCardTitle,
pub description: Option<String>,
pub(crate) struct Card {
pub(crate) id: NextcloudCardId,
pub(crate) title: NextcloudCardTitle,
pub(crate) description: Option<String>,
#[serde(rename = "stackId")]
pub stack_id: NextcloudStackId,
pub order: NextcloudOrder,
pub archived: bool,
pub(crate) stack_id: NextcloudStackId,
pub(crate) order: NextcloudOrder,
pub(crate) archived: bool,
#[serde(default)]
pub due_date: Option<String>,
pub(crate) due_date: Option<String>,
#[serde(default)]
pub labels: Option<Vec<Label>>,
pub(crate) labels: Option<Vec<Label>>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Label {
pub id: NextcloudLabelId,
pub title: String,
pub color: String,
pub(crate) struct Label {
pub(crate) id: NextcloudLabelId,
pub(crate) title: String,
pub(crate) color: String,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Acl {
pub participant: String,
pub permission_edit: bool,
pub permission_share: bool,
pub permission_manage: bool,
pub(crate) struct Acl {
pub(crate) participant: String,
pub(crate) permission_edit: bool,
pub(crate) permission_share: bool,
pub(crate) permission_manage: bool,
}

View file

@ -1,7 +1,26 @@
//
use clap::Parser;
use crate::execute::Execute;
use crate::{p, FullCtx};
pub async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
#[derive(Parser, Debug)]
pub(crate) enum NextcloudStackCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
impl Execute for NextcloudStackCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::List { dump } => list(ctx, dump).await,
}
}
}
pub(crate) async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
let api_result = ctx
.deck_client()
.get_stacks(ctx.cfg.nextcloud.board_id)

View file

@ -5,14 +5,13 @@ use std::collections::HashMap;
use assert2::let_assert;
use crate::{config::AppConfig, f, init::run, Ctx, NAME};
use crate::{
config::AppConfig, f, init::run, nextcloud::NextcloudConfig, s, trello::TrelloConfig, Ctx, NAME,
};
mod config {
use super::*;
use crate::config::{NextcloudConfig, TrelloConfig};
use crate::s;
#[test]
fn load_config() {
//given
@ -124,18 +123,18 @@ mod given {
print::Printer,
};
pub fn a_context(fs: FileSystem, net: Net, prt: Printer) -> Ctx {
pub(crate) fn a_context(fs: FileSystem, net: Net, prt: Printer) -> Ctx {
Ctx { fs, net, prt }
}
pub fn a_filesystem() -> TempFileSystem {
pub(crate) fn a_filesystem() -> TempFileSystem {
kxio::fs::temp().expect("temp fs")
}
pub fn a_network() -> MockNet {
pub(crate) fn a_network() -> MockNet {
kxio::net::mock()
}
pub fn a_printer() -> Printer {
pub(crate) fn a_printer() -> Printer {
kxio::print::test()
}
}

View file

@ -2,8 +2,3 @@ mod create;
mod delete;
mod get;
mod update;
pub use create::create_card;
pub use delete::delete_card;
pub use get::get_card;
pub use update::{update_card, TrelloCardUpdate};

View file

@ -30,7 +30,7 @@ use crate::{
/// 200 OK Success
///
/// application/json
pub async fn get_lists_cards(
pub(crate) async fn get_lists_cards(
auth: &TrelloAuth,
list: &TrelloList,
net: &Net,

View file

@ -1,11 +1,13 @@
//
use kxio::{net::Net, print::Printer};
use crate::api_result::APIResult;
use crate::config::TrelloConfig;
use crate::trello::{
types::{auth::TrelloAuth, board::TrelloBoard},
url,
use crate::trello::TrelloConfig;
use crate::{
api_result::APIResult,
trello::{
types::{auth::TrelloAuth, board::TrelloBoard},
url,
},
};
/// Get lists from named board that Member belongs to
@ -37,7 +39,7 @@ use crate::trello::{
/// curl --request GET \
/// --url "https://api.trello.com/1/members/$TRELLO_USERNAME/boards?key=$TRELLO_KEY&token=$TRELLO_SECRET&lists=open" \
/// --header 'Accept: application/json'
pub async fn get_boards_that_member_belongs_to(
pub(crate) async fn get_boards_that_member_belongs_to(
cfg: &TrelloConfig,
net: &Net,
prt: &Printer,

View file

@ -1,9 +1,5 @@
//
pub mod boards;
// pub mod cards;
// pub mod lists;
pub mod members;
pub(crate) mod members;
#[cfg(test)]
mod tests;

View file

@ -5,7 +5,6 @@ use kxio::net::StatusCode;
use serde_json::json;
use crate::{
config::TrelloConfig,
s,
trello::{api::members::get_boards_that_member_belongs_to, types::board::TrelloBoard},
};
@ -15,8 +14,8 @@ mod given;
type TestResult = color_eyre::Result<()>;
mod members {
use super::*;
use crate::trello::TrelloConfig;
#[tokio::test]
async fn get_member_boards() -> TestResult {

View file

@ -1,12 +1,29 @@
//
use clap::Parser;
use crate::execute::Execute;
use crate::trello::types::board::TrelloBoard;
use crate::trello::types::TrelloBoardName;
use crate::{p, FullCtx};
#[derive(Parser, Debug)]
pub(crate) enum TrelloBoardCommand {
List {
#[clap(long, action = clap::ArgAction::SetTrue)]
dump: bool,
},
}
impl Execute for TrelloBoardCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::List { dump } => list(ctx, dump).await,
}
}
}
pub(crate) async fn list(ctx: FullCtx, dump: bool) -> color_eyre::Result<()> {
let api_result =
super::api::members::get_boards_that_member_belongs_to(&ctx.cfg.trello, &ctx.net, &ctx.prt)
.await;
let api_result = ctx.trello_client().boards(&ctx.cfg.trello).await;
if dump {
p!(ctx.prt, "{}", api_result.text);
} else {

22
src/trello/client.rs Normal file
View file

@ -0,0 +1,22 @@
//
use crate::api_result::APIResult;
use crate::trello::types::board::TrelloBoard;
use crate::trello::TrelloConfig;
use crate::FullCtx;
pub(crate) struct TrelloClient<'ctx> {
ctx: &'ctx FullCtx,
}
impl<'ctx> TrelloClient<'ctx> {
pub(crate) async fn boards(&self, cfg: &TrelloConfig) -> APIResult<Vec<TrelloBoard>> {
super::api::members::get_boards_that_member_belongs_to(cfg, &self.ctx.net, &self.ctx.prt)
.await
}
}
impl TrelloClient<'_> {
pub(crate) fn new(ctx: &FullCtx) -> TrelloClient {
TrelloClient { ctx }
}
}

View file

@ -1,16 +1,42 @@
//
// pub mod api;
pub mod api;
pub mod boards;
pub mod types;
pub(crate) mod api;
pub(crate) mod boards;
pub(crate) mod client;
pub(crate) mod types;
// #[cfg(test)]
// mod tests;
use crate::f;
use crate::execute::Execute;
use crate::trello::boards::TrelloBoardCommand;
use crate::trello::types::auth::{TrelloApiKey, TrelloApiSecret};
use crate::trello::types::TrelloBoardName;
use crate::{f, FullCtx};
use clap::Parser;
pub fn url(path: impl Into<String>) -> String {
pub(crate) fn url(path: impl Into<String>) -> String {
let path = path.into();
assert!(path.starts_with("/"));
f!("https://api.trello.com/1{path}")
}
#[derive(Parser, Debug)]
pub(crate) enum TrelloCommand {
#[clap(subcommand)]
Board(TrelloBoardCommand),
}
impl Execute for TrelloCommand {
async fn execute(self, ctx: FullCtx) -> color_eyre::Result<()> {
match self {
Self::Board(cmd) => cmd.execute(ctx).await,
}
}
}
#[derive(Clone, Debug, derive_more::From, PartialEq, Eq, serde::Deserialize)]
pub struct TrelloConfig {
pub(crate) api_key: TrelloApiKey,
pub(crate) api_secret: TrelloApiSecret,
pub(crate) board_name: TrelloBoardName,
}

View file

@ -2,7 +2,7 @@
use crate::trello::{api::cards::TrelloCardUpdate, TrelloCardId, TrelloCardName, TrelloListId};
#[derive(Debug, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
pub struct TrelloCard {
pub(crate) struct TrelloCard {
id: TrelloCardId,
name: TrelloCardName,
#[serde(rename = "idList")]
@ -10,11 +10,11 @@ pub struct TrelloCard {
}
impl TrelloCard {
#[cfg(test)]
pub const fn new(id: TrelloCardId, name: TrelloCardName, id_list: TrelloListId) -> Self {
pub(crate) const fn new(id: TrelloCardId, name: TrelloCardName, id_list: TrelloListId) -> Self {
Self { id, name, id_list }
}
pub const fn list_id(&self) -> &TrelloListId {
pub(crate) const fn list_id(&self) -> &TrelloListId {
&self.id_list
}
}

View file

@ -1,7 +1,7 @@
pub(crate) mod auth;
pub(crate) mod board;
// mod card;
mod list;
pub(crate) mod list;
// mod new_card;
use derive_more::derive::Display;

View file

@ -1,20 +1,20 @@
use super::{TrelloCardName, TrelloListId};
pub struct NewTrelloCard {
pub(crate) struct NewTrelloCard {
name: TrelloCardName,
list_id: TrelloListId,
}
impl NewTrelloCard {
pub fn new(name: impl Into<TrelloCardName>, list_id: impl Into<TrelloListId>) -> Self {
pub(crate) fn new(name: impl Into<TrelloCardName>, list_id: impl Into<TrelloListId>) -> Self {
Self {
name: name.into(),
list_id: list_id.into(),
}
}
pub const fn name(&self) -> &TrelloCardName {
pub(crate) const fn name(&self) -> &TrelloCardName {
&self.name
}
pub const fn list_id(&self) -> &TrelloListId {
pub(crate) const fn list_id(&self) -> &TrelloListId {
&self.list_id
}
}