1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
//! # Crud Api
//!
//! This crate provides a framework to generate an executable to manipulate your HTTP API from CLI.
//!
//! The apps using this lib can replace your _curl_ queries when you need to access to your favorite API.
//!
//! ## Features
//!
//! API:
//! - data are encoded in JSON. It don't support XML, grpc, ...
//! - output can be formated on json, yaml, toml, csv or tsv
//! - output stream on stdout or in a file
//!
//!
//! ## Tutorial
//!
//! Let's create an CLI for [jsonplaceholder](http://jsonplaceholder.typicode.com/) API.
//! For the impatients, the whole code of this example can be found in [`examples/jsonplaceholder_api.rs`](./examples/jsonplaceholder_api.rs "jsonplaceholder_api.rs")
//!
//! First add these dependencies to `Cargo.toml`:
//! ```toml
//! [dependencies]
//! log = "0.4"
//! pretty_env_logger = "0.4"
//! clap = "4.0"
//! crud-api = {version = "0.1", path="../crud/crud-api", default-features=false, features=["toml","json","yaml"]}
//! crud-auth = {version = "0.1", path="../crud/crud-auth"}
//! crud-auth-bearer = {version = "0.1", path="../crud/crud-auth-bearer"}
//! hyper = "0.14"
//! hyper-tls = "0.5"
//! miette = { version = "5.2", features = ["fancy"] }
//! tokio = { version = "1", features = ["full"] }
//! serde = { version = "1.0", features = ["derive"] }
//! # To force static openssl
//! openssl = { version = "0.10", features = ["vendored"] }
//! ```
//!
//! Now, create a minimal runner stucture and a `main` function.
//! `ApiRun` on `JSONPlaceHolder` derives all the CLI.
//! ```rust
//! use crud_api::ApiRun;
//! use crud_auth::CrudAuth;
//! use crud_auth_no_auth::Auth;
//! use miette::{IntoDiagnostic, Result};
//!
//! #[derive(ApiRun)]
//! struct JSONPlaceHolder;
//!
//! #[tokio::main]
//! async fn main() -> Result<()> {
//!   JSONPlaceHolder::run().await
//! }
//! ```
//! [`crud_api_endpoint::ApiRun`] accepts some parameters. They are documented in `crud_api_endoint` crate.
//! Let's customize our CLI with a `base_url` for our API, a `name` used in the documentation and the settings. `qualifier` and `organisation` is used to compute the settings location and `env_prefix` is the prefix of the environment variables
//! ```rust
//! # use crud_api::ApiRun;
//! # use crud_auth::CrudAuth;
//! # use crud_auth_no_auth::Auth;
//! # use miette::{IntoDiagnostic, Result};
//! #[derive(ApiRun)]
//! #[api(infos(
//!   base_url = "http://jsonplaceholder.typicode.com",
//!   name = "jsonplaceholder",
//!   qualifier = "com",
//!   organisation = "typicode",
//!   env_prefix = "JSONPLACEHOLDER"
//! ))]
//! struct JSONPlaceHolder;
//! # #[tokio::main]
//! # async fn main() -> Result<()> {
//! #   JSONPlaceHolder::run().await
//! # }
//! ```
//! Before creating the first endpoint we need to describe its output structure.
//! ```rust
//! use serde::{Deserialize, Serialize};
//! #[derive(Debug, Default, Deserialize, Serialize)]
//! struct Post {
//!   id: u32,
//!   #[serde(rename = "userId")]
//!   user_id: u32,
//!   title: String,
//!   body: String,
//! }
//! ```
//!
//! Now, we can declare the endpoint.
//! The minimal parameters are:
//! - `route`, the target api route.
//! - `cli_route`, the route transcipted as cli arguments. Each slash separate a subcommand.
//! The other parameters can found in [`crud_api_endpoint::Api`] and [`crud_api_endpoint::Enpoint`] structs documentation.
//!
//! ```rust
//! # use serde::{Deserialize, Serialize};
//! use crud_api::Api;
//! #[derive(Api, Debug, Default, Deserialize, Serialize)]
//! #[api(
//!   endpoint(
//!     route = "/posts",
//!     cli_route = "/post",
//!     multiple_results,
//!   ))]
//! struct Post {
//!   id: u32,
//!   #[serde(rename = "userId")]
//!   user_id: u32,
//!   title: String,
//!   body: String,
//! }
//! ```
//!
//! We can create more complex enpoint. Let's create an edit route.
//!
//! - The `route` parameter takes a post's `id` argument. This argument should be present in the `cli_route`.
//! - the HTTP method is set with the `method` parameter.
//! - Some help can be provided via the parameters `cli_help` and `cli_long_help`.
//! - the payload is described by the struct declared with the `payload_struct`. The query parameter can be add with the `query_struct` parameter.
//!
//! In this step, the payload structure is `PostCreate` (the same structure is used for both creation and update). `PostCreate` derives `ApiInput`. All `PostCreate` fields parameters are describe in the [`crud_api_endpoint::ApiInputConfig`] structs.
//!
//!
//! ```rust
//! # use serde::{Deserialize, Serialize};
//! use crud_api::{Api, ApiInput};
//! #[derive(Api, Debug, Default, Deserialize, Serialize)]
//! #[api(
//!   endpoint(
//!     route = "/posts",
//!     cli_route = "/post",
//!     multiple_results,
//!   ),
//!   endpoint(
//!     route = "/posts/{id}",
//!     method = "PUT",
//!     payload_struct = "PostCreate",
//!     cli_route = "/post/{id}/replace",
//!     cli_help = "Update a Posts (replace the whole post)"
//!   )
//! )]
//! struct Post {
//!   id: u32,
//!   #[serde(rename = "userId")]
//!   user_id: u32,
//!   title: String,
//!   body: String,
//! }
//!
//! #[derive(Debug, ApiInput, Default, Serialize, Deserialize)]
//! #[allow(dead_code, non_snake_case)]
//! struct PostCreate {
//!   #[api(long = "user-id")]
//!   userId: u32,
//!   #[api(no_short, help = "Title of the post")]
//!   title: String,
//!   #[api(no_short)]
//!   body: String,
//! }
//! ```

