Skip to content

Commit 966061c

Browse files
authored
More into_future usage (#663)
* Add nightly only into_future feature * CreateCollectionBuilder::into_future * CreateDocument::into_future * Create permission * CreateUser::into_future * DeleteCollection::into_future * DeleteDatabase::into_future * DeleteDocument::into_future * DeletePermission::into_future * Run format * Run clippy * Fix e2e tests * Fix local tests
1 parent 6ed76b0 commit 966061c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+701
-660
lines changed

sdk/data_cosmos/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,4 @@ stop-token = { version = "0.7.0", features = ["tokio"] }
4343
[features]
4444
test_e2e = []
4545
mock_transport_framework = [ "azure_core/mock_transport_framework"]
46+
into_future = []

sdk/data_cosmos/examples/attachments_00.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
use azure_core::Context;
21
use azure_data_cosmos::prelude::*;
32
use serde::{Deserialize, Serialize};
4-
use std::borrow::Cow;
53
use std::error::Error;
64

7-
// Now we create a sample struct. The Cow trick
8-
// allows us to use the same struct for serializing
9-
// (without having to own the items if not needed) and
10-
// for deserializing (where owning is required).
11-
// We do not need to define the "id" field here, it will be
12-
// specified in the Document struct below.
5+
// Now we create a sample struct.
136
#[derive(Serialize, Deserialize, Clone, Debug)]
14-
struct MySampleStruct<'a> {
15-
id: Cow<'a, str>,
16-
a_string: Cow<'a, str>,
7+
struct MySampleStruct {
8+
id: String,
9+
a_string: String,
1710
a_number: u64,
1811
a_timestamp: i64,
1912
}
2013

21-
impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
22-
type Entity = &'a str;
14+
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
15+
type Entity = String;
2316

24-
fn partition_key(&'a self) -> Self::Entity {
25-
self.id.as_ref()
17+
fn partition_key(&self) -> Self::Entity {
18+
self.id.clone().into()
2619
}
2720
}
2821

@@ -50,17 +43,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
5043
let id = format!("unique_id{}", 100);
5144

5245
let doc = MySampleStruct {
53-
id: Cow::Borrowed(&id),
54-
a_string: Cow::Borrowed("Something here"),
46+
id,
47+
a_string: "Something here".into(),
5548
a_number: 100,
5649
a_timestamp: chrono::Utc::now().timestamp(),
5750
};
5851

5952
// let's add an entity.
60-
match client
61-
.create_document(Context::new(), &doc, CreateDocumentOptions::new())
62-
.await
63-
{
53+
match client.create_document(doc.clone()).into_future().await {
6454
Ok(_) => {
6555
println!("document created");
6656
}

sdk/data_cosmos/examples/create_delete_database.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4646
// create collection!
4747
{
4848
let db_client = client.clone().into_database_client(database_name.clone());
49-
5049
let create_collection_response = db_client
51-
.create_collection(
52-
Context::new(),
53-
"panzadoro",
54-
CreateCollectionOptions::new("/id"),
55-
)
50+
.create_collection("panzadoro", "/id")
51+
.into_future()
5652
.await?;
5753

5854
println!(
@@ -74,15 +70,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
7470
println!("res == {:#?}", res);
7571
}
7672

77-
let delete_response = db_collection
78-
.delete_collection(Context::new(), DeleteCollectionOptions::new())
79-
.await?;
73+
let delete_response = db_collection.delete_collection().into_future().await?;
8074
println!("collection deleted: {:#?}", delete_response);
8175
}
8276

8377
let resp = client
8478
.into_database_client(database_name)
85-
.delete_database(Context::new(), DeleteDatabaseOptions::new())
79+
.delete_database()
80+
.into_future()
8681
.await?;
8782
println!("database deleted. resp == {:#?}", resp);
8883

sdk/data_cosmos/examples/database_00.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4949
}"#;
5050
let document: Value = serde_json::from_str(data)?;
5151

52-
let options = CreateDocumentOptions::new()
53-
.is_upsert(true)
54-
.partition_key(&43u32)
55-
.unwrap();
5652
let resp = collection_client
57-
.create_document(Context::new(), &document, options)
53+
.create_document(document)
54+
.is_upsert(true)
55+
.partition_key(&43u32)?
56+
.into_future()
5857
.await?;
5958

6059
println!("resp == {:?}", resp);

sdk/data_cosmos/examples/document_00.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ use serde::{Deserialize, Serialize};
44
// DB.
55
use azure_core::prelude::*;
66
use azure_data_cosmos::prelude::*;
7-
use std::borrow::Cow;
87
use std::error::Error;
98

109
#[derive(Clone, Serialize, Deserialize, Debug)]
11-
struct MySampleStruct<'a> {
12-
id: Cow<'a, str>,
13-
a_string: Cow<'a, str>,
10+
struct MySampleStruct {
11+
id: String,
12+
a_string: String,
1413
a_number: u64,
1514
a_timestamp: i64,
1615
}
1716

18-
impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
19-
type Entity = &'a str;
17+
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
18+
type Entity = String;
2019

21-
fn partition_key(&'a self) -> Self::Entity {
22-
self.id.as_ref()
20+
fn partition_key(&self) -> Self::Entity {
21+
self.id.clone()
2322
}
2423
}
2524

@@ -102,11 +101,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
102101
client
103102
.clone()
104103
.into_database_client(database.id.clone())
105-
.create_collection(
106-
Context::new(),
107-
COLLECTION,
108-
CreateCollectionOptions::new("/id"),
109-
)
104+
.create_collection(COLLECTION, "/id")
105+
.into_future()
110106
.await?
111107
.collection
112108
}
@@ -118,8 +114,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
118114
// data in them. Let's create a Document. The only constraint
119115
// is that we need an id and an arbitrary, Serializable type.
120116
let doc = MySampleStruct {
121-
id: Cow::Owned("unique_id100".to_owned()),
122-
a_string: Cow::Borrowed("Something here"),
117+
id: "unique_id100".into(),
118+
a_string: "Something here".into(),
123119
a_number: 100,
124120
a_timestamp: chrono::Utc::now().timestamp(),
125121
};
@@ -135,7 +131,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
135131
// the document attributes.
136132

137133
let create_document_response = collection_client
138-
.create_document(Context::new(), &doc, CreateDocumentOptions::new())
134+
.create_document(doc.clone())
135+
.into_future()
139136
.await?;
140137
println!(
141138
"create_document_response == {:#?}",
@@ -191,14 +188,16 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
191188
.clone()
192189
.into_database_client(DATABASE.to_owned())
193190
.into_collection_client(COLLECTION.to_owned())
194-
.delete_collection(Context::new(), DeleteCollectionOptions::new())
191+
.delete_collection()
192+
.into_future()
195193
.await?;
196194
println!("collection deleted");
197195

198196
// And then we delete the database.
199197
client
200198
.into_database_client(database.id)
201-
.delete_database(Context::new(), DeleteDatabaseOptions::new())
199+
.delete_database()
200+
.into_future()
202201
.await?;
203202
println!("database deleted");
204203

sdk/data_cosmos/examples/document_entries_00.rs

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,22 @@ use azure_core::prelude::*;
22
use azure_data_cosmos::prelude::*;
33
use futures::stream::StreamExt;
44
use serde::{Deserialize, Serialize};
5-
use std::borrow::Cow;
65
use std::error::Error;
76

8-
// Now we create a sample struct. The Cow trick
9-
// allows us to use the same struct for serializing
10-
// (without having to own the items if not needed) and
11-
// for deserializing (where owning is required).
12-
// We do not need to define the "id" field here, it will be
13-
// specified in the Document struct below.
7+
// Now we create a sample struct.
148
#[derive(Serialize, Deserialize, Clone, Debug)]
15-
struct MySampleStruct<'a> {
16-
id: Cow<'a, str>,
17-
a_string: Cow<'a, str>,
9+
struct MySampleStruct {
10+
id: String,
11+
a_string: String,
1812
a_number: u64,
1913
a_timestamp: i64,
2014
}
2115

22-
impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
23-
type Entity = &'a str;
16+
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
17+
type Entity = String;
2418

25-
fn partition_key(&'a self) -> Self::Entity {
26-
self.id.as_ref()
19+
fn partition_key(&self) -> Self::Entity {
20+
self.id.clone()
2721
}
2822
}
2923

@@ -51,18 +45,14 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
5145
let mut response = None;
5246
for i in 0u64..5 {
5347
let doc = MySampleStruct {
54-
id: Cow::Owned(format!("unique_id{}", i)),
55-
a_string: Cow::Borrowed("Something here"),
48+
id: format!("unique_id{}", i),
49+
a_string: "Something here".into(),
5650
a_number: i,
5751
a_timestamp: chrono::Utc::now().timestamp(),
5852
};
5953

6054
// let's add an entity.
61-
response = Some(
62-
client
63-
.create_document(Context::new(), &doc, CreateDocumentOptions::new())
64-
.await?,
65-
);
55+
response = Some(client.create_document(doc.clone()).into_future().await?);
6656
}
6757

6858
println!("Created 5 documents.");
@@ -183,10 +173,8 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
183173
client
184174
.clone()
185175
.into_document_client(id.clone(), &id)?
186-
.delete_document(
187-
Context::new(),
188-
DeleteDocumentOptions::new().consistency_level(&response),
189-
)
176+
.delete_document()
177+
.into_future()
190178
.await?;
191179
}
192180
println!("Cleaned up");

sdk/data_cosmos/examples/document_entries_01.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
use azure_core::Context;
22
use azure_data_cosmos::prelude::*;
33
use serde::{Deserialize, Serialize};
4-
use std::borrow::Cow;
54
use std::error::Error;
65

76
#[derive(Serialize, Deserialize, Clone, Debug)]
8-
struct MySampleStruct<'a> {
9-
id: Cow<'a, str>,
10-
a_string: Cow<'a, str>,
7+
struct MySampleStruct {
8+
id: String,
9+
a_string: String,
1110
a_number: u64,
1211
a_timestamp: i64,
1312
}
1413

15-
impl<'a> azure_data_cosmos::CosmosEntity<'a> for MySampleStruct<'a> {
16-
type Entity = &'a str;
14+
impl azure_data_cosmos::CosmosEntity for MySampleStruct {
15+
type Entity = String;
1716

18-
fn partition_key(&'a self) -> Self::Entity {
19-
self.id.as_ref()
17+
fn partition_key(&self) -> Self::Entity {
18+
self.id.clone()
2019
}
2120
}
2221

@@ -42,19 +41,17 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
4241
let client = client.into_collection_client(collection_name);
4342

4443
let mut doc = MySampleStruct {
45-
id: Cow::Owned(format!("unique_id{}", 500)),
46-
a_string: Cow::Borrowed("Something here"),
44+
id: format!("unique_id{}", 500),
45+
a_string: "Something here".into(),
4746
a_number: 600,
4847
a_timestamp: chrono::Utc::now().timestamp(),
4948
};
5049

5150
// let's add an entity.
5251
let create_document_response = client
53-
.create_document(
54-
Context::new(),
55-
&doc,
56-
CreateDocumentOptions::new().is_upsert(true),
57-
)
52+
.create_document(doc.clone())
53+
.is_upsert(true)
54+
.into_future()
5855
.await?;
5956

6057
println!(

sdk/data_cosmos/examples/permission_00.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,18 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
5858
get_collection2_response
5959
);
6060

61-
let create_user_response = user_client
62-
.create_user(Context::new(), CreateUserOptions::default())
63-
.await?;
61+
let create_user_response = user_client.create_user().into_future().await?;
6462
println!("create_user_response == {:#?}", create_user_response);
6563

6664
// create the first permission!
6765
let permission_client = user_client.clone().into_permission_client("matrix");
6866
let permission_mode = get_collection_response.collection.read_permission();
6967

7068
let create_permission_response = permission_client
71-
.create_permission(
72-
Context::new(),
73-
CreatePermissionOptions::new()
74-
.consistency_level(&create_user_response)
75-
.expiry_seconds(18000u64),
76-
&permission_mode,
77-
)
69+
.create_permission(permission_mode)
70+
.consistency_level(&create_user_response)
71+
.expiry_seconds(18000u64)
72+
.into_future()
7873
.await
7974
.unwrap();
8075
println!(
@@ -87,11 +82,9 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
8782
let permission_mode = get_collection2_response.collection.all_permission();
8883

8984
let create_permission2_response = permission_client
90-
.create_permission(
91-
Context::new(),
92-
CreatePermissionOptions::new().consistency_level(&create_user_response),
93-
&permission_mode,
94-
)
85+
.create_permission(permission_mode)
86+
.consistency_level(&create_user_response)
87+
.into_future()
9588
.await
9689
.unwrap();
9790

@@ -145,12 +138,11 @@ async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
145138
);
146139

147140
let delete_permission_response = permission_client
148-
.delete_permission(
149-
Context::new(),
150-
DeletePermissionOptions::new().consistency_level(ConsistencyLevel::Session(
151-
replace_permission_response.session_token,
152-
)),
153-
)
141+
.delete_permission()
142+
.consistency_level(ConsistencyLevel::Session(
143+
replace_permission_response.session_token,
144+
))
145+
.into_future()
154146
.await
155147
.unwrap();
156148

0 commit comments

Comments
 (0)