Skip to content

Commit 0ca894f

Browse files
Impl std traits
1 parent 703ee0f commit 0ca894f

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

src/lib.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@
2929
use std::process::{Command, Output};
3030
use std::io::{Result, Error, ErrorKind};
3131
use std::env;
32+
use std::default::Default;
33+
use std::fmt;
34+
use std::str::FromStr;
35+
use std::error;
3236

33-
#[derive(Debug)]
37+
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
3438
/// Browser types available
3539
pub enum Browser {
3640
///Operating system's default browser
@@ -52,6 +56,57 @@ pub enum Browser {
5256
Safari
5357
}
5458

59+
///The Error type for parsing a string into a Browser.
60+
#[derive(Debug, Eq, PartialEq, Copy, Clone, Hash)]
61+
pub struct ParseBrowserError;
62+
63+
impl fmt::Display for ParseBrowserError {
64+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65+
f.write_str("Invalid browser given")
66+
}
67+
}
68+
69+
impl error::Error for ParseBrowserError {
70+
fn description(&self) -> &str {
71+
"invalid browser"
72+
}
73+
}
74+
75+
impl Default for Browser {
76+
fn default() -> Self {
77+
Browser::Default
78+
}
79+
}
80+
81+
impl fmt::Display for Browser {
82+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
83+
match *self {
84+
Browser::Default => f.write_str("Default"),
85+
Browser::Firefox => f.write_str("Firefox"),
86+
Browser::InternetExplorer => f.write_str("Internet Explorer"),
87+
Browser::Chrome => f.write_str("Chrome"),
88+
Browser::Opera => f.write_str("Opera"),
89+
Browser::Safari => f.write_str("Safari"),
90+
}
91+
}
92+
}
93+
94+
impl FromStr for Browser {
95+
type Err = ParseBrowserError;
96+
97+
fn from_str(s: &str) -> ::std::result::Result<Self, Self::Err> {
98+
match s {
99+
"firefox" => Ok(Browser::Firefox),
100+
"default" => Ok(Browser::Default),
101+
"ie" | "internet explorer" | "internetexplorer" => Ok(Browser::InternetExplorer),
102+
"chrome" => Ok(Browser::Chrome),
103+
"opera" => Ok(Browser::Opera),
104+
"safari" => Ok(Browser::Safari),
105+
_ => Err(ParseBrowserError)
106+
}
107+
}
108+
}
109+
55110
/// Opens the URL on the default browser of this platform
56111
///
57112
/// Returns Ok(..) so long as the browser invocation was successful. An Err(..) is returned only if

0 commit comments

Comments
 (0)