mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-09 09:49:10 +02:00
Implement Decodable for OsType
This commit is contained in:
parent
34fa7d94b4
commit
a07f7e00dc
3 changed files with 53 additions and 8 deletions
|
|
@ -105,7 +105,7 @@ impl Cache {
|
|||
let platform = match self.os {
|
||||
OsType::Linux => Some("linux"),
|
||||
OsType::OsX => Some("osx"),
|
||||
OsType::SunOS => None, // TODO: Does Rust support SunOS
|
||||
OsType::SunOs => None, // TODO: Does Rust support SunOS?
|
||||
OsType::Other => None,
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ struct Args {
|
|||
flag_version: bool,
|
||||
flag_list: bool,
|
||||
flag_render: Option<String>,
|
||||
flag_os: Option<String>, // TODO enum
|
||||
flag_os: Option<OsType>,
|
||||
flag_update: bool,
|
||||
flag_clear_cache: bool,
|
||||
}
|
||||
|
|
@ -173,11 +173,6 @@ fn main() {
|
|||
process::exit(1);
|
||||
}
|
||||
|
||||
// Override OS and exit
|
||||
if let Some(os) = args.flag_os {
|
||||
println!("Flag --os not yet implemented.");
|
||||
}
|
||||
|
||||
// Show command from cache
|
||||
if let Some(command) = args.arg_command {
|
||||
|
||||
|
|
|
|||
52
src/types.rs
52
src/types.rs
|
|
@ -1,15 +1,38 @@
|
|||
//! Types used in the client.
|
||||
|
||||
use rustc_serialize::{Decodable, Decoder};
|
||||
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
#[allow(dead_code)]
|
||||
pub enum OsType {
|
||||
Linux,
|
||||
OsX,
|
||||
SunOS,
|
||||
SunOs,
|
||||
Other,
|
||||
}
|
||||
|
||||
|
||||
/// Custom Decodable implementation, so that we can parse command line arguments
|
||||
/// directly into an `OsType` instance.
|
||||
impl Decodable for OsType {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
|
||||
return d.read_str().and_then(|input| {
|
||||
let lowercase = input.to_lowercase();
|
||||
match &lowercase[..] {
|
||||
"linux" => Ok(OsType::Linux),
|
||||
"osx" => Ok(OsType::OsX),
|
||||
"sunos" => Ok(OsType::SunOs),
|
||||
"other" => Ok(OsType::Other),
|
||||
_ => Err(d.error(&format!("Invalid OS type: '{}'. Choose one of 'linux', \
|
||||
'osx', 'sunos' or 'other'.", lowercase)))
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum LineType {
|
||||
Empty,
|
||||
|
|
@ -35,3 +58,30 @@ impl<'a> From<&'a str> for LineType {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate rustc_serialize;
|
||||
extern crate docopt;
|
||||
|
||||
use super::OsType::{self, Linux, OsX, SunOs, Other};
|
||||
use rustc_serialize::json;
|
||||
|
||||
#[test]
|
||||
fn test_os_type_decoding_regular() {
|
||||
assert_eq!(json::decode::<OsType>("\"linux\"").unwrap(), Linux);
|
||||
assert_eq!(json::decode::<OsType>("\"osx\"").unwrap(), OsX);
|
||||
assert_eq!(json::decode::<OsType>("\"sunos\"").unwrap(), SunOs);
|
||||
assert_eq!(json::decode::<OsType>("\"other\"").unwrap(), Other);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_os_type_decoding_uppercase() {
|
||||
assert_eq!(json::decode::<OsType>("\"Linux\"").unwrap(), Linux);
|
||||
assert_eq!(json::decode::<OsType>("\"LINUX\"").unwrap(), Linux);
|
||||
}
|
||||
#[test]
|
||||
fn test_os_type_decoding_unknown() {
|
||||
assert!(json::decode::<OsType>("\"lindows\"").is_err());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue