use crate::backend::Inner;
use crate::grants::GrantStore;
use std::sync::Arc;
pub(crate) struct Session {
backend: Arc<Inner>,
pub(crate) grants: Option<Arc<GrantStore>>,
}
impl Session {
pub(crate) fn new(backend: Arc<Inner>, grants: Option<Arc<GrantStore>>) -> Arc<Self> {
Arc::new(Self { backend, grants })
}
pub(crate) fn persist_grants(&self) {
let Some(store) = &self.grants else {
return;
};
let mut devices = std::collections::BTreeMap::new();
for id in self.granted_devices() {
devices.insert(
id.clone(),
crate::grants::Stored {
name: self.device_name(&id),
grant: self.grant(&id),
},
);
}
let _ = store.save(&devices);
}
pub(crate) fn backend(&self) -> &Arc<Inner> {
&self.backend
}
}
impl std::ops::Deref for Session {
type Target = Inner;
fn deref(&self) -> &Inner {
&self.backend
}
}