Skip to content

Commit

Permalink
fully qualify generated uses of httpmock and clap (#993)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahl authored Dec 5, 2024
1 parent 96e977a commit 04da119
Show file tree
Hide file tree
Showing 16 changed files with 4,619 additions and 4,495 deletions.
28 changes: 14 additions & 14 deletions progenitor-impl/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Generator {
Self { client, config }
}

pub fn get_command(cmd: CliCommand) -> clap::Command {
pub fn get_command(cmd: CliCommand) -> ::clap::Command {
match cmd {
#(
CliCommand::#cli_variants => Self::#cli_fns(),
Expand All @@ -133,7 +133,7 @@ impl Generator {
pub async fn execute(
&self,
cmd: CliCommand,
matches: &clap::ArgMatches,
matches: &::clap::ArgMatches,
) -> anyhow::Result<()> {
match cmd {
#(
Expand Down Expand Up @@ -217,9 +217,9 @@ impl Generator {
let fn_name = format_ident!("cli_{}", &method.operation_id);

let cli_fn = quote! {
pub fn #fn_name() -> clap::Command
pub fn #fn_name() -> ::clap::Command
{
clap::Command::new("")
::clap::Command::new("")
#parser_args
#about
#long_about
Expand Down Expand Up @@ -357,7 +357,7 @@ impl Generator {
};

let execute_fn = quote! {
pub async fn #fn_name(&self, matches: &clap::ArgMatches)
pub async fn #fn_name(&self, matches: &::clap::ArgMatches)
-> anyhow::Result<()>
{
let mut request = self.client.#op_name();
Expand All @@ -377,7 +377,7 @@ impl Generator {
let execute_trait = quote! {
fn #fn_name(
&self,
matches: &clap::ArgMatches,
matches: &::clap::ArgMatches,
request: &mut builder :: #struct_ident,
) -> anyhow::Result<()> {
Ok(())
Expand Down Expand Up @@ -517,19 +517,19 @@ impl Generator {

quote! {
.arg(
clap::Arg::new("json-body")
::clap::Arg::new("json-body")
.long("json-body")
.value_name("JSON-FILE")
// Required if we can't turn the body into individual
// parameters.
.required(#required)
.value_parser(clap::value_parser!(std::path::PathBuf))
.value_parser(::clap::value_parser!(std::path::PathBuf))
.help(#help)
)
.arg(
clap::Arg::new("json-body-template")
::clap::Arg::new("json-body-template")
.long("json-body-template")
.action(clap::ArgAction::SetTrue)
.action(::clap::ArgAction::SetTrue)
.help("XXX")
)
}
Expand Down Expand Up @@ -700,8 +700,8 @@ fn clap_arg(

maybe_var_names.map(|var_names| {
quote! {
clap::builder::TypedValueParser::map(
clap::builder::PossibleValuesParser::new([
::clap::builder::TypedValueParser::map(
::clap::builder::PossibleValuesParser::new([
#( #arg_type_name :: #var_names.to_string(), )*
]),
|s| #arg_type_name :: try_from(s).unwrap()
Expand All @@ -719,7 +719,7 @@ fn clap_arg(
// allowing for override implementations. A generated client may
// implement ValueParserFactory for a type to create a custom parser.
quote! {
clap::value_parser!(#arg_type_name)
::clap::value_parser!(#arg_type_name)
}
};

Expand All @@ -732,7 +732,7 @@ fn clap_arg(
};

quote! {
clap::Arg::new(#arg_name)
::clap::Arg::new(#arg_name)
.long(#arg_name)
.value_parser(#value_parser)
#required
Expand Down
42 changes: 21 additions & 21 deletions progenitor-impl/src/httpmock.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Oxide Computer Company
// Copyright 2024 Oxide Computer Company

//! Generation of mocking extensions for `httpmock`
Expand Down Expand Up @@ -93,36 +93,36 @@ impl Generator {
let code = quote! {
pub mod operations {

//! [`When`](httpmock::When) and [`Then`](httpmock::Then)
//! [`When`](::httpmock::When) and [`Then`](::httpmock::Then)
//! wrappers for each operation. Each can be converted to
//! its inner type with a call to `into_inner()`. This can
//! be used to explicitly deviate from permitted values.
use #crate_path::*;

#(
pub struct #when(httpmock::When);
pub struct #when(::httpmock::When);
#when_impl

pub struct #then(httpmock::Then);
pub struct #then(::httpmock::Then);
#then_impl
)*
}

/// An extension trait for [`MockServer`](httpmock::MockServer) that
/// An extension trait for [`MockServer`](::httpmock::MockServer) that
/// adds a method for each operation. These are the equivalent of
/// type-checked [`mock()`](httpmock::MockServer::mock) calls.
/// type-checked [`mock()`](::httpmock::MockServer::mock) calls.
pub trait MockServerExt {
#(
fn #op<F>(&self, config_fn: F) -> httpmock::Mock
fn #op<F>(&self, config_fn: F) -> ::httpmock::Mock
where
F: FnOnce(operations::#when, operations::#then);
)*
}

impl MockServerExt for httpmock::MockServer {
impl MockServerExt for ::httpmock::MockServer {
#(
fn #op<F>(&self, config_fn: F) -> httpmock::Mock
fn #op<F>(&self, config_fn: F) -> ::httpmock::Mock
where
F: FnOnce(operations::#when, operations::#then)
{
Expand Down Expand Up @@ -151,14 +151,14 @@ impl Generator {
let then = format_ident!("{}", then_name).to_token_stream();

let http_method = match &method.method {
HttpMethod::Get => quote! { httpmock::Method::GET },
HttpMethod::Put => quote! { httpmock::Method::PUT },
HttpMethod::Post => quote! { httpmock::Method::POST },
HttpMethod::Delete => quote! { httpmock::Method::DELETE },
HttpMethod::Options => quote! { httpmock::Method::OPTIONS },
HttpMethod::Head => quote! { httpmock::Method::HEAD },
HttpMethod::Patch => quote! { httpmock::Method::PATCH },
HttpMethod::Trace => quote! { httpmock::Method::TRACE },
HttpMethod::Get => quote! { ::httpmock::Method::GET },
HttpMethod::Put => quote! { ::httpmock::Method::PUT },
HttpMethod::Post => quote! { ::httpmock::Method::POST },
HttpMethod::Delete => quote! { ::httpmock::Method::DELETE },
HttpMethod::Options => quote! { ::httpmock::Method::OPTIONS },
HttpMethod::Head => quote! { ::httpmock::Method::HEAD },
HttpMethod::Patch => quote! { ::httpmock::Method::PATCH },
HttpMethod::Trace => quote! { ::httpmock::Method::TRACE },
};

let path_re = method.path.as_wildcard();
Expand Down Expand Up @@ -283,13 +283,13 @@ impl Generator {

let when_impl = quote! {
impl #when {
pub fn new(inner: httpmock::When) -> Self {
pub fn new(inner: ::httpmock::When) -> Self {
Self(inner
.method(#http_method)
.path_matches(regex::Regex::new(#path_re).unwrap()))
}

pub fn into_inner(self) -> httpmock::When {
pub fn into_inner(self) -> ::httpmock::When {
self.0
}

Expand Down Expand Up @@ -391,11 +391,11 @@ impl Generator {

let then_impl = quote! {
impl #then {
pub fn new(inner: httpmock::Then) -> Self {
pub fn new(inner: ::httpmock::Then) -> Self {
Self(inner)
}

pub fn into_inner(self) -> httpmock::Then {
pub fn into_inner(self) -> ::httpmock::Then {
self.0
}

Expand Down
Loading

0 comments on commit 04da119

Please sign in to comment.