Skip to content

Commit 685df2f

Browse files
api: Support [(K, V); N] for AsAttributes as use that
1 parent 247fe3e commit 685df2f

14 files changed

Lines changed: 248 additions & 301 deletions

File tree

cargo-credential/src/main.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::collections::HashMap;
2-
31
use cargo_credential::{Action, CredentialResponse, Error, RegistryInfo, Secret};
42

53
pub struct SecretServiceCredential;
@@ -17,9 +15,9 @@ impl SecretServiceCredential {
1715
.default_collection()
1816
.await
1917
.map_err(|err| Error::Other(Box::new(err)))?;
20-
let attributes = HashMap::from([("url", registry.index_url)]);
18+
let attributes = &[("url", registry.index_url)];
2119
let items = collection
22-
.search_items(&attributes)
20+
.search_items(attributes)
2321
.await
2422
.map_err(|err| Error::Other(Box::new(err)))?;
2523

@@ -57,7 +55,7 @@ impl SecretServiceCredential {
5755
collection
5856
.create_item(
5957
&format!("cargo-registry:{}", registry.index_url),
60-
&attributes,
58+
attributes,
6159
token,
6260
true,
6361
None,

cli/src/main.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,18 @@ impl Commands {
164164
// We get the secret first from the app-id, then if the --keyring is set, we try
165165
// to use the --secret variable.
166166
let (secret, path) = if let Some(app_id) = &args.app_id {
167-
let attributes = HashMap::from([("app_id", app_id)]);
168167
let default_collection = service.default_collection().await?;
169-
let secret =
170-
if let Some(item) = default_collection.search_items(&attributes).await?.first() {
171-
item.secret().await?
172-
} else {
173-
return Err(Error::new(
174-
"The application doesn't have a stored key on the host keyring.",
175-
));
176-
};
168+
let secret = if let Some(item) = default_collection
169+
.search_items(&[("app_id", app_id)])
170+
.await?
171+
.first()
172+
{
173+
item.secret().await?
174+
} else {
175+
return Err(Error::new(
176+
"The application doesn't have a stored key on the host keyring.",
177+
));
178+
};
177179

178180
// That is the path used by libsecret/oo7, how does it work with kwallet for
179181
// example?

client/README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,19 @@ The library provides types that automatically pick a backend based on whether th
2424
### Basic usage
2525

2626
```rust,no_run
27-
use std::collections::HashMap;
28-
2927
async fn run() -> oo7::Result<()> {
3028
let keyring = oo7::Keyring::new().await?;
31-
let attributes = HashMap::from([("attribute", "attribute_value")]);
29+
let attributes = &[("attribute", "attribute_value")];
3230
3331
// Store a secret
3432
keyring
35-
.create_item("Item Label", &attributes, b"secret", true).await?;
33+
.create_item("Item Label", attributes, b"secret", true).await?;
3634
3735
// Find a stored secret
38-
let items = keyring.search_items(&attributes).await?;
36+
let items = keyring.search_items(attributes).await?;
3937
4038
// Delete a stored secret
41-
keyring.delete(&attributes).await?;
39+
keyring.delete(attributes).await?;
4240
4341
// Unlock the collection if the Secret Service is used
4442
keyring.unlock().await?;
@@ -53,7 +51,6 @@ If your application makes heavy usage of the keyring like a password manager. Yo
5351

5452
```rust,ignore
5553
use std::sync::OnceLock;
56-
use std::collections::HashMap;
5754
5855
static KEYRING: OnceLock<oo7::Keyring> = OnceLock::new();
5956

client/examples/basic.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
use std::collections::HashMap;
2-
3-
use oo7::Keyring;
4-
51
#[tokio::main]
62
async fn main() -> oo7::Result<()> {
7-
let keyring = Keyring::new().await?;
8-
let attributes = HashMap::from([("attr", "value")]);
3+
let keyring = oo7::Keyring::new().await?;
4+
let attributes = &[("attr", "value")];
95
keyring
10-
.create_item("Some Label", &attributes, "secret", true)
6+
.create_item("Some Label", attributes, "secret", true)
117
.await?;
128

13-
let items = keyring.search_items(&attributes).await?;
9+
let items = keyring.search_items(attributes).await?;
1410

1511
for item in items {
1612
println!("{}", item.label().await?);

client/examples/basic_2.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::HashMap, sync::OnceLock};
1+
use std::sync::OnceLock;
22

33
use oo7::Keyring;
44

@@ -9,7 +9,7 @@ async fn main() -> oo7::Result<()> {
99
let keyring = Keyring::new().await?;
1010
KEYRING.set(keyring).unwrap();
1111

12-
let attributes = HashMap::from([("attr", "value")]);
12+
let attributes = [("attr", "value")];
1313

1414
KEYRING
1515
.get()

client/examples/dbus_service.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
use std::collections::HashMap;
2-
3-
use oo7::dbus::Service;
4-
51
#[tokio::main]
62
async fn main() -> oo7::Result<()> {
7-
let service = Service::new().await?;
3+
let service = oo7::dbus::Service::new().await?;
84

9-
let attributes = HashMap::from([("type", "token")]);
105
let collection = service.default_collection().await?;
11-
let items = collection.search_items(&attributes).await?;
6+
let items = collection.search_items(&[("type", "token")]).await?;
127
for item in items {
138
println!("{}", item.label().await?);
149
println!("{}", item.is_locked().await?);

client/examples/file_tracing.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! Run with: cargo run --example file_tracing --features "tokio tracing"
88
//! --release
99
10-
use std::{collections::HashMap, time::Instant};
10+
use std::time::Instant;
1111

1212
use oo7::file::Keyring;
1313
use tempfile::tempdir;
@@ -68,7 +68,7 @@ async fn test_keyring_lifecycle() -> oo7::Result<()> {
6868
keyring
6969
.create_item(
7070
"Test Item",
71-
&HashMap::from([("app", "test"), ("user", "alice")]),
71+
&[("app", "test"), ("user", "alice")],
7272
"my-secret-password",
7373
false,
7474
)
@@ -85,9 +85,7 @@ async fn test_keyring_lifecycle() -> oo7::Result<()> {
8585

8686
// Measure search
8787
let start = Instant::now();
88-
let items = keyring
89-
.search_items(&HashMap::from([("app", "test")]))
90-
.await?;
88+
let items = keyring.search_items(&[("app", "test")]).await?;
9189
let search_time = start.elapsed();
9290
info!(
9391
"Single item search: {:?} (found {} items)",
@@ -118,11 +116,11 @@ async fn test_bulk_operations() -> oo7::Result<()> {
118116
keyring
119117
.create_item(
120118
&format!("Item {}", i),
121-
&HashMap::from([
119+
&[
122120
("app", "bulk_test"),
123121
("index", &i.to_string()),
124122
("batch", "individual"),
125-
]),
123+
],
126124
format!("secret-{}", i),
127125
false,
128126
)
@@ -138,9 +136,7 @@ async fn test_bulk_operations() -> oo7::Result<()> {
138136

139137
// Test search performance with more items
140138
let start = Instant::now();
141-
let items = keyring
142-
.search_items(&HashMap::from([("app", "bulk_test")]))
143-
.await?;
139+
let items = keyring.search_items(&[("app", "bulk_test")]).await?;
144140
let search_time = start.elapsed();
145141

146142
info!(
@@ -179,7 +175,7 @@ async fn test_scaling_behavior() -> oo7::Result<()> {
179175
keyring
180176
.create_item(
181177
&format!("Scale Item {}", i),
182-
&HashMap::from([("app", "scaling_test"), ("index", &i.to_string())]),
178+
&[("app", "scaling_test"), ("index", &i.to_string())],
183179
format!("scaling-secret-{}", i),
184180
false,
185181
)
@@ -201,7 +197,7 @@ async fn test_scaling_behavior() -> oo7::Result<()> {
201197
keyring
202198
.create_item(
203199
"New Item",
204-
&HashMap::from([("app", "scaling_test"), ("type", "new")]),
200+
&[("app", "scaling_test"), ("type", "new")],
205201
"new-secret",
206202
false,
207203
)
@@ -211,9 +207,7 @@ async fn test_scaling_behavior() -> oo7::Result<()> {
211207

212208
// Test search performance
213209
let start = Instant::now();
214-
let items = keyring
215-
.search_items(&HashMap::from([("app", "scaling_test")]))
216-
.await?;
210+
let items = keyring.search_items(&[("app", "scaling_test")]).await?;
217211
let search_time = start.elapsed();
218212
info!(
219213
"Search keyring with {} items: {:?} (found {})",
@@ -246,11 +240,11 @@ async fn test_search_performance() -> oo7::Result<()> {
246240
keyring
247241
.create_item(
248242
&format!("{} - {}", app, user),
249-
&HashMap::from([
243+
&[
250244
("app", *app),
251245
("user", *user),
252246
("index", &(i * users.len() + j).to_string()),
253-
]),
247+
],
254248
format!("{}-{}-secret", app, user),
255249
false,
256250
)
@@ -267,7 +261,7 @@ async fn test_search_performance() -> oo7::Result<()> {
267261
// 1. Exact match (should find 1 item)
268262
let start = Instant::now();
269263
let items = keyring
270-
.search_items(&HashMap::from([("app", "browser"), ("user", "alice")]))
264+
.search_items(&[("app", "browser"), ("user", "alice")])
271265
.await?;
272266
let exact_time = start.elapsed();
273267
info!(
@@ -278,9 +272,7 @@ async fn test_search_performance() -> oo7::Result<()> {
278272

279273
// 2. Single attribute match (should find multiple items)
280274
let start = Instant::now();
281-
let items = keyring
282-
.search_items(&HashMap::from([("app", "browser")]))
283-
.await?;
275+
let items = keyring.search_items(&[("app", "browser")]).await?;
284276
let single_attr_time = start.elapsed();
285277
info!(
286278
"Single attribute search: {:?} (found {} items)",
@@ -290,9 +282,7 @@ async fn test_search_performance() -> oo7::Result<()> {
290282

291283
// 3. No match
292284
let start = Instant::now();
293-
let items = keyring
294-
.search_items(&HashMap::from([("app", "nonexistent")]))
295-
.await?;
285+
let items = keyring.search_items(&[("app", "nonexistent")]).await?;
296286
let no_match_time = start.elapsed();
297287
info!(
298288
"No match search: {:?} (found {} items)",

0 commit comments

Comments
 (0)