taskfast_cli/cmd/
discover.rs1use clap::{Parser, ValueEnum};
20use serde_json::json;
21
22use super::{CmdResult, Ctx};
23use crate::envelope::Envelope;
24
25use taskfast_client::api::types::{ListOpenTasksAssignmentType, ListOpenTasksStatus};
26use taskfast_client::map_api_error;
27
28#[derive(Debug, Parser)]
29pub struct Args {
30 #[arg(long)]
33 pub status: Option<DiscoverStatus>,
34
35 #[arg(long)]
37 pub assignment_type: Option<DiscoverAssignmentType>,
38
39 #[arg(long = "capability", value_name = "CAP")]
41 pub capabilities: Vec<String>,
42
43 #[arg(long)]
45 pub budget_max: Option<f64>,
46
47 #[arg(long)]
49 pub budget_min: Option<f64>,
50
51 #[arg(long)]
52 pub cursor: Option<String>,
53
54 #[arg(long, default_value_t = 50)]
57 pub limit: i64,
58}
59
60#[derive(Debug, Clone, Copy, ValueEnum)]
62pub enum DiscoverStatus {
63 Open,
64 Bidding,
65}
66
67impl From<DiscoverStatus> for ListOpenTasksStatus {
68 fn from(s: DiscoverStatus) -> Self {
69 match s {
70 DiscoverStatus::Open => Self::Open,
71 DiscoverStatus::Bidding => Self::Bidding,
72 }
73 }
74}
75
76#[derive(Debug, Clone, Copy, ValueEnum)]
78pub enum DiscoverAssignmentType {
79 Open,
80 Direct,
81}
82
83impl From<DiscoverAssignmentType> for ListOpenTasksAssignmentType {
84 fn from(a: DiscoverAssignmentType) -> Self {
85 match a {
86 DiscoverAssignmentType::Open => Self::Open,
87 DiscoverAssignmentType::Direct => Self::Direct,
88 }
89 }
90}
91
92pub async fn run(ctx: &Ctx, args: Args) -> CmdResult {
93 let client = ctx.client()?;
94 let capabilities_vec: Option<Vec<String>> = if args.capabilities.is_empty() {
95 None
96 } else {
97 Some(args.capabilities.clone())
98 };
99 let resp = match client
100 .inner()
101 .list_open_tasks(
102 args.assignment_type.map(Into::into),
103 args.budget_max,
104 args.budget_min,
105 capabilities_vec.as_ref(),
106 args.cursor.as_deref(),
107 taskfast_client::page_limit(args.limit),
108 args.status.map(Into::into),
109 )
110 .await
111 {
112 Ok(v) => v.into_inner(),
113 Err(e) => return Err(map_api_error(e).await.into()),
114 };
115 Ok(Envelope::success(
116 ctx.environment,
117 ctx.dry_run,
118 json!({
119 "tasks": resp.data,
120 "meta": resp.meta,
121 }),
122 ))
123}