Remove duplicated file read code
-use crate::error::{Error, FileIOAction, FileKind};
+use crate::{
+ error::{Error, FileIOAction, FileKind},
+ file,
+};
use serde::Deserialize;
use std::collections::HashMap;
-use std::fs::File;
-use std::io::Read;
use std::path::{Path, PathBuf};
#[derive(Deserialize, Debug, PartialEq)]
@@ -43,28 +44,12 @@ pub struct DocsPage {
pub fn read_project_config(root: impl AsRef<Path>) -> Result<PackageConfig, Error> {
let config_path = root.as_ref().join("gleam.toml");
+ let toml = file::read(&config_path)?;
- let mut file = File::open(&config_path).map_err(|e| Error::FileIO {
- action: FileIOAction::Open,
- kind: FileKind::File,
- path: config_path.clone(),
- err: Some(e.to_string()),
- })?;
-
- let mut toml = String::new();
- file.read_to_string(&mut toml).map_err(|e| Error::FileIO {
- action: FileIOAction::Read,
- kind: FileKind::File,
- path: config_path.clone(),
- err: Some(e.to_string()),
- })?;
-
- let project_config = toml::from_str(&toml).map_err(|e| Error::FileIO {
+ toml::from_str(&toml).map_err(|e| Error::FileIO {
action: FileIOAction::Parse,
kind: FileKind::File,
path: config_path.clone(),
err: Some(e.to_string()),
- })?;
-
- Ok(project_config)
+ })
}