use async_trait::async_trait;
use clap::{ArgMatches, Command, Id};
pub use crud_api_derive::*;
#[cfg(any(feature = "json", feature = "toml", feature = "yaml", feature = "csv"))]
use crud_tidy_viewer::{display_table, TableConfig};
use formats::OutputFormat;
#[doc(hidden)]
pub use formats::{
  clap_match_input_from_file, clap_match_output_format, clap_match_template, clap_output_format_decl,
};
use miette::Result;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{fmt::Debug, marker::Sized};

extern crate crud_api_derive;
#[doc(hidden)]
pub mod cli;
#[doc(hidden)]
pub mod completions;
#[doc(hidden)]
pub mod error;
mod formats;
#[doc(hidden)]
pub mod http;
#[doc(hidden)]
pub mod settings;

#[doc(hidden)]
pub struct ApiInputOptions {
  pub conflicts_with_all: Vec<Id>,
}

#[doc(hidden)]
pub trait ApiInput {
  /// Generate the clap command declatations.
  fn clap(app: Command, options: Option<ApiInputOptions>) -> Command;
  fn from_clap_matches(matches: &ArgMatches) -> Result<Self>
  where
    Self: Sized;
}

#[doc(hidden)]
#[async_trait]
pub trait Query {
  async fn query<P, R, Q>(&self, payload: Option<P>, argument: Option<Q>) -> Result<R>
  where
    P: Send + Serialize + Debug,
    R: Send + DeserializeOwned + Debug + Default,
    Q: Send + Serialize + Debug;
  async fn stream<P, Q>(
    &self,
    payload: Option<P>,
    argument: Option<Q>,
    filename: Option<String>,
  ) -> Result<()>
  where
    P: Send + Serialize + Debug,
    Q: Send + Serialize + Debug;
}

#[doc(hidden)]
pub trait Api {
  fn to_table_header(&self) -> Vec<String>;
  fn to_table(&self) -> Result<Vec<String>>;

  #[cfg(any(feature = "json", feature = "toml", feature = "yaml", feature = "csv"))]
  fn output(&self, format: Option<OutputFormat>) -> Result<()>
  where
    Self: Serialize + Debug,
  {
    use miette::IntoDiagnostic;

    //    log::trace!("{:#?}", self);

    let out = match format {
      Some(format) => match format {
        #[cfg(feature = "json")]
        OutputFormat::Json => Some(serde_json::to_string_pretty(self).into_diagnostic()?),
        #[cfg(feature = "toml")]
        OutputFormat::Toml => Some(toml::to_string(self).into_diagnostic()?),
        #[cfg(feature = "yaml")]
        OutputFormat::Yaml => Some(serde_yaml::to_string(self).into_diagnostic()?),
        #[cfg(feature = "csv")]
        OutputFormat::Csv => {
          let mut wtr = csv::Writer::from_writer(std::io::stdout());
          wtr.serialize(self).into_diagnostic()?;
          wtr.flush().into_diagnostic()?;
          None
        }
        #[cfg(feature = "csv")]
        OutputFormat::Tsv => {
          let mut wtr = csv::WriterBuilder::new()
            .delimiter(b'\t')
            .quote_style(csv::QuoteStyle::NonNumeric)
            .from_writer(std::io::stdout());
          wtr.serialize(self).into_diagnostic()?;
          wtr.flush().into_diagnostic()?;
          None
        }
      },
      None => Some(toml::to_string_pretty(self).into_diagnostic()?),
    };

    if let Some(out) = out {
      println!("{out}");
    }
    Ok(())
  }

  #[cfg(all(
    not(feature = "json"),
    not(feature = "toml"),
    not(feature = "yaml"),
    not(feature = "csv")
  ))]
  fn output(&self, _format: Option<OutputFormat>) -> Result<()>
  where
    Self: Serialize + Debug,
  {
    Ok(())
  }

  #[cfg(any(feature = "json", feature = "toml", feature = "yaml", feature = "csv"))]
  fn output_multiple(results: &Vec<Self>, format: Option<OutputFormat>) -> Result<()>
  where
    Self: Sized + Serialize + Debug,
  {
    use miette::IntoDiagnostic;

    //    log::trace!("{:#?}", self);
    let out = match format {
      Some(format) => match format {
        #[cfg(feature = "json")]
        OutputFormat::Json => Some(serde_json::to_string_pretty(results).into_diagnostic()?),
        #[cfg(feature = "toml")]
        OutputFormat::Toml => Some(toml::to_string(results).into_diagnostic()?),
        #[cfg(feature = "yaml")]
        OutputFormat::Yaml => Some(serde_yaml::to_string(results).into_diagnostic()?),
        #[cfg(feature = "csv")]
        OutputFormat::Csv => {
          let mut wtr = csv::Writer::from_writer(std::io::stdout());
          for result in results {
            wtr.serialize(result).into_diagnostic()?;
          }
          wtr.flush().into_diagnostic()?;
          None
        }
        #[cfg(feature = "csv")]
        OutputFormat::Tsv => {
          let mut wtr = csv::WriterBuilder::new()
            .delimiter(b'\t')
            .quote_style(csv::QuoteStyle::NonNumeric)
            .from_writer(std::io::stdout());
          for result in results {
            wtr.serialize(result).into_diagnostic()?;
          }
          wtr.flush().into_diagnostic()?;
          None
        }
      },
      None => {
        if !results.is_empty() {
          let mut table = vec![results.iter().next().unwrap().to_table_header()];
          table.append(
            &mut results
              .iter()
              .map(|row| row.to_table().expect("Formating data table"))
              .collect(),
          ); // FIXME: replace expect by something better from miette
          display_table(&table, TableConfig::default());
        }
        None
      }
    };
    if let Some(out) = out {
      println!("{out}");
    }
    Ok(())
  }

  #[cfg(all(
    not(feature = "json"),
    not(feature = "toml"),
    not(feature = "yaml"),
    not(feature = "csv")
  ))]
  fn output_multiple(_results: &Vec<Self>, _format: Option<OutputFormat>) -> Result<()>
  where
    Self: Sized + Serialize + Debug,
  {
    Ok(())
  }
}

/// An empty response. Use it in `result_struct`
#[derive(Debug, Default, Deserialize, Serialize)]
pub struct EmptyResponse {}
impl Api for EmptyResponse {
  fn to_table_header(&self) -> Vec<String> {
    vec![]
  }

  fn to_table(&self) -> Result<Vec<String>> {
    Ok(vec![])
  }
}

#[cfg(test)]
mod tests {
  #[test]
  fn it_works() {
    let result = 2 + 2;
    assert_eq!(result, 4);
  }
}