Skip to content

Commit

Permalink
V0.4.0 (#5)
Browse files Browse the repository at this point in the history
* Add html module

* Add re module

* Update readme
  • Loading branch information
guoxbin authored Dec 26, 2019
1 parent d8e6ca8 commit 610a1ca
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ ring = "0.16.9"
sha2 = "0.8.0"
sha3 = "0.8.2"
ripemd160 = "0.8.0"
escaper = "0.1.0"
regex = "1.3.1"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
| hash | Convert hex to RIPEMD-160 | $ dtool hash -a ripemd_160 0x616263 <br> 0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc | v0.2.0 |
| s2u | Convert UTF-8 String to <br> unicode | $ dtool s2u 💯 <br> \u1f4af <br> $ dtool s2u 💯 -f html <br> \&#x1f4af; <br> $ dtool s2u 💯 -f html_d <br> \&#128175; <br> $ dtool s2u 💯 -f rust <br> \u{1f4af} | v0.3.0 |
| u2s | Convert unicode to <br> UTF-8 string | $ dtool u2s '\u1f4af' <br> 💯 <br> $ dtool u2s '\&#x1f4af;' <br> 💯 <br> $ dtool u2s '\&#128175;' <br> 💯 <br> $ dtool u2s '\u{1f4af}' <br> 💯 | v0.3.0 |
| he | HTML entity encode | $ dtool he &lt;b&gt; <br> &amp;lt;b&amp;gt; | v0.4.0 |
| hd | HTML entity decode | $ dtool hd &amp;lt;b&amp;gt; <br> &lt;b&gt; | v0.4.0 |
| re | Regex match | $ dtool re -p 'a(.)c' abcadc <br> abc <br> &nbsp;&nbsp;&nbsp;&nbsp; group#1: b <br> adc <br> &nbsp;&nbsp;&nbsp;&nbsp; group#1: d | v0.4.0 |

## Tips

Expand Down
4 changes: 4 additions & 0 deletions src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod url;
mod number_codec;
mod hash;
mod unicode;
mod html;
mod re;

pub struct Command<'a, 'b> {
pub app: App<'a, 'b>,
Expand All @@ -35,6 +37,8 @@ impl<'a, 'b> ModuleManager<'a, 'b> {
mm.register(number_codec::commands());
mm.register(hash::commands());
mm.register(unicode::commands());
mm.register(html::commands());
mm.register(re::commands());
mm
}

Expand Down
2 changes: 1 addition & 1 deletion src/modules/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::{BufRead, Read};
pub fn input_string(matches: &ArgMatches) -> Result<String, String> {
match matches.value_of("INPUT") {
Some(input) => Ok(input.to_string()),
None => io::stdin().lock().lines().collect::<Result<Vec<String>, io::Error>>().map(|x|x.join("")).map_err(|_| "Invalid input".to_string()),
None => io::stdin().lock().lines().collect::<Result<Vec<String>, io::Error>>().map(|x|x.join("\n")).map_err(|_| "Invalid input".to_string()),
}
}

Expand Down
64 changes: 64 additions & 0 deletions src/modules/html.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use clap::{SubCommand, Arg, ArgMatches};
use crate::modules::{Command, base};
use escaper;

pub fn commands<'a, 'b>() -> Vec<Command<'a, 'b>> {
vec![
Command {
app: SubCommand::with_name("he").about("HTML entity encode").arg(
Arg::with_name("INPUT")
.required(false)
.index(1)),
f: he,
},
Command {
app: SubCommand::with_name("hd").about("HTML entity decode").arg(
Arg::with_name("INPUT")
.required(false)
.index(1)),
f: hd,
}
]
}

fn he(matches: &ArgMatches) -> Result<Vec<String>, String> {

let input = base::input_string(matches)?;

let result = escaper::encode_minimal(&input);

Ok(vec![result])
}

fn hd(matches: &ArgMatches) -> Result<Vec<String>, String> {

let input = base::input_string(matches)?;

let result = escaper::decode_html(&input).map_err(|_| "Decode failed")?;

Ok(vec![result])
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn test_he() {
let app = &commands()[0].app;

let matches = app.clone().get_matches_from(vec!["he", "<br>"]);
assert_eq!(he(&matches) , Ok(vec!["&lt;br&gt;".to_string()]));
}

#[test]
fn test_hd() {
let app = &commands()[1].app;

let matches = app.clone().get_matches_from(vec!["hd", "&lt;br&gt;"]);
assert_eq!(hd(&matches) , Ok(vec!["<br>".to_string()]));

}

}
56 changes: 56 additions & 0 deletions src/modules/re.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use clap::{SubCommand, Arg, ArgMatches};
use crate::modules::{Command, base};
use regex::Regex;

pub fn commands<'a, 'b>() -> Vec<Command<'a, 'b>> {
vec![
Command {
app: SubCommand::with_name("re").about("Regex match")
.arg(
Arg::with_name("p")
.short("p").help("Pattern")
.takes_value(true)
.required(true))
.arg(
Arg::with_name("INPUT")
.required(false)
.index(1)),
f: re,
}
]
}

fn re(matches: &ArgMatches) -> Result<Vec<String>, String> {
let input = base::input_string(matches)?;

let pattern = matches.value_of("p").ok_or("Invalid pattern")?;

let pattern = Regex::new(pattern).map_err(|_| "Invalid pattern")?;

let mut result = vec![];

for (_i, c) in pattern.captures_iter(&input).enumerate() {
for (j, x) in c.iter().enumerate() {
if j == 0 {
result.push(format!("{}", x.unwrap().as_str()));
} else {
result.push(format!(" group#{}: {}", j, x.unwrap().as_str()));
}
}
}

Ok(result)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_re() {
let app = &commands()[0].app;

let matches = app.clone().get_matches_from(vec!["re", "-p", "a(.)c", "abc\nadc"]);
assert_eq!(re(&matches), Ok(vec!["abc".to_string(), " group#1: b".to_string(), "adc".to_string(), " group#1: d".to_string()]));
}
}

0 comments on commit 610a1ca

Please sign in to comment.