Skip to content

Commit

Permalink
Don't use an empty struct when everything's stateless anyway
Browse files Browse the repository at this point in the history
  • Loading branch information
nabijaczleweli committed Sep 15, 2016
1 parent fdc5343 commit a7a4d94
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 37 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ To encode a string, do the following:
```rust
extern crate urlencoding;

use urlencoding::UrlEncoder;
use urlencoding::encode;

fn main() {
let encoder = UrlEncoder::new();

let encoded = encoder.encode("This string will be URL encoded.");
let encoded = encode("This string will be URL encoded.");
// This%20string%20will%20be%20URL%20encoded.
}
```
Expand Down
48 changes: 15 additions & 33 deletions src/lib.rs
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"));
}
}

0 comments on commit a7a4d94

Please sign in to comment.