Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add traits for client types #121

Merged
merged 4 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-121.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: feature
feature:
description: Generated service clients now implement the new `Service` and `AsyncService`
traits.
links:
- https://github.com/palantir/conjure-rust/pull/121
25 changes: 25 additions & 0 deletions conjure-codegen/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,22 @@ fn generate_inner(ctx: &Context, def: &ServiceDefinition, style: Style) -> Token
};
let name = ctx.type_name(&format!("{}{}", def.service_name().name(), suffix));

let service = match style {
Style::Async => quote!(AsyncService),
Style::Sync => quote!(Service),
};

let client_bound = match style {
Style::Async => quote!(AsyncClient),
Style::Sync => quote!(Client),
};

let service_name = def.service_name().name();
let version = match ctx.version() {
Some(version) => quote!(conjure_http::private::Option::Some(#version)),
None => quote!(conjure_http::private::Option::None),
};

let endpoints = def
.endpoints()
.iter()
Expand All @@ -59,10 +70,24 @@ fn generate_inner(ctx: &Context, def: &ServiceDefinition, style: Style) -> Token
#[derive(Clone, Debug)]
pub struct #name<T>(T);

impl<T> conjure_http::client::#service<T> for #name<T>
where
T: conjure_http::client::#client_bound,
{
const NAME: &'static str = #service_name;

const VERSION: conjure_http::private::Option<&'static str> = #version;

fn new(client: T) -> Self {
#name(client)
}
}

impl<T> #name<T>
where
T: conjure_http::client::#client_bound,
{
// FIXME remove in the next major version
/// Creates a new client.
#[inline]
pub fn new(client: T) -> #name<T> {
Expand Down
13 changes: 12 additions & 1 deletion conjure-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,21 @@ pub struct Context {
types: HashMap<TypeName, TypeContext>,
exhaustive: bool,
strip_prefix: Vec<String>,
version: Option<String>,
}

impl Context {
pub fn new(defs: &ConjureDefinition, exhaustive: bool, strip_prefix: Option<&str>) -> Context {
pub fn new(
defs: &ConjureDefinition,
exhaustive: bool,
strip_prefix: Option<&str>,
version: Option<&str>,
) -> Context {
let mut context = Context {
types: HashMap::new(),
exhaustive,
strip_prefix: vec![],
version: version.map(str::to_owned),
};

if let Some(strip_prefix) = strip_prefix {
Expand Down Expand Up @@ -956,6 +963,10 @@ impl Context {
_ => false,
}
}

pub fn version(&self) -> Option<&str> {
self.version.as_deref()
}
}

pub enum SetterBounds {
Expand Down
22 changes: 22 additions & 0 deletions conjure-codegen/src/example_types/another/test_service.rs

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

7 changes: 6 additions & 1 deletion conjure-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,12 @@ impl Config {
}

fn create_modules(&self, defs: &ConjureDefinition) -> ModuleTrie {
let context = Context::new(&defs, self.exhaustive, self.strip_prefix.as_deref());
let context = Context::new(
&defs,
self.exhaustive,
self.strip_prefix.as_deref(),
self.build_crate.as_ref().map(|v| &*v.version),
);

let mut root = ModuleTrie::new();

Expand Down
24 changes: 24 additions & 0 deletions conjure-http/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ use std::future::Future;
use std::io::Write;
use std::pin::Pin;

/// A trait implemented by generated blocking client interfaces for a Conjure service.
pub trait Service<C> {
/// The name of the service.
const NAME: &'static str;

/// The version of the Conjure definition defining the service, if known.
const VERSION: Option<&'static str>;

/// Creates a new service wrapping an HTTP client.
fn new(client: C) -> Self;
}

/// A trait implemented by HTTP client implementations.
pub trait Client {
/// The client's binary request body writer type.
Expand Down Expand Up @@ -55,6 +67,18 @@ pub trait Client {
U: VisitResponse<Self::BinaryBody>;
}

/// A trait implemented by generated async client interfaces for a Conjure service.
pub trait AsyncService<C> {
/// The name of the service.
const NAME: &'static str;

/// The version of the Conjure definition defining the service, if known.
const VERSION: Option<&'static str>;

/// Creates a new service wrapping an async HTTP client.
fn new(client: C) -> Self;
}

/// A trait implemented by async HTTP client implementations.
pub trait AsyncClient {
/// The client's binary request body writer type.
Expand Down
1 change: 1 addition & 0 deletions conjure-http/src/private/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use conjure_error::Error;
pub use conjure_serde::json;
pub use http;
pub use std::future::Future;
pub use std::option::Option;
pub use std::pin::Pin;

pub use crate::private::client::*;
Expand Down
22 changes: 22 additions & 0 deletions example-api/src/another/test_service.rs

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