Skip to content

Commit

Permalink
feat(generator/rust): introduce dyn-compatible traits (#375)
Browse files Browse the repository at this point in the history
These are generated, but not used, for now.
  • Loading branch information
coryan authored Dec 9, 2024
1 parent edd1b1a commit 2c05b48
Show file tree
Hide file tree
Showing 35 changed files with 2,184 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions generator/templates/rust/crate/Cargo.toml.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ publish = false
{{/NotForPublication}}

[dependencies]
async-trait = "0.1.83"
serde = { version = "1.0.214", features = ["serde_derive"] }
serde_with = { version = "3.11.0", features = ["base64"] }
serde_json = "1.0.132"
Expand Down
1 change: 1 addition & 0 deletions generator/templates/rust/crate/src/lib.rs.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ impl ConfigBuilder {

{{/HasServices}}
{{#Services}}
{{! TODO(#370) - remove once the new structure is fully in place }}
pub type {{NameToPascal}}Client = crate::transport::{{NameToPascal}};

{{/Services}}
52 changes: 52 additions & 0 deletions generator/templates/rust/crate/src/traits/dyntraits.rs.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{{!
Copyright 2024 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
}}
// Copyright {{CopyrightYear}} Google LLC
{{#BoilerPlate}}
//{{{.}}}
{{/BoilerPlate}}
{{#Services}}

/// A dyn-compatible, crate-private version of `{{NameToPascal}}`.
#[async_trait::async_trait]
pub trait {{NameToPascal}}: Send + Sync {
{{#Methods}}
{{#DocLines}}
{{{.}}}
{{/DocLines}}
async fn {{NameToSnake}}(
&self,
req: {{InputTypeName}}
) -> crate::Result<{{OutputTypeName}}>;

{{/Methods}}
}

/// All implementations of [crate::traits::{{NameToPascal}}] also implement [{{NameToPascal}}].
#[async_trait::async_trait]
impl<T: crate::traits::{{NameToPascal}}> {{NameToPascal}} for T {
{{#Methods}}
/// Forwards the call to the implementation provided by `T`.
async fn {{NameToSnake}}(
&self,
req: {{InputTypeName}}
) -> crate::Result<{{OutputTypeName}}> {
let response = T::{{NameToSnake}}(self, req).await?;
Ok(response)
}

{{/Methods}}
}
{{/Services}}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ limitations under the License.

use gax::error::Error;

{{! TODO(#370) - enable the warning once the new structure is fully in place }}
#[allow(dead_code)]
pub(crate) mod dyntraits;

{{/HasServices}}
{{#Services}}
{{#DocLines}}
Expand Down
1 change: 1 addition & 0 deletions generator/testdata/rust/gclient/golden/iam/v1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ categories.workspace = true
publish = false

[dependencies]
async-trait = "0.1.83"
serde = { version = "1.0.214", features = ["serde_derive"] }
serde_with = { version = "3.11.0", features = ["base64"] }
serde_json = "1.0.132"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Code generated by sidekick. DO NOT EDIT.

/// A dyn-compatible, crate-private version of `Iampolicy`.
#[async_trait::async_trait]
pub trait Iampolicy: Send + Sync {
/// Sets the access control policy on the specified resource. Replaces any
/// existing policy.
///
/// Can return `NOT_FOUND`, `INVALID_ARGUMENT`, and `PERMISSION_DENIED` errors.
async fn set_iam_policy(
&self,
req: crate::model::SetIamPolicyRequest
) -> crate::Result<crate::model::Policy>;

/// Gets the access control policy for a resource.
/// Returns an empty policy if the resource exists and does not have a policy
/// set.
async fn get_iam_policy(
&self,
req: crate::model::GetIamPolicyRequest
) -> crate::Result<crate::model::Policy>;

/// Returns permissions that a caller has on the specified resource.
/// If the resource does not exist, this will return an empty set of
/// permissions, not a `NOT_FOUND` error.
///
/// Note: This operation is designed to be used for building permission-aware
/// UIs and command-line tools, not for authorization checking. This operation
/// may "fail open" without warning.
async fn test_iam_permissions(
&self,
req: crate::model::TestIamPermissionsRequest
) -> crate::Result<crate::model::TestIamPermissionsResponse>;

}

/// All implementations of [crate::traits::Iampolicy] also implement [Iampolicy].
#[async_trait::async_trait]
impl<T: crate::traits::Iampolicy> Iampolicy for T {
/// Forwards the call to the implementation provided by `T`.
async fn set_iam_policy(
&self,
req: crate::model::SetIamPolicyRequest
) -> crate::Result<crate::model::Policy> {
let response = T::set_iam_policy(self, req).await?;
Ok(response)
}

/// Forwards the call to the implementation provided by `T`.
async fn get_iam_policy(
&self,
req: crate::model::GetIamPolicyRequest
) -> crate::Result<crate::model::Policy> {
let response = T::get_iam_policy(self, req).await?;
Ok(response)
}

/// Forwards the call to the implementation provided by `T`.
async fn test_iam_permissions(
&self,
req: crate::model::TestIamPermissionsRequest
) -> crate::Result<crate::model::TestIamPermissionsResponse> {
let response = T::test_iam_permissions(self, req).await?;
Ok(response)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

use gax::error::Error;

#[allow(dead_code)]
pub(crate) mod dyntraits;

/// API Overview
///
/// Manages Identity and Access Management (IAM) policies.
Expand Down
1 change: 1 addition & 0 deletions generator/testdata/rust/gclient/golden/location/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ categories.workspace = true
publish = false

[dependencies]
async-trait = "0.1.83"
serde = { version = "1.0.214", features = ["serde_derive"] }
serde_with = { version = "3.11.0", features = ["base64"] }
serde_json = "1.0.132"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Code generated by sidekick. DO NOT EDIT.

/// A dyn-compatible, crate-private version of `Locations`.
#[async_trait::async_trait]
pub trait Locations: Send + Sync {
/// Lists information about the supported locations for this service.
async fn list_locations(
&self,
req: crate::model::ListLocationsRequest
) -> crate::Result<crate::model::ListLocationsResponse>;

/// Gets information about a location.
async fn get_location(
&self,
req: crate::model::GetLocationRequest
) -> crate::Result<crate::model::Location>;

}

/// All implementations of [crate::traits::Locations] also implement [Locations].
#[async_trait::async_trait]
impl<T: crate::traits::Locations> Locations for T {
/// Forwards the call to the implementation provided by `T`.
async fn list_locations(
&self,
req: crate::model::ListLocationsRequest
) -> crate::Result<crate::model::ListLocationsResponse> {
let response = T::list_locations(self, req).await?;
Ok(response)
}

/// Forwards the call to the implementation provided by `T`.
async fn get_location(
&self,
req: crate::model::GetLocationRequest
) -> crate::Result<crate::model::Location> {
let response = T::get_location(self, req).await?;
Ok(response)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

use gax::error::Error;

#[allow(dead_code)]
pub(crate) mod dyntraits;

/// An abstract interface that provides location-related information for
/// a service. Service-specific metadata is provided through the
/// [Location.metadata][google.cloud.location.Location.metadata] field.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ categories.workspace = true
publish = false

[dependencies]
async-trait = "0.1.83"
serde = { version = "1.0.214", features = ["serde_derive"] }
serde_with = { version = "3.11.0", features = ["base64"] }
serde_json = "1.0.132"
Expand Down
Loading

0 comments on commit 2c05b48

Please sign in to comment.