-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add html module * Add re module * Update readme
- Loading branch information
Showing
6 changed files
with
130 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" |
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
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 |
---|---|---|
@@ -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!["<br>".to_string()])); | ||
} | ||
|
||
#[test] | ||
fn test_hd() { | ||
let app = &commands()[1].app; | ||
|
||
let matches = app.clone().get_matches_from(vec!["hd", "<br>"]); | ||
assert_eq!(hd(&matches) , Ok(vec!["<br>".to_string()])); | ||
|
||
} | ||
|
||
} |
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 |
---|---|---|
@@ -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()])); | ||
} | ||
} |