mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-23 14:01:37 +00:00
Move error messages into fmt::Display impl for Project
This commit is contained in:
parent
a5c0cfb12b
commit
844b20e7fc
2 changed files with 27 additions and 32 deletions
|
|
@ -20,29 +20,8 @@ pub fn serve(project_path: &PathBuf, verbose: bool, port: Option<u64>) {
|
|||
v
|
||||
},
|
||||
Err(err) => {
|
||||
match err {
|
||||
ProjectLoadError::InvalidJson(serde_err) => {
|
||||
eprintln!(
|
||||
"Found invalid JSON!\nProject in: {}\nError: {}",
|
||||
project_path.display(),
|
||||
serde_err,
|
||||
);
|
||||
|
||||
process::exit(1);
|
||||
},
|
||||
ProjectLoadError::FailedToOpen | ProjectLoadError::FailedToRead => {
|
||||
eprintln!("Found project file, but failed to read it!");
|
||||
eprintln!("Check the permissions of the project file in folder {}", project_path.display());
|
||||
|
||||
process::exit(1);
|
||||
},
|
||||
_ => {
|
||||
eprintln!("Unable to find project file in folder {}", project_path.display());
|
||||
eprintln!("Use 'rojo init' to create a new project file.");
|
||||
|
||||
process::exit(1);
|
||||
},
|
||||
}
|
||||
eprintln!("{}", err);
|
||||
process::exit(1);
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
|||
use std::fmt;
|
||||
use std::fs::{self, File};
|
||||
use std::io::{Read, Write};
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde_json;
|
||||
|
||||
|
|
@ -10,10 +10,26 @@ pub static PROJECT_FILENAME: &'static str = "rojo.json";
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum ProjectLoadError {
|
||||
DidNotExist,
|
||||
FailedToOpen,
|
||||
FailedToRead,
|
||||
InvalidJson(serde_json::Error),
|
||||
DidNotExist(PathBuf),
|
||||
FailedToOpen(PathBuf),
|
||||
FailedToRead(PathBuf),
|
||||
InvalidJson(PathBuf, serde_json::Error),
|
||||
}
|
||||
|
||||
impl fmt::Display for ProjectLoadError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
&ProjectLoadError::InvalidJson(ref project_path, ref serde_err) => {
|
||||
write!(f, "Found invalid JSON reading project: {}\nError: {}", project_path.display(), serde_err)
|
||||
},
|
||||
&ProjectLoadError::FailedToOpen(ref project_path) | &ProjectLoadError::FailedToRead(ref project_path) => {
|
||||
write!(f, "Found project file, but failed to read it: {}", project_path.display())
|
||||
},
|
||||
&ProjectLoadError::DidNotExist(ref project_path) => {
|
||||
write!(f, "Could not locate a project file at {}", project_path.display())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -116,24 +132,24 @@ impl Project {
|
|||
|
||||
match fs::metadata(&package_path) {
|
||||
Ok(_) => {},
|
||||
Err(_) => return Err(ProjectLoadError::DidNotExist),
|
||||
Err(_) => return Err(ProjectLoadError::DidNotExist(package_path.clone())),
|
||||
}
|
||||
|
||||
let mut file = match File::open(&package_path) {
|
||||
Ok(f) => f,
|
||||
Err(_) => return Err(ProjectLoadError::FailedToOpen),
|
||||
Err(_) => return Err(ProjectLoadError::FailedToOpen(package_path.clone())),
|
||||
};
|
||||
|
||||
let mut contents = String::new();
|
||||
|
||||
match file.read_to_string(&mut contents) {
|
||||
Ok(_) => {},
|
||||
Err(_) => return Err(ProjectLoadError::FailedToRead),
|
||||
Err(_) => return Err(ProjectLoadError::FailedToRead(package_path.clone())),
|
||||
}
|
||||
|
||||
match serde_json::from_str(&contents) {
|
||||
Ok(v) => Ok(v),
|
||||
Err(e) => return Err(ProjectLoadError::InvalidJson(e)),
|
||||
Err(e) => return Err(ProjectLoadError::InvalidJson(package_path.clone(), e)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue