mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-24 15:01:38 +00:00
Beautify project code
This commit is contained in:
parent
8cd7d990e0
commit
204084dcae
1 changed files with 20 additions and 34 deletions
|
|
@ -24,7 +24,8 @@ impl fmt::Display for ProjectLoadError {
|
||||||
&ProjectLoadError::InvalidJson(ref project_path, ref serde_err) => {
|
&ProjectLoadError::InvalidJson(ref project_path, ref serde_err) => {
|
||||||
write!(f, "Found invalid JSON reading project: {}\nError: {}", project_path.display(), 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) => {
|
&ProjectLoadError::FailedToOpen(ref project_path) |
|
||||||
|
&ProjectLoadError::FailedToRead(ref project_path) => {
|
||||||
write!(f, "Found project file, but failed to read it: {}", project_path.display())
|
write!(f, "Found project file, but failed to read it: {}", project_path.display())
|
||||||
},
|
},
|
||||||
&ProjectLoadError::DidNotExist(ref project_path) => {
|
&ProjectLoadError::DidNotExist(ref project_path) => {
|
||||||
|
|
@ -52,7 +53,8 @@ impl fmt::Display for ProjectInitError {
|
||||||
&ProjectInitError::AlreadyExists => {
|
&ProjectInitError::AlreadyExists => {
|
||||||
write!(f, "A project already exists at that location.")
|
write!(f, "A project already exists at that location.")
|
||||||
},
|
},
|
||||||
&ProjectInitError::FailedToCreate | &ProjectInitError::FailedToWrite => {
|
&ProjectInitError::FailedToCreate |
|
||||||
|
&ProjectInitError::FailedToWrite => {
|
||||||
write!(f, "Failed to write to the given location.")
|
write!(f, "Failed to write to the given location.")
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -90,15 +92,11 @@ impl Project {
|
||||||
let package_path = location.join(PROJECT_FILENAME);
|
let package_path = location.join(PROJECT_FILENAME);
|
||||||
|
|
||||||
// We abort if the project file already exists.
|
// We abort if the project file already exists.
|
||||||
match fs::metadata(&package_path) {
|
fs::metadata(&package_path)
|
||||||
Ok(_) => return Err(ProjectInitError::AlreadyExists),
|
.map_err(|_| ProjectInitError::AlreadyExists)?;
|
||||||
Err(_) => {},
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut file = match File::create(&package_path) {
|
let mut file = File::create(&package_path)
|
||||||
Ok(f) => f,
|
.map_err(|_| ProjectInitError::FailedToCreate)?;
|
||||||
Err(_) => return Err(ProjectInitError::FailedToCreate),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Try to give the project a meaningful name.
|
// Try to give the project a meaningful name.
|
||||||
// If we can't, we'll just fall back to a default.
|
// If we can't, we'll just fall back to a default.
|
||||||
|
|
@ -118,10 +116,8 @@ impl Project {
|
||||||
};
|
};
|
||||||
let serialized = serde_json::to_string_pretty(&project).unwrap();
|
let serialized = serde_json::to_string_pretty(&project).unwrap();
|
||||||
|
|
||||||
match file.write(serialized.as_bytes()) {
|
file.write(serialized.as_bytes())
|
||||||
Ok(_) => {},
|
.map_err(|_| ProjectInitError::FailedToWrite)?;
|
||||||
Err(_) => return Err(ProjectInitError::FailedToWrite),
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(project)
|
Ok(project)
|
||||||
}
|
}
|
||||||
|
|
@ -131,37 +127,27 @@ impl Project {
|
||||||
pub fn load<T: AsRef<Path>>(location: T) -> Result<Project, ProjectLoadError> {
|
pub fn load<T: AsRef<Path>>(location: T) -> Result<Project, ProjectLoadError> {
|
||||||
let package_path = location.as_ref().join(Path::new(PROJECT_FILENAME));
|
let package_path = location.as_ref().join(Path::new(PROJECT_FILENAME));
|
||||||
|
|
||||||
match fs::metadata(&package_path) {
|
fs::metadata(&package_path)
|
||||||
Ok(_) => {},
|
.map_err(|_| ProjectLoadError::DidNotExist(package_path.clone()))?;
|
||||||
Err(_) => return Err(ProjectLoadError::DidNotExist(package_path.clone())),
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut file = match File::open(&package_path) {
|
let mut file = File::open(&package_path)
|
||||||
Ok(f) => f,
|
.map_err(|_| ProjectLoadError::FailedToOpen(package_path.clone()))?;
|
||||||
Err(_) => return Err(ProjectLoadError::FailedToOpen(package_path.clone())),
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
|
|
||||||
match file.read_to_string(&mut contents) {
|
file.read_to_string(&mut contents)
|
||||||
Ok(_) => {},
|
.map_err(|_| ProjectLoadError::FailedToRead(package_path.clone()))?;
|
||||||
Err(_) => return Err(ProjectLoadError::FailedToRead(package_path.clone())),
|
|
||||||
}
|
|
||||||
|
|
||||||
match serde_json::from_str(&contents) {
|
serde_json::from_str(&contents)
|
||||||
Ok(v) => Ok(v),
|
.map_err(|e| ProjectLoadError::InvalidJson(package_path.clone(), e))
|
||||||
Err(e) => return Err(ProjectLoadError::InvalidJson(package_path.clone(), e)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Saves the given project file to the given folder with the appropriate name.
|
/// Saves the given project file to the given folder with the appropriate name.
|
||||||
pub fn save<T: AsRef<Path>>(&self, location: T) -> Result<(), ProjectSaveError> {
|
pub fn save<T: AsRef<Path>>(&self, location: T) -> Result<(), ProjectSaveError> {
|
||||||
let package_path = location.as_ref().join(Path::new(PROJECT_FILENAME));
|
let package_path = location.as_ref().join(Path::new(PROJECT_FILENAME));
|
||||||
|
|
||||||
let mut file = match File::create(&package_path) {
|
let mut file = File::create(&package_path)
|
||||||
Ok(f) => f,
|
.map_err(|_| ProjectSaveError::FailedToCreate)?;
|
||||||
Err(_) => return Err(ProjectSaveError::FailedToCreate),
|
|
||||||
};
|
|
||||||
|
|
||||||
let serialized = serde_json::to_string_pretty(self).unwrap();
|
let serialized = serde_json::to_string_pretty(self).unwrap();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue