-
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.
- Loading branch information
Zachary Dziura
committed
Mar 16, 2018
1 parent
01e8097
commit 26aecf2
Showing
6 changed files
with
397 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::error::Error; | ||
use std::fmt::{self, Display, Formatter}; | ||
use std::convert::From; | ||
use std::fs::File; | ||
use std::io::{self, BufReader, ErrorKind}; | ||
|
||
use serde_yaml; | ||
|
||
use currency::Currency; | ||
|
||
pub fn load_config(filename: &str) -> Result<Vec<Currency>, ConfigError> { | ||
let config_file = File::open(filename)?; | ||
let mut configs: Vec<Currency> = serde_yaml::from_reader(BufReader::new(config_file))?; | ||
configs.sort_by(|a, b| b.cmp(a)); | ||
|
||
Ok(configs) | ||
} | ||
|
||
pub fn default_config() -> Vec<Currency> { | ||
vec![ | ||
Currency::new("platinum", 1000000, "p", None), | ||
Currency::new("gold", 10000, "g", None), | ||
Currency::new("silver", 100, "s", None), | ||
Currency::new("copper", 1, "c", None), | ||
] | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct ConfigError { | ||
desc: String, | ||
pub kind: ErrorKind, | ||
} | ||
|
||
impl Display for ConfigError { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
write!(f, "Sterling Error: {}", self.desc) | ||
} | ||
} | ||
|
||
impl Error for ConfigError { | ||
fn description(&self) -> &str { | ||
&self.desc | ||
} | ||
} | ||
|
||
impl From<io::Error> for ConfigError { | ||
fn from(error: io::Error) -> Self { | ||
ConfigError { | ||
desc: error.description().to_owned(), | ||
kind: error.kind(), | ||
} | ||
} | ||
} | ||
|
||
impl From<serde_yaml::Error> for ConfigError { | ||
fn from(error: serde_yaml::Error) -> Self { | ||
ConfigError { | ||
desc: error.description().to_owned(), | ||
kind: ErrorKind::Other, | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::cmp::Ordering; | ||
|
||
#[derive(Clone, Debug, Eq, Serialize, Deserialize)] | ||
pub struct Currency { | ||
pub name: String, | ||
pub rate: usize, | ||
pub value: Option<usize>, | ||
pub alias: String, | ||
pub plural: Option<String>, | ||
} | ||
|
||
impl Currency { | ||
pub fn new(name: &str, rate: usize, alias: &str, plural: Option<String>) -> Currency { | ||
Currency { | ||
name: name.to_owned(), | ||
rate, | ||
value: None, | ||
alias: alias.to_owned(), | ||
plural, | ||
} | ||
} | ||
|
||
pub fn with_value(&mut self, value: usize) -> Currency { | ||
Currency { | ||
name: self.name.clone(), | ||
rate: self.rate, | ||
value: Some(value), | ||
alias: self.alias.clone(), | ||
plural: self.plural.clone(), | ||
} | ||
} | ||
|
||
pub fn alias_display(&self) -> String { | ||
self.value.unwrap_or(0).to_string() + &self.alias | ||
} | ||
|
||
pub fn full_display(&self) -> String { | ||
let mut display = self.value.unwrap_or(0).to_string() + " "; | ||
|
||
if self.value.unwrap_or(0) > 1 { | ||
match &self.plural { | ||
&Some(ref plural) => display = display + &plural, | ||
&None => display = display + &self.name, | ||
} | ||
} else { | ||
display = display + &self.name; | ||
} | ||
|
||
display | ||
} | ||
} | ||
|
||
// impl Display for Currency { | ||
// fn | ||
// } | ||
|
||
impl Ord for Currency { | ||
fn cmp(&self, other: &Currency) -> Ordering { | ||
self.value.cmp(&other.value) | ||
} | ||
} | ||
|
||
impl PartialOrd for Currency { | ||
fn partial_cmp(&self, other: &Currency) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl PartialEq for Currency { | ||
fn eq(&self, other: &Currency) -> bool { | ||
self.value == other.value | ||
} | ||
} |
Oops, something went wrong.