Skip to content

Commit

Permalink
bex: Add store
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed May 27, 2022
1 parent 020ab17 commit 653604c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
36 changes: 8 additions & 28 deletions bex/crates/bex/src/credential_store.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::{
fs,
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::store::Store;

#[derive(Debug, Deserialize, Serialize)]
pub struct Credential {
pub access_token: String,
Expand All @@ -30,32 +29,13 @@ impl CredentialStore {
path: dir.as_ref().join("credential.json"),
}
}
}

pub fn delete(&self) -> anyhow::Result<()> {
let p = self.path.as_path();
if p.exists() {
fs::remove_file(p)?;
}
Ok(())
}

pub fn load(&self) -> anyhow::Result<Option<Credential>> {
let p = self.path.as_path();
if p.exists() {
let s = fs::read_to_string(p)?;
Ok(serde_json::from_str(&s)?)
} else {
Ok(None)
}
}
impl Store for CredentialStore {
type Item = Credential;

pub fn store(&self, credential: &Credential) -> anyhow::Result<()> {
let p = self.path.as_path();
if let Some(dir) = p.parent() {
fs::create_dir_all(dir)?;
}
fs::write(p, serde_json::to_string(credential)?)?;
Ok(())
fn path(&self) -> &Path {
self.path.as_path()
}
}

Expand Down
4 changes: 3 additions & 1 deletion bex/crates/bex/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod biscuit;
mod credential_store;
mod store;

use std::{
env, fs, io,
Expand All @@ -15,6 +16,7 @@ use pocket::{
AuthorizationRequest, RetrieveRequest, RetrieveRequestDetailType, RetrieveRequestState,
};
use serde::{Deserialize, Serialize};
use store::Store;
use xdg::BaseDirectories;

use crate::credential_store::CredentialStore;
Expand Down Expand Up @@ -198,7 +200,7 @@ async fn login(consumer_key: Option<String>) -> anyhow::Result<()> {
}
None => {
let credential = authorize(consumer_key.as_str()).await?;
credential_store.store(&credential)?
credential_store.save(&credential)?
}
}
println!("Logged in");
Expand Down
36 changes: 36 additions & 0 deletions bex/crates/bex/src/store.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::{fs, path::Path};

use serde::{de::DeserializeOwned, Serialize};

pub trait Store {
type Item: DeserializeOwned + Serialize;

fn delete(&self) -> anyhow::Result<()> {
let p = self.path();
if p.exists() {
fs::remove_file(p)?;
}
Ok(())
}

fn load(&self) -> anyhow::Result<Option<Self::Item>> {
let p = self.path();
if p.exists() {
let s = fs::read_to_string(p)?;
Ok(Some(serde_json::from_str(&s)?))
} else {
Ok(None)
}
}

fn path(&self) -> &Path;

fn save(&self, item: &Self::Item) -> anyhow::Result<()> {
let p = self.path();
if let Some(dir) = p.parent() {
fs::create_dir_all(dir)?;
}
fs::write(p, serde_json::to_string(item)?)?;
Ok(())
}
}

0 comments on commit 653604c

Please sign in to comment.