1use anyhow::{anyhow, Result};
2use keyring::Entry;
3
4pub struct Keychain {
5 service: String,
6 account: String,
7}
8
9impl Keychain {
10 pub fn new(service: &str, account: &str) -> Self {
11 Self { service: service.into(), account: account.into() }
12 }
13
14 fn entry(&self) -> Result<Entry> {
15 Entry::new(&self.service, &self.account).map_err(|e| anyhow!("keyring error: {e}"))
16 }
17
18 pub fn set_password(&self, secret: &str) -> Result<()> {
19 self.entry()?.set_password(secret).map_err(|e| anyhow!("keyring error: {e}"))
20 }
21
22 pub fn get_password(&self) -> Result<String> {
23 self.entry()?.get_password().map_err(|e| anyhow!("keyring error: {e}"))
24 }
25
26 pub fn delete_password(&self) -> Result<()> {
27 self.entry()?.delete_credential().map_err(|e| anyhow!("keyring error: {e}"))
28 }
29}