Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Surface errors from reading toml
Browse files Browse the repository at this point in the history
  • Loading branch information
KillTheMule committed Sep 14, 2023
1 parent bf52a44 commit dd2dc35
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/server/formatting.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::path::PathBuf;

use anyhow::Context;
use tokio::fs::File;
use tokio::io::AsyncReadExt;

Expand Down Expand Up @@ -29,7 +32,7 @@ pub fn get_formatting_unregistration() -> Unregistration {
impl TypstServer {
pub async fn format_document(&self, source: &Source) -> anyhow::Result<Vec<TextEdit>> {
let original_text = source.text();
let res = typstfmt_lib::format(original_text, self.get_fmt_config().await);
let res = typstfmt_lib::format(original_text, self.get_fmt_config().await?);

Ok(vec![TextEdit {
new_text: res,
Expand All @@ -46,25 +49,28 @@ impl TypstServer {
}])
}

async fn get_fmt_config(&self) -> FmtConfig {
async fn get_fmt_config(&self) -> anyhow::Result<FmtConfig> {
// Ignoring all errors since we're returning the default config in case
// we can't find something more specific
let mut config_file: Option<File> = File::options().read(true).open(CONFIG_PATH).await.ok();
let mut path = PathBuf::from(CONFIG_PATH);
let mut config_file: Option<File> = File::options().read(true).open(&path).await.ok();

if config_file.is_none() {
if let Some(root_path) = &self.config.read().await.root_path {
let mut root_path = root_path.clone();
root_path.push(CONFIG_PATH);
config_file = File::options().read(true).open(root_path).await.ok();
path = root_path.clone();
path.push(CONFIG_PATH);
config_file = File::options().read(true).open(&path).await.ok();
}
}

if let Some(mut f) = config_file {
let mut buf = String::default();
let _ = f.read_to_string(&mut buf).await;
FmtConfig::from_toml(&buf).unwrap_or_default()
// An error here should be surfaced to the user though
FmtConfig::from_toml(&buf)
.map_err(|s| anyhow::anyhow!(s))
} else {
FmtConfig::default()
Ok(FmtConfig::default())
}
}
}

0 comments on commit dd2dc35

Please sign in to comment.