Skip to content

Commit e6cc295

Browse files
authored
Derive Macro for typespec_client_core::http::Model (#1772)
1 parent 851994b commit e6cc295

File tree

26 files changed

+682
-52
lines changed

26 files changed

+682
-52
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ resolver = "2"
33
members = [
44
"sdk/typespec",
55
"sdk/typespec/typespec_client_core",
6+
"sdk/typespec/typespec_derive",
67
"sdk/core/azure_core",
78
"sdk/core/azure_core_amqp",
89
"sdk/identity/azure_identity",
@@ -27,6 +28,9 @@ default-features = false
2728
path = "sdk/typespec/typespec_client_core"
2829
default-features = false
2930

31+
[workspace.dependencies.typespec_derive]
32+
path = "sdk/typespec/typespec_derive"
33+
3034
[workspace.dependencies.azure_core]
3135
path = "sdk/core/azure_core"
3236

@@ -49,6 +53,7 @@ async-stream = { version = "0.3.5" }
4953
async-trait = "0.1"
5054
base64 = "0.22"
5155
bytes = "1.0"
56+
cargo_metadata = "0.18.1"
5257
clap = { version = "4.4.16", features = ["derive"] }
5358
dyn-clone = "1.0"
5459
fe2o3-amqp = { version = "0.12", features = ["native-tls", "tracing", "uuid"] }
@@ -65,6 +70,7 @@ once_cell = "1.18"
6570
openssl = { version = "0.10.46" }
6671
paste = "1.0"
6772
pin-project = "1.0"
73+
proc-macro2 = "1.0.86"
6874
quick-xml = { version = "0.31", features = ["serialize", "serde-types"] }
6975
rand = "0.8"
7076
reqwest = { version = "0.12", features = [
@@ -79,6 +85,8 @@ serde_json = "1.0"
7985
serde_test = "1"
8086
serial_test = "3.0"
8187
sha2 = { version = "0.10" }
88+
syn = { version = "2.0.74", features = ["full"] }
89+
quote = "1.0.36"
8290
thiserror = "1.0"
8391
time = { version = "0.3.10", features = [
8492
"serde-well-known",

sdk/core/azure_core/src/lib.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ pub use options::*;
4242
pub use pipeline::*;
4343
pub use policies::*;
4444
pub use typespec_client_core::http::{Model, PinnedStream, Response, ResponseBody};
45-
pub use typespec_client_core::json_model;
46-
#[cfg(feature = "xml")]
47-
pub use typespec_client_core::xml_model;
4845

4946
// Re-export typespec types that are not specific to Azure.
5047
pub use typespec::{Error, Result};

sdk/identity/azure_identity/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ async-trait = { workspace = true }
2525
openssl = { workspace = true, optional = true }
2626
uuid = { workspace = true, features = ["v4"] }
2727
pin-project = { workspace = true }
28+
typespec_client_core = { workspace = true, features = ["derive"] }
2829

2930
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
3031
async-process = { workspace = true }

sdk/identity/azure_identity/src/federated_credentials_flow/login_response.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use azure_core::auth::Secret;
55
use serde::{Deserialize, Deserializer};
66
use time::OffsetDateTime;
7+
use typespec_client_core::Model;
78

89
#[derive(Debug, Clone, Deserialize)]
910
struct RawLoginResponse {
@@ -16,7 +17,7 @@ struct RawLoginResponse {
1617
access_token: String,
1718
}
1819

19-
#[derive(Debug, Clone)]
20+
#[derive(Model, Debug, Clone)]
2021
pub struct LoginResponse {
2122
pub token_type: String,
2223
pub expires_in: u64,
@@ -37,8 +38,6 @@ impl<'de> Deserialize<'de> for LoginResponse {
3738
}
3839
}
3940

40-
azure_core::json_model!(LoginResponse);
41-
4241
impl LoginResponse {
4342
pub fn access_token(&self) -> &Secret {
4443
&self.access_token

sdk/identity/azure_identity/src/refresh_token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use azure_core::{
1414
use serde::Deserialize;
1515
use std::fmt;
1616
use std::sync::Arc;
17+
use typespec_client_core::Model;
1718
use url::form_urlencoded;
1819

1920
/// Exchange a refresh token for a new access token and refresh token
@@ -64,7 +65,7 @@ pub async fn exchange(
6465
}
6566

6667
/// A refresh token
67-
#[derive(Debug, Clone, Deserialize)]
68+
#[derive(Model, Debug, Clone, Deserialize)]
6869
pub struct RefreshTokenResponse {
6970
token_type: String,
7071
#[serde(rename = "scope", deserialize_with = "deserialize::split")]
@@ -74,7 +75,6 @@ pub struct RefreshTokenResponse {
7475
access_token: Secret,
7576
refresh_token: Secret,
7677
}
77-
azure_core::json_model!(RefreshTokenResponse);
7878

7979
impl RefreshTokenResponse {
8080
/// Returns the `token_type`. Always `Bearer` for Azure AD.

sdk/identity/azure_identity/src/token_credentials/client_certificate_credentials.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use openssl::{
2121
use serde::Deserialize;
2222
use std::{str, sync::Arc, time::Duration};
2323
use time::OffsetDateTime;
24+
use typespec_client_core::http::Model;
2425
use url::{form_urlencoded, Url};
2526

2627
/// Refresh time to use in seconds
@@ -330,7 +331,7 @@ impl ClientCertificateCredential {
330331
}
331332
}
332333

333-
#[derive(Deserialize, Debug, Default)]
334+
#[derive(Model, Deserialize, Debug, Default)]
334335
#[serde(default)]
335336
struct AadTokenResponse {
336337
token_type: String,
@@ -339,8 +340,6 @@ struct AadTokenResponse {
339340
access_token: String,
340341
}
341342

342-
azure_core::json_model!(AadTokenResponse);
343-
344343
fn get_encoded_cert(cert: &X509) -> azure_core::Result<String> {
345344
Ok(format!(
346345
"\"{}\"",

sdk/typespec/src/error/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
//! Interfaces for working with errors.
55
6+
#[cfg(feature = "http")]
67
use http_types::StatusCode;
8+
79
use std::borrow::Cow;
810
use std::fmt::{Debug, Display};
911

@@ -51,6 +53,7 @@ impl ErrorKind {
5153
impl Display for ErrorKind {
5254
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5355
match self {
56+
#[cfg(feature = "http")]
5457
ErrorKind::HttpResponse { status, error_code } => {
5558
write!(
5659
f,

sdk/typespec/typespec_client_core/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ time = { workspace = true }
2727
tokio = { workspace = true, optional = true }
2828
tracing = { workspace = true }
2929
typespec = { workspace = true, default-features = false }
30+
typespec_derive = { workspace = true, optional = true }
3031
url = { workspace = true }
3132

3233
[target.'cfg(not(target_family = "wasm"))'.dependencies]
@@ -39,6 +40,7 @@ tokio = { workspace = true, features = ["macros", "rt", "time"] }
3940
[dev-dependencies]
4041
once_cell = { workspace = true }
4142
tokio.workspace = true
43+
typespec_derive.workspace = true
4244

4345
[features]
4446
default = [
@@ -49,6 +51,7 @@ default = [
4951
"reqwest_rustls",
5052
"tokio_sleep",
5153
]
54+
derive = ["dep:typespec_derive"]
5255
http = ["dep:http-types", "typespec/http"]
5356
json = ["typespec/json"]
5457
reqwest = ["dep:reqwest", "reqwest/default-tls"]

sdk/typespec/typespec_client_core/src/http/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ pub use policies::*;
2525
pub use request::*;
2626
pub use response::*;
2727

28+
// Re-export derive macro for Model.
29+
#[cfg(feature = "derive")]
30+
pub use typespec_derive::Model;
31+
2832
// Re-export important types.
2933
pub use http_types::{Method, StatusCode};
3034
pub use url::Url;

sdk/typespec/typespec_client_core/src/http/pipeline.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ mod tests {
9595
use super::*;
9696
use crate::{
9797
http::{headers::Headers, Method, PolicyResult, StatusCode, TransportOptions},
98-
json_model,
9998
stream::BytesStream,
10099
};
101100
use bytes::Bytes;
102101
use serde::Deserialize;
102+
use typespec_derive::Model;
103103

104104
#[tokio::test]
105105
async fn deserializes_response() {
@@ -122,12 +122,12 @@ mod tests {
122122
}
123123
}
124124

125-
#[derive(Debug, Deserialize)]
125+
#[derive(Model, Debug, Deserialize)]
126+
#[typespec(crate = "crate")]
126127
struct Model {
127128
foo: i32,
128129
bar: String,
129130
}
130-
json_model!(Model);
131131

132132
let options =
133133
ClientOptions::new(TransportOptions::new_custom_policy(Arc::new(Responder {})));

0 commit comments

Comments
 (0)