Skip to content

Commit

Permalink
Use re-exports to make depending on propolis-client less cumbersome
Browse files Browse the repository at this point in the history
  • Loading branch information
lif committed Sep 15, 2022
1 parent 183ef35 commit 28f397c
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 38 deletions.
18 changes: 8 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 19 additions & 20 deletions README.md
Expand Up @@ -28,23 +28,6 @@ You'll need to add add the following to `Cargo.toml`:
```diff
[dependencies]
+progenitor = { git = "https://github.com/oxidecomputer/progenitor" }
+reqwest = { version = "0.11", features = ["json", "stream"] }
+serde = { version = "1.0", features = ["derive"] }
```

In addition, if the OpenAPI document contains string types with the `format`
field set to `date` or `date-time`, include

```diff
[dependencies]
+chrono = { version = "0.4", features = ["serde"] }
```

Similarly if there is a `format` field set to `uuid`:

```diff
[dependencies]
+uuid = { version = "1.0.0", features = ["serde", "v4"] }
```

The macro has some additional fancy options to control the generated code:
Expand Down Expand Up @@ -109,11 +92,23 @@ You'll need to add add the following to `Cargo.toml`:
+serde_json = "1.0"
```

(`chrono` and `uuid` as above)

Note that `progenitor` is used by `build.rs`, but the generated code required
`progenitor-client`.

In addition, if the OpenAPI document contains string types with the `format`
field set to `date` or `date-time`, include

```diff
[dependencies]
+chrono = { version = "0.4", features = ["serde"] }
```

Similarly if there is a `format` field set to `uuid`:

```diff
[dependencies]
+uuid = { version = "1.0.0", features = ["serde", "v4"] }
```

### Static Crate

Expand All @@ -139,7 +134,7 @@ For example:

This will produce a package in the specified directory. The output has no
persistent dependency on Progenitor including the `progenitor-client` crate.
Here's a excerpt from the emitted `Cargo.toml`:
Here's an excerpt from the emitted `Cargo.toml`:

```toml
[dependencies]
Expand All @@ -154,3 +149,7 @@ uuid = { version = ">=0.8.0, <2.0.0", features = ["serde", "v4"] }

Note that there is a dependency on `percent-encoding` which macro- and
build.rs-generated clients is included from `progenitor-client`.

Additionally, note that `chrono` is only required if the OpenAPI document
contains string types with the `format` field set to `date` or `date-time`,
and `uuid` is only required if there is a `format` field set to `uuid`.
5 changes: 0 additions & 5 deletions example-macro/Cargo.toml
Expand Up @@ -5,10 +5,5 @@ authors = ["Adam H. Leventhal <ahl@oxidecomputer.com>"]
edition = "2021"

[dependencies]
chrono = { version = "0.4", features = ["serde"] }
progenitor = { path = "../progenitor" }
reqwest = { git = "https://github.com/seanmonstar/reqwest", rev = "6ceb23958c8b6d7f1d8ee093f0ad73184d133d40", features = ["json", "stream"] }
#reqwest = { version = "0.11", features = ["json", "stream"] }
schemars = { version = "0.8.10", features = ["uuid1"] }
serde = { version = "1.0", features = ["derive"] }
uuid = { version = "1.0", features = ["serde", "v4"] }
5 changes: 3 additions & 2 deletions progenitor-client/Cargo.toml
Expand Up @@ -11,8 +11,9 @@ bytes = "1.2.1"
futures-core = "0.3.24"
percent-encoding = "2.1"
reqwest = { git = "https://github.com/seanmonstar/reqwest", rev = "6ceb23958c8b6d7f1d8ee093f0ad73184d133d40", features = ["json", "stream"] }
#reqwest = { version = "0.11", default-features = false, features = ["json", "stream"] }
serde = "1.0"
chrono = { version = "0.4", features = ["serde"] }
uuid = { version = "1.0.0", features = ["serde", "v4"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serde_urlencoded = "0.7.1"
base64 = "0.13"
Expand Down
7 changes: 7 additions & 0 deletions progenitor-client/src/lib.rs
Expand Up @@ -4,6 +4,13 @@ mod progenitor_client;

pub use crate::progenitor_client::*;

pub mod deps {
pub use ::chrono;
pub use ::reqwest;
pub use ::serde;
pub use ::uuid;
}

// For stand-alone crates, rather than adding a dependency on
// progenitor-client, we simply dump the code right in. This means we don't
// need to determine the provenance of progenitor (crates.io, github, etc.)
Expand Down
9 changes: 8 additions & 1 deletion progenitor-impl/src/lib.rs
Expand Up @@ -127,7 +127,10 @@ impl Generator {
let mut type_settings = TypeSpaceSettings::default();
type_settings
.with_type_mod("types")
.with_struct_builder(settings.interface == InterfaceStyle::Builder);
.with_struct_builder(settings.interface == InterfaceStyle::Builder)
.with_serde_crate_location(
"progenitor_client::deps::serde".to_string(),
);
settings.extra_derives.iter().for_each(|derive| {
let _ = type_settings.with_derive(derive.clone());
});
Expand Down Expand Up @@ -225,6 +228,8 @@ impl Generator {
use progenitor_client::{encode_path, RequestBuilderExt};

pub mod types {
#[allow(unused_imports)]
use super::*;
use serde::{Deserialize, Serialize};

// This may be used by some impl Deserialize, but not all.
Expand Down Expand Up @@ -389,6 +394,8 @@ impl Generator {
}

/// Render text output.
// TODO: configuration for whether to use re-exports from progenitor-client
// (standalone crate vs. build.rs codegen)
pub fn generate_text(&mut self, spec: &OpenAPI) -> Result<String> {
self.generate_text_impl(
spec,
Expand Down
3 changes: 3 additions & 0 deletions progenitor-macro/src/lib.rs
Expand Up @@ -182,6 +182,9 @@ fn do_generate_api(item: TokenStream) -> Result<TokenStream, syn::Error> {
// The progenitor_client is tautologically visible from macro
// consumers.
use progenitor::progenitor_client;
// Re-exported such that macro consumers don't have to maintain
// a matching dependency on the same reqwest version we use.
use progenitor::progenitor_client::deps::*;

#code

Expand Down

0 comments on commit 28f397c

Please sign in to comment.