core: Add better error handling for reading files.
This commit is contained in:
parent
4b0b82c19a
commit
e7f22164ac
|
@ -11,7 +11,7 @@ pub enum RookeriesError {
|
||||||
CopyFileFailure(io::Error, String, FileType),
|
CopyFileFailure(io::Error, String, FileType),
|
||||||
WriteFileFailure(io::Error, String, FileType),
|
WriteFileFailure(io::Error, String, FileType),
|
||||||
CreateDirectoryFailure(io::Error, FileType),
|
CreateDirectoryFailure(io::Error, FileType),
|
||||||
ReadMarkdownSourceFailure(io::Error, String),
|
ReadFileFailure(io::Error, String, FileType),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for RookeriesError {
|
impl fmt::Display for RookeriesError {
|
||||||
|
@ -42,8 +42,8 @@ impl fmt::Display for RookeriesError {
|
||||||
writeln!(f, "Could not write {} {}.", file_type, filename.red())?;
|
writeln!(f, "Could not write {} {}.", file_type, filename.red())?;
|
||||||
write!(f, "Exact cause: {}", e)
|
write!(f, "Exact cause: {}", e)
|
||||||
}
|
}
|
||||||
RookeriesError::ReadMarkdownSourceFailure(ref e, ref filename) => {
|
RookeriesError::ReadFileFailure(ref e, ref filename, ref file_type) => {
|
||||||
writeln!(f, "Could not read Markdown source {}.", filename.red())?;
|
writeln!(f, "Could not read {} {}.", file_type, filename.red())?;
|
||||||
write!(f, "Exact cause: {}", e)
|
write!(f, "Exact cause: {}", e)
|
||||||
}
|
}
|
||||||
RookeriesError::CreateDirectoryFailure(ref e, ref directory_type) => {
|
RookeriesError::CreateDirectoryFailure(ref e, ref directory_type) => {
|
||||||
|
@ -66,7 +66,7 @@ impl error::Error for RookeriesError {
|
||||||
RookeriesError::MissingSiteToml => None,
|
RookeriesError::MissingSiteToml => None,
|
||||||
RookeriesError::CopyFileFailure(ref e, _, _) => Some(e),
|
RookeriesError::CopyFileFailure(ref e, _, _) => Some(e),
|
||||||
RookeriesError::WriteFileFailure(ref e, _, _) => Some(e),
|
RookeriesError::WriteFileFailure(ref e, _, _) => Some(e),
|
||||||
RookeriesError::ReadMarkdownSourceFailure(ref e, _) => Some(e),
|
RookeriesError::ReadFileFailure(ref e, _, _) => Some(e),
|
||||||
RookeriesError::CreateDirectoryFailure(ref e, _) => Some(e),
|
RookeriesError::CreateDirectoryFailure(ref e, _) => Some(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ pub enum FileType {
|
||||||
Plugin,
|
Plugin,
|
||||||
HtmlTemplate,
|
HtmlTemplate,
|
||||||
SampleMarkdown,
|
SampleMarkdown,
|
||||||
|
SourceMarkdown,
|
||||||
PageAPI,
|
PageAPI,
|
||||||
APIRoot,
|
APIRoot,
|
||||||
ProjectRoot,
|
ProjectRoot,
|
||||||
|
@ -28,6 +29,7 @@ impl fmt::Display for FileType {
|
||||||
FileType::HtmlTemplate => "HTML template",
|
FileType::HtmlTemplate => "HTML template",
|
||||||
FileType::Template => "template",
|
FileType::Template => "template",
|
||||||
FileType::SampleMarkdown => "sample Markdown",
|
FileType::SampleMarkdown => "sample Markdown",
|
||||||
|
FileType::SourceMarkdown => "sample Markdown",
|
||||||
FileType::PageAPI => "API page root",
|
FileType::PageAPI => "API page root",
|
||||||
FileType::APIRoot => "API root",
|
FileType::APIRoot => "API root",
|
||||||
FileType::ProjectRoot => "project root",
|
FileType::ProjectRoot => "project root",
|
||||||
|
|
28
src/main.rs
28
src/main.rs
|
@ -191,8 +191,9 @@ fn build_site(args: &Args) -> Result {
|
||||||
return Err(RookeriesError::MissingSiteToml);
|
return Err(RookeriesError::MissingSiteToml);
|
||||||
}
|
}
|
||||||
|
|
||||||
let project_manifest_details =
|
let project_manifest_details = read_to_string(project_manifest).map_err(|err| {
|
||||||
read_to_string(project_manifest).expect("Unable to read file with toml.");
|
RookeriesError::ReadFileFailure(err, "site.toml".to_string(), FileType::SiteToml)
|
||||||
|
})?;
|
||||||
let project_site: Site =
|
let project_site: Site =
|
||||||
toml::from_str(&project_manifest_details).expect("Unable to read from TOML.");
|
toml::from_str(&project_manifest_details).expect("Unable to read from TOML.");
|
||||||
success_message(&format!(
|
success_message(&format!(
|
||||||
|
@ -241,7 +242,11 @@ fn build_site(args: &Args) -> Result {
|
||||||
.clone()
|
.clone()
|
||||||
.into_string()
|
.into_string()
|
||||||
.unwrap_or(ROOKERIES_UNKNOWN_PLACEHOLDER.to_string());
|
.unwrap_or(ROOKERIES_UNKNOWN_PLACEHOLDER.to_string());
|
||||||
RookeriesError::ReadMarkdownSourceFailure(err, source_page_err)
|
RookeriesError::ReadMarkdownSourceFailure(
|
||||||
|
err,
|
||||||
|
source_page_err,
|
||||||
|
FileType::SourceMarkdown,
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.unwrap_or(ROOKERIES_UNKNOWN_PLACEHOLDER.to_string());
|
.unwrap_or(ROOKERIES_UNKNOWN_PLACEHOLDER.to_string());
|
||||||
|
|
||||||
|
@ -335,10 +340,7 @@ fn build_site(args: &Args) -> Result {
|
||||||
FileType::RenderedHtml,
|
FileType::RenderedHtml,
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
success_message(&format!(
|
success_message(&format!("Created the {} HTML page.", &page.slug.green()));
|
||||||
"Created the {} HTML page.",
|
|
||||||
&page.slug.green()
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the API representations.
|
// Create the API representations.
|
||||||
|
@ -370,7 +372,10 @@ fn build_site(args: &Args) -> Result {
|
||||||
FileType::RenderedSiteJson,
|
FileType::RenderedSiteJson,
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
success_message(&format!("Created the {}.", FileType::RenderedSiteJson.to_string().green()));
|
success_message(&format!(
|
||||||
|
"Created the {}.",
|
||||||
|
FileType::RenderedSiteJson.to_string().green()
|
||||||
|
));
|
||||||
|
|
||||||
// Create all the page API reps.
|
// Create all the page API reps.
|
||||||
let api_pages_path = &api_path.join("pages");
|
let api_pages_path = &api_path.join("pages");
|
||||||
|
@ -379,8 +384,9 @@ fn build_site(args: &Args) -> Result {
|
||||||
let page_json = serde_json::to_string_pretty(&page)
|
let page_json = serde_json::to_string_pretty(&page)
|
||||||
.expect("Unable to create JSON representation of page.");
|
.expect("Unable to create JSON representation of page.");
|
||||||
let page_json_filename = format!("{}.json", &page.slug);
|
let page_json_filename = format!("{}.json", &page.slug);
|
||||||
write(api_pages_path.join(&page_json_filename), page_json)
|
write(api_pages_path.join(&page_json_filename), page_json).map_err(|err| {
|
||||||
.map_err(|err| RookeriesError::WriteFileFailure(err, page_json_filename, FileType::RenderedPageJson))?;
|
RookeriesError::WriteFileFailure(err, page_json_filename, FileType::RenderedPageJson)
|
||||||
|
})?;
|
||||||
success_message(&format!(
|
success_message(&format!(
|
||||||
"Created the {} for the {} page.",
|
"Created the {} for the {} page.",
|
||||||
FileType::RenderedPageJson.to_string().green(),
|
FileType::RenderedPageJson.to_string().green(),
|
||||||
|
@ -460,7 +466,7 @@ fn initialize_site(args: &Args) -> Result {
|
||||||
}
|
}
|
||||||
let project_directory = project_directory
|
let project_directory = project_directory
|
||||||
.canonicalize()
|
.canonicalize()
|
||||||
.expect("Project directory fails.");
|
.expect("Unable to build canonical path to the project directory.");
|
||||||
|
|
||||||
// TODO: Make the sample pages happen.
|
// TODO: Make the sample pages happen.
|
||||||
let mut sample_pages: HashMap<String, Value> = HashMap::new();
|
let mut sample_pages: HashMap<String, Value> = HashMap::new();
|
||||||
|
|
Loading…
Reference in New Issue