-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't use an empty struct when everything's stateless anyway
- Loading branch information
1 parent
fdc5343
commit a7a4d94
Showing
2 changed files
with
17 additions
and
37 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,28 @@ | ||
pub struct UrlEncoder {} | ||
|
||
impl UrlEncoder { | ||
pub fn new() -> UrlEncoder { | ||
UrlEncoder {} | ||
} | ||
|
||
pub fn encode(&self, data: &str) -> String { | ||
let mut escaped = String::new(); | ||
for b in data.as_bytes().iter() { | ||
match *b as char { | ||
// Accepted characters | ||
'A'...'Z' | 'a'...'z' | '-' | '_' | '.' | '~' => escaped.push(*b as char), | ||
|
||
// Everything else is percent-encoded | ||
b => escaped.push_str(format!("%{:02X}", b as u32).as_str()), | ||
}; | ||
} | ||
return escaped; | ||
pub fn encode(data: &str) -> String { | ||
let mut escaped = String::new(); | ||
for b in data.as_bytes().iter() { | ||
match *b as char { | ||
// Accepted characters | ||
'A'...'Z' | 'a'...'z' | '-' | '_' | '.' | '~' => escaped.push(*b as char), | ||
|
||
// Everything else is percent-encoded | ||
b => escaped.push_str(format!("%{:02X}", b as u32).as_str()), | ||
}; | ||
} | ||
return escaped; | ||
} | ||
|
||
pub struct UrlDecoder {} | ||
|
||
impl UrlDecoder { | ||
pub fn new() -> UrlDecoder { | ||
UrlDecoder {} | ||
} | ||
|
||
pub fn decode(&self, data: &str) -> String { | ||
unimplemented!(); | ||
} | ||
pub fn decode(data: &str) -> String { | ||
unimplemented!(); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::UrlEncoder; | ||
use super::encode; | ||
|
||
#[test] | ||
fn it_encodes_successfully() { | ||
let expected = "this%20that"; | ||
|
||
let encoder = UrlEncoder::new(); | ||
assert_eq!(expected, encoder.encode("this that")); | ||
assert_eq!(expected, encode("this that")); | ||
} | ||
} |