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 26abdce
Show file tree
Hide file tree
Showing 20 changed files with 666 additions and 76 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
21 changes: 21 additions & 0 deletions progenitor-impl/tests/output/buildomat-builder-tagged.out
Expand Up @@ -2,10 +2,13 @@
use progenitor_client::{encode_path, RequestBuilderExt};
pub use progenitor_client::{generate_websocket_key, ByteStream, Error, ResponseValue};
pub mod types {
#[allow(unused_imports)]
use super::*;
use serde::{Deserialize, Serialize};
#[allow(unused_imports)]
use std::convert::TryFrom;
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct Task {
pub id: String,
pub name: String,
Expand All @@ -21,6 +24,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct TaskEvent {
pub payload: String,
pub seq: u32,
Expand All @@ -35,6 +39,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct TaskOutput {
pub id: String,
pub path: String,
Expand All @@ -48,6 +53,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct TaskSubmit {
pub name: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
Expand All @@ -62,6 +68,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct TaskSubmitResult {
pub id: String,
}
Expand All @@ -73,6 +80,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct UploadedChunk {
pub id: String,
}
Expand All @@ -84,6 +92,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct UserCreate {
pub name: String,
}
Expand All @@ -95,6 +104,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct UserCreateResult {
pub id: String,
pub name: String,
Expand All @@ -108,6 +118,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WhoamiResult {
pub id: String,
pub name: String,
Expand All @@ -120,6 +131,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct Worker {
pub deleted: bool,
pub id: String,
Expand All @@ -138,6 +150,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WorkerAddOutput {
pub chunks: Vec<String>,
pub path: String,
Expand All @@ -151,6 +164,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WorkerAppendTask {
pub payload: String,
pub stream: String,
Expand All @@ -164,6 +178,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WorkerBootstrap {
pub bootstrap: String,
pub token: String,
Expand All @@ -176,6 +191,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WorkerBootstrapResult {
pub id: String,
}
Expand All @@ -187,6 +203,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WorkerCompleteTask {
pub failed: bool,
}
Expand All @@ -198,6 +215,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WorkerPingResult {
pub poweroff: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand All @@ -211,6 +229,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WorkerPingTask {
pub id: String,
pub output_rules: Vec<String>,
Expand All @@ -224,6 +243,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WorkerTask {
pub id: String,
pub name: String,
Expand All @@ -237,6 +257,7 @@ pub mod types {
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(crate = "progenitor_client::deps::serde")]
pub struct WorkersResult {
pub workers: Vec<Worker>,
}
Expand Down

0 comments on commit 26abdce

Please sign in to comment.