Skip to content

Commit

Permalink
cleanup(generator/rust): rename traits module (#891)
Browse files Browse the repository at this point in the history
Use a name that tells us something about what it does (or provides) vs.
how it is implemented.
  • Loading branch information
coryan authored Jan 29, 2025
1 parent 9330f2d commit c2edb7c
Show file tree
Hide file tree
Showing 232 changed files with 2,018 additions and 1,764 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ pub mod {{NameToSnake}} {
/// Common implementation for [crate::client::{{NameToPascal}}] request builders.
#[derive(Clone, Debug)]
pub struct RequestBuilder<R: std::default::Default> {
stub: Arc<dyn crate::traits::dyntraits::{{NameToPascal}}>,
stub: Arc<dyn crate::stubs::dynamic::{{NameToPascal}}>,
request: R,
options: gax::options::RequestOptions,
}

impl<R> RequestBuilder<R>
where R: std::default::Default {
pub(crate) fn new(stub: Arc<dyn crate::traits::dyntraits::{{NameToPascal}}>) -> Self {
pub(crate) fn new(stub: Arc<dyn crate::stubs::dynamic::{{NameToPascal}}>) -> Self {
Self {
stub,
request: R::default(),
Expand All @@ -48,7 +48,7 @@ pub mod {{NameToSnake}} {
pub struct {{NameToPascal}}(RequestBuilder<{{InputTypeName}}>);

impl {{NameToPascal}} {
pub(crate) fn new(stub: Arc<dyn crate::traits::dyntraits::{{ServiceNameToPascal}}>) -> Self {
pub(crate) fn new(stub: Arc<dyn crate::stubs::dynamic::{{ServiceNameToPascal}}>) -> Self {
Self(
RequestBuilder::new(stub)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::sync::Arc;
{{/HasServices}}
{{#Services}}

/// An implementation of [crate::traits::{{NameToPascal}}] to make requests with.
/// An implementation of [crate::stubs::{{NameToPascal}}] to make requests with.
///
/// `{{NameToPascal}}` has various configuration parameters, but the defaults
/// are set to work with most applications.
Expand All @@ -41,7 +41,7 @@ use std::sync::Arc;
{{/DocLines}}
#[derive(Clone, Debug)]
pub struct {{NameToPascal}} {
inner: Arc<dyn crate::traits::dyntraits::{{NameToPascal}}>,
inner: Arc<dyn crate::stubs::dynamic::{{NameToPascal}}>,
}

impl {{NameToPascal}} {
Expand All @@ -61,22 +61,22 @@ impl {{NameToPascal}} {
/// The most common case for calling this function is when mocking the
/// client.
pub fn from_stub<T>(stub: T) -> Self
where T: crate::traits::{{NameToPascal}} + 'static {
where T: crate::stubs::{{NameToPascal}} + 'static {
Self { inner: Arc::new(stub) }
}

async fn build_inner(conf: gax::options::ClientConfig) -> Result<Arc<dyn crate::traits::dyntraits::{{NameToPascal}}>> {
async fn build_inner(conf: gax::options::ClientConfig) -> Result<Arc<dyn crate::stubs::dynamic::{{NameToPascal}}>> {
if conf.tracing_enabled() {
return Ok(Arc::new(Self::build_with_tracing(conf).await?));
}
Ok(Arc::new(Self::build_transport(conf).await?))
}

async fn build_transport(conf: gax::options::ClientConfig) -> Result<impl crate::traits::{{NameToPascal}}> {
async fn build_transport(conf: gax::options::ClientConfig) -> Result<impl crate::stubs::{{NameToPascal}}> {
crate::transport::{{NameToPascal}}::new(conf).await
}

async fn build_with_tracing(conf: gax::options::ClientConfig) -> Result<impl crate::traits::{{NameToPascal}}> {
async fn build_with_tracing(conf: gax::options::ClientConfig) -> Result<impl crate::stubs::{{NameToPascal}}> {
Self::build_transport(conf).await.map(crate::tracing::{{NameToPascal}}::new)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ pub mod model;
{{#HasServices}}
pub(crate) use gax::Result;

/// The traits implemented by this client library.
{{! Google APIs often use angle brackets for <PLACEHOLDERS>, rustdoc does not like those. }}
#[allow(rustdoc::invalid_html_tags)]
{{! We use explicit links because it is easier to generate the code with them. }}
#[allow(rustdoc::redundant_explicit_links)]
pub mod traits;
pub mod stubs;

/// Concrete implementations of this client library traits.
pub mod client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ limitations under the License.
//{{{.}}}
{{/BoilerPlate}}
{{#HasServices}}

//! Traits to mock the clients in this library.
//!
//! Application developers may need to mock the clients in this library to test
//! how their application responds. Such applications should define mocks that
//! implement one of the traits defined in this module, initialize the client
//! with an instance of this mock in their tests, and verify their application
//! responds as expected.

{{! TODO(#742) - remove this workaround once the bug is fixed }}
#![allow(rustdoc::broken_intra_doc_links)]

use gax::error::Error;

pub(crate) mod dyntraits;
pub(crate) mod dynamic;

{{/HasServices}}
{{#Services}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pub trait {{NameToPascal}}: std::fmt::Debug + Send + Sync {
{{/HasLROs}}
}

/// All implementations of [crate::traits::{{NameToPascal}}] also implement [{{NameToPascal}}].
/// All implementations of [crate::stubs::{{NameToPascal}}] also implement [{{NameToPascal}}].
#[async_trait::async_trait]
impl<T: crate::traits::{{NameToPascal}}> {{NameToPascal}} for T {
impl<T: crate::stubs::{{NameToPascal}}> {{NameToPascal}} for T {
{{#Methods}}
/// Forwards the call to the implementation provided by `T`.
async fn {{NameToSnake}}(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ use crate::Result;

{{/HasServices}}
{{#Services}}
/// Implements a [{{NameToPascal}}](crate::traits::{{NametoPascal}}) decorator for logging and tracing.
/// Implements a [{{NameToPascal}}](crate::stubs::{{NametoPascal}}) decorator for logging and tracing.
#[derive(Clone, Debug)]
pub struct {{NameToPascal}}<T>
where T: crate::traits::{{NameToPascal}} + std::fmt::Debug + Send + Sync {
where T: crate::stubs::{{NameToPascal}} + std::fmt::Debug + Send + Sync {
inner: T,
}

impl<T> {{NameToPascal}}<T>
where T: crate::traits::{{NameToPascal}} + std::fmt::Debug + Send + Sync {
where T: crate::stubs::{{NameToPascal}} + std::fmt::Debug + Send + Sync {
pub fn new(inner: T) -> Self {
Self { inner }
}
}

impl<T> crate::traits::{{NameToPascal}} for {{NameToPascal}}<T>
where T: crate::traits::{{NameToPascal}} + std::fmt::Debug + Send + Sync {
impl<T> crate::stubs::{{NameToPascal}} for {{NameToPascal}}<T>
where T: crate::stubs::{{NameToPascal}} + std::fmt::Debug + Send + Sync {
{{#Methods}}
#[tracing::instrument(ret)]
async fn {{NameToSnake}}(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use gax::error::Error;

{{/HasServices}}
{{#Services}}
/// Implements [{{NameToPascal}}](crate::traits::{{NameToPascal}}) using a [gax::http_client::ReqwestClient].
/// Implements [{{NameToPascal}}](crate::stubs::{{NameToPascal}}) using a [gax::http_client::ReqwestClient].
#[derive(Clone)]
pub struct {{NameToPascal}} {
inner: gax::http_client::ReqwestClient,
Expand All @@ -47,7 +47,7 @@ impl {{NameToPascal}} {
}
}
impl crate::traits::{{NameToPascal}} for {{NameToPascal}} {
impl crate::stubs::{{NameToPascal}} for {{NameToPascal}} {
{{#Methods}}
async fn {{NameToSnake}}(
&self,
Expand Down
Loading

0 comments on commit c2edb7c

Please sign in to comment.