Refactor load into a seperate function
This commit is contained in:
parent
99257a8213
commit
d8a3f16891
1 changed files with 22 additions and 21 deletions
|
@ -27,6 +27,27 @@ impl SettingsFile {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn load_settings(path: &Path, fs: &Arc<dyn Fs>) -> Result<String> {
|
||||||
|
match fs.load(path).await {
|
||||||
|
result @ Ok(_) => result,
|
||||||
|
Err(err) => {
|
||||||
|
if let Some(e) = err.downcast_ref::<std::io::Error>() {
|
||||||
|
if e.kind() == ErrorKind::NotFound {
|
||||||
|
return Ok(std::str::from_utf8(
|
||||||
|
Assets
|
||||||
|
.load("settings/initial_user_settings.json")
|
||||||
|
.unwrap()
|
||||||
|
.as_ref(),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Err(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn update(
|
pub fn update(
|
||||||
cx: &mut MutableAppContext,
|
cx: &mut MutableAppContext,
|
||||||
update: impl 'static + Send + FnOnce(&mut SettingsFileContent),
|
update: impl 'static + Send + FnOnce(&mut SettingsFileContent),
|
||||||
|
@ -40,27 +61,7 @@ impl SettingsFile {
|
||||||
|
|
||||||
cx.background()
|
cx.background()
|
||||||
.spawn(async move {
|
.spawn(async move {
|
||||||
let old_text = match fs.load(path).await {
|
let old_text = SettingsFile::load_settings(path, &fs).await?;
|
||||||
Ok(settings) => settings,
|
|
||||||
Err(err) => {
|
|
||||||
if let Ok(e) = err.downcast::<std::io::Error>() {
|
|
||||||
if e.kind() == ErrorKind::NotFound {
|
|
||||||
std::str::from_utf8(
|
|
||||||
Assets
|
|
||||||
.load("settings/initial_user_settings.json")
|
|
||||||
.unwrap()
|
|
||||||
.as_ref(),
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
.to_string()
|
|
||||||
} else {
|
|
||||||
anyhow::bail!("Failed to load settings");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
anyhow::bail!("Failed to load settings")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let new_text = update_settings_file(old_text, current_file_content, update);
|
let new_text = update_settings_file(old_text, current_file_content, update);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue