crud_api_endpoint/
api_run.rs

1use crate::Header;
2use darling::{FromDeriveInput, FromMeta};
3use syn::Ident;
4
5// Copy from cruds.rs/CrudsConfig
6/// Information block for `#[derive(ApiRun)]`.
7///
8/// # Example
9/// ```rust
10/// # use crud_api::ApiRun;
11/// # use crud_auth::CrudAuth;
12/// # use crud_auth_no_auth::Auth;
13/// # use miette::IntoDiagnostic;
14/// # #[derive(ApiRun)]
15/// #[api(infos(
16///   base_url = "http://jsonplaceholder.typicode.com",
17///   name = "jsonplaceholder",
18///   qualifier = "com",
19///   organisation = "typicode",
20///   env_prefix = "JSONPLACEHOLDER"
21/// ))]
22/// # struct JSONPlaceHoder;
23///
24/// ```
25#[derive(Debug, Default, FromMeta)]
26#[allow(dead_code)]
27pub struct ApiInformation {
28  /// Name of the application. If omitted, the crate's name is used.
29  /// This name appears in help and is used to generate the config's path.
30  pub name: Option<String>,
31  /// The base URL of the api. All the endpoints are relative to this URL.
32  pub base_url: String,
33  /// Short description of the application. It appears in the help.
34  pub about: Option<String>,
35  /// The version of the application. If omitted, the crate's version is used.
36  pub version: Option<String>,
37  /// The author of the application. If omitted, the crate's author is used.
38  pub author: Option<String>,
39  /// A qualifier to generate the configuration path. If omitted, an empty
40  /// string is used. The qualifier can be the TLD of the application's
41  /// url. Example: "com"
42  pub qualifier: Option<String>,
43  /// A organisation to generate the configuration path. If omitted, an empty
44  /// string is used. The organisation can be the domain of the application's
45  /// url. Example: "foobar" in "foobar.com".
46  pub organisation: Option<String>,
47  /// A prefix for environment variables.Some parameters can be
48  /// passed/overried by environment variables (base-url, auth-token). The
49  /// environment variables will be generate by prefixing this
50  /// parameter. Example: When `env_prefix` is "_MYAPP_" the `base_url`
51  /// parameter become `MYPAPP_BASE_URL`. If omitted, the `env_prefix` is
52  /// "_APP_".
53  pub env_prefix: Option<String>,
54}
55
56/// Attribute used by `#[derive(ApiRun)]`.
57///
58/// It declare a new cli application:
59/// - create the cli
60/// - handle the command
61///
62/// It can take parameters by `#[api(...)]`.
63/// Only one `#[derive(ApiRun)]` should be present in your application.
64#[derive(FromDeriveInput)]
65#[darling(attributes(api))]
66pub struct ApiRun {
67  /// Information block of the application.
68  ///
69  /// # Example
70  /// ```rust
71  /// # use crud_api::ApiRun;
72  /// # use crud_auth::CrudAuth;
73  /// # use crud_auth_no_auth::Auth;
74  /// # use miette::IntoDiagnostic;
75  /// # #[derive(ApiRun)]
76  /// #[api(infos(
77  ///   base_url = "http://jsonplaceholder.typicode.com",
78  ///   name = "jsonplaceholder",
79  ///   qualifier = "com",
80  ///   organisation = "typicode",
81  ///   env_prefix = "JSONPLACEHOLDER"
82  /// ))]
83  /// # struct JSONPlaceHoder;
84  ///
85  /// ```
86  #[darling(default)]
87  pub infos: ApiInformation,
88  /// Name of the struct derived by `ApiRun`.
89  /// Used to implentent the `run` function.
90
91  /// Add extra header to all endpoints.
92  #[darling(default)]
93  #[darling(multiple)]
94  pub extra_header: Vec<Header>,
95  #[doc(hidden)]
96  pub ident: Ident,
97  //  attrs: Vec<syn::Attribute>,
98}