mirror of
https://github.com/chatmail/core.git
synced 2026-05-01 20:36:31 +03:00
accounts: remove unnecessary Arc<RwLock<_>> from Config.inner
This commit is contained in:
@@ -102,7 +102,7 @@ impl Accounts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Select the given account.
|
/// Select the given account.
|
||||||
pub async fn select_account(&self, id: u32) -> Result<()> {
|
pub async fn select_account(&mut self, id: u32) -> Result<()> {
|
||||||
self.config.select_account(id).await?;
|
self.config.select_account(id).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -339,12 +339,15 @@ pub const CONFIG_NAME: &str = "accounts.toml";
|
|||||||
pub const DB_NAME: &str = "dc.db";
|
pub const DB_NAME: &str = "dc.db";
|
||||||
|
|
||||||
/// Account manager configuration file.
|
/// Account manager configuration file.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
file: PathBuf,
|
file: PathBuf,
|
||||||
inner: Arc<RwLock<InnerConfig>>,
|
inner: InnerConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Account manager configuration file contents.
|
||||||
|
///
|
||||||
|
/// This is serialized into TOML.
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
||||||
struct InnerConfig {
|
struct InnerConfig {
|
||||||
pub os_name: String,
|
pub os_name: String,
|
||||||
@@ -356,14 +359,15 @@ struct InnerConfig {
|
|||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub async fn new(os_name: String, dir: &PathBuf) -> Result<Self> {
|
pub async fn new(os_name: String, dir: &PathBuf) -> Result<Self> {
|
||||||
let cfg = Config {
|
let inner = InnerConfig {
|
||||||
file: dir.join(CONFIG_NAME),
|
|
||||||
inner: Arc::new(RwLock::new(InnerConfig {
|
|
||||||
os_name,
|
os_name,
|
||||||
accounts: Vec::new(),
|
accounts: Vec::new(),
|
||||||
selected_account: 0,
|
selected_account: 0,
|
||||||
next_id: 1,
|
next_id: 1,
|
||||||
})),
|
};
|
||||||
|
let cfg = Config {
|
||||||
|
file: dir.join(CONFIG_NAME),
|
||||||
|
inner,
|
||||||
};
|
};
|
||||||
|
|
||||||
cfg.sync().await?;
|
cfg.sync().await?;
|
||||||
@@ -372,15 +376,12 @@ impl Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn os_name(&self) -> String {
|
pub async fn os_name(&self) -> String {
|
||||||
self.inner.read().await.os_name.clone()
|
self.inner.os_name.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sync the inmemory representation to disk.
|
/// Sync the inmemory representation to disk.
|
||||||
async fn sync(&self) -> Result<()> {
|
async fn sync(&self) -> Result<()> {
|
||||||
fs::write(
|
fs::write(&self.file, toml::to_string_pretty(&self.inner)?)
|
||||||
&self.file,
|
|
||||||
toml::to_string_pretty(&*self.inner.read().await)?,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
.context("failed to write config")
|
.context("failed to write config")
|
||||||
}
|
}
|
||||||
@@ -390,18 +391,14 @@ impl Config {
|
|||||||
let bytes = fs::read(&file).await.context("failed to read file")?;
|
let bytes = fs::read(&file).await.context("failed to read file")?;
|
||||||
let inner: InnerConfig = toml::from_slice(&bytes).context("failed to parse config")?;
|
let inner: InnerConfig = toml::from_slice(&bytes).context("failed to parse config")?;
|
||||||
|
|
||||||
Ok(Config {
|
Ok(Config { file, inner })
|
||||||
file,
|
|
||||||
inner: Arc::new(RwLock::new(inner)),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn load_accounts(&self) -> Result<BTreeMap<u32, Context>> {
|
pub async fn load_accounts(&self) -> Result<BTreeMap<u32, Context>> {
|
||||||
let cfg = &*self.inner.read().await;
|
|
||||||
let mut accounts = BTreeMap::new();
|
let mut accounts = BTreeMap::new();
|
||||||
for account_config in &cfg.accounts {
|
for account_config in &self.inner.accounts {
|
||||||
let ctx = Context::new(
|
let ctx = Context::new(
|
||||||
cfg.os_name.clone(),
|
self.inner.os_name.clone(),
|
||||||
account_config.dbfile().into(),
|
account_config.dbfile().into(),
|
||||||
account_config.id,
|
account_config.id,
|
||||||
)
|
)
|
||||||
@@ -413,19 +410,18 @@ impl Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new account in the given root directory.
|
/// Create a new account in the given root directory.
|
||||||
async fn new_account(&self, dir: &PathBuf) -> Result<AccountConfig> {
|
async fn new_account(&mut self, dir: &PathBuf) -> Result<AccountConfig> {
|
||||||
let id = {
|
let id = {
|
||||||
let inner = &mut self.inner.write().await;
|
let id = self.inner.next_id;
|
||||||
let id = inner.next_id;
|
|
||||||
let uuid = Uuid::new_v4();
|
let uuid = Uuid::new_v4();
|
||||||
let target_dir = dir.join(uuid.to_simple_ref().to_string());
|
let target_dir = dir.join(uuid.to_simple_ref().to_string());
|
||||||
|
|
||||||
inner.accounts.push(AccountConfig {
|
self.inner.accounts.push(AccountConfig {
|
||||||
id,
|
id,
|
||||||
dir: target_dir.into(),
|
dir: target_dir.into(),
|
||||||
uuid,
|
uuid,
|
||||||
});
|
});
|
||||||
inner.next_id += 1;
|
self.inner.next_id += 1;
|
||||||
id
|
id
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -437,16 +433,16 @@ impl Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Removes an existing acccount entirely.
|
/// Removes an existing acccount entirely.
|
||||||
pub async fn remove_account(&self, id: u32) -> Result<()> {
|
pub async fn remove_account(&mut self, id: u32) -> Result<()> {
|
||||||
{
|
{
|
||||||
let inner = &mut *self.inner.write().await;
|
if let Some(idx) = self.inner.accounts.iter().position(|e| e.id == id) {
|
||||||
if let Some(idx) = inner.accounts.iter().position(|e| e.id == id) {
|
|
||||||
// remove account from the configs
|
// remove account from the configs
|
||||||
inner.accounts.remove(idx);
|
self.inner.accounts.remove(idx);
|
||||||
}
|
}
|
||||||
if inner.selected_account == id {
|
if self.inner.selected_account == id {
|
||||||
// reset selected account
|
// reset selected account
|
||||||
inner.selected_account = inner.accounts.get(0).map(|e| e.id).unwrap_or_default();
|
self.inner.selected_account =
|
||||||
|
self.inner.accounts.get(0).map(|e| e.id).unwrap_or_default();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -454,29 +450,22 @@ impl Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn get_account(&self, id: u32) -> Option<AccountConfig> {
|
async fn get_account(&self, id: u32) -> Option<AccountConfig> {
|
||||||
self.inner
|
self.inner.accounts.iter().find(|e| e.id == id).cloned()
|
||||||
.read()
|
|
||||||
.await
|
|
||||||
.accounts
|
|
||||||
.iter()
|
|
||||||
.find(|e| e.id == id)
|
|
||||||
.cloned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_selected_account(&self) -> u32 {
|
pub async fn get_selected_account(&self) -> u32 {
|
||||||
self.inner.read().await.selected_account
|
self.inner.selected_account
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn select_account(&self, id: u32) -> Result<()> {
|
pub async fn select_account(&mut self, id: u32) -> Result<()> {
|
||||||
{
|
{
|
||||||
let inner = &mut *self.inner.write().await;
|
|
||||||
ensure!(
|
ensure!(
|
||||||
inner.accounts.iter().any(|e| e.id == id),
|
self.inner.accounts.iter().any(|e| e.id == id),
|
||||||
"invalid account id: {}",
|
"invalid account id: {}",
|
||||||
id
|
id
|
||||||
);
|
);
|
||||||
|
|
||||||
inner.selected_account = id;
|
self.inner.selected_account = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.sync().await?;
|
self.sync().await?;
|
||||||
@@ -519,10 +508,7 @@ mod tests {
|
|||||||
assert_eq!(accounts1.config.get_selected_account().await, 1);
|
assert_eq!(accounts1.config.get_selected_account().await, 1);
|
||||||
|
|
||||||
assert_eq!(accounts1.dir, accounts2.dir);
|
assert_eq!(accounts1.dir, accounts2.dir);
|
||||||
assert_eq!(
|
assert_eq!(accounts1.config, accounts2.config,);
|
||||||
&*accounts1.config.inner.read().await,
|
|
||||||
&*accounts2.config.inner.read().await,
|
|
||||||
);
|
|
||||||
assert_eq!(accounts1.accounts.len(), accounts2.accounts.len());
|
assert_eq!(accounts1.accounts.len(), accounts2.accounts.len());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user