-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Still work to do, but incrementally improve error-handling to reduce usage of unwrap and expect. Also, consolidate file parsing around a Parsable trait. Signed-off-by: Bruce D'Arcus <[email protected]>
- Loading branch information
Showing
13 changed files
with
89 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,7 @@ | ||
use crate::HasFile; | ||
use std::collections::HashMap; | ||
use std::fs; | ||
|
||
pub mod reference; | ||
pub use reference::InputReference; | ||
|
||
/// A bibliography is a collection of references. | ||
pub type InputBibliography = HashMap<String, InputReference>; | ||
|
||
impl HasFile for InputBibliography { | ||
/// Load and parse a YAML or JSON bibliography file. | ||
fn from_file(bib_path: &str) -> InputBibliography { | ||
let contents = | ||
fs::read_to_string(bib_path).expect("Failed to read bibliography file"); | ||
if bib_path.ends_with(".json") { | ||
serde_json::from_str(&contents).expect("Failed to parse JSON") | ||
} else if bib_path.ends_with(".yaml") || bib_path.ends_with(".yml") { | ||
serde_yaml::from_str(&contents).expect("Failed to parse YAML") | ||
} else { | ||
panic!("Style file must be in YAML or JSON format") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,41 @@ | ||
pub mod style; | ||
use std::path::Path; | ||
|
||
use serde::de::DeserializeOwned; | ||
pub use style::Style; | ||
|
||
use std::fs; | ||
|
||
pub mod bibliography; | ||
pub use bibliography::InputBibliography; | ||
use style::locale::Locale; | ||
|
||
use anyhow::{Context, Result}; | ||
|
||
pub mod citation; | ||
|
||
pub trait HasFile { | ||
fn from_file(path: &str) -> Self; | ||
pub trait Parsable: DeserializeOwned {} | ||
impl Parsable for Style {} | ||
impl Parsable for Locale {} | ||
impl Parsable for InputBibliography {} | ||
impl Parsable for citation::Citations {} | ||
|
||
pub fn from_file<T: Parsable, P: AsRef<Path>>(path: P) -> Result<T> { | ||
let path = path.as_ref(); | ||
let contents = fs::read_to_string(path) | ||
.with_context(|| format!("Failed to read file: {}", path.display()))?; | ||
|
||
let value = if path.extension().and_then(|s| s.to_str()) == Some("json") { | ||
serde_json::from_str(&contents).with_context(|| { | ||
format!("Failed to parse JSON from file: {}", path.display()) | ||
})? | ||
} else if path.extension().and_then(|s| s.to_str()) == Some("yaml") { | ||
serde_yaml::from_str(&contents).with_context(|| { | ||
format!("Failed to parse YAML from file: {}", path.display()) | ||
})? | ||
} else { | ||
return Err(anyhow::anyhow!("Unsupported file extension")); | ||
}; | ||
|
||
Ok(value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters