io_keyring/
entry.rs

1//! Module dedicated to the keyring entry.
2
3use keyring::{Entry, Error, Result};
4
5/// The keyring entry structure.
6///
7/// Represents an entry inside a keyring. An entry is mostly composed
8/// of a keyring entry name and a keyring service name.
9#[derive(Clone, Debug, Eq, PartialEq)]
10pub struct KeyringEntry {
11    pub name: String,
12    pub service: String,
13}
14
15impl KeyringEntry {
16    /// Creates a new keyring entry from the given name.
17    ///
18    /// The service name is set to the cargo crate name by default.
19    pub fn new(name: impl ToString) -> Self {
20        Self {
21            name: name.to_string(),
22            service: env!("CARGO_CRATE_NAME").to_string(),
23        }
24    }
25
26    /// Changes the keyring service name using the builder pattern.
27    pub fn with_service(mut self, service: impl ToString) -> Self {
28        self.service = service.to_string();
29        self
30    }
31}
32
33impl TryFrom<KeyringEntry> for Entry {
34    type Error = Error;
35
36    fn try_from(entry: KeyringEntry) -> Result<Self> {
37        Entry::new(&entry.service, &entry.name)
38    }
39}