mirror of
https://github.com/nushell/nushell.git
synced 2025-05-09 12:34:26 +02:00
Refactored.
This commit is contained in:
parent
d105d77928
commit
174abf68bc
@ -4,12 +4,16 @@ use nu::{
|
|||||||
ReturnSuccess, ReturnValue, ShellError, Tagged, Value,
|
ReturnSuccess, ReturnValue, ShellError, Tagged, Value,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum Action {
|
||||||
|
Downcase,
|
||||||
|
Upcase,
|
||||||
|
ToInteger,
|
||||||
|
}
|
||||||
|
|
||||||
struct Str {
|
struct Str {
|
||||||
field: Option<String>,
|
field: Option<String>,
|
||||||
error: Option<String>,
|
error: Option<String>,
|
||||||
downcase: bool,
|
action: Option<Action>,
|
||||||
upcase: bool,
|
|
||||||
toint: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Str {
|
impl Str {
|
||||||
@ -17,83 +21,49 @@ impl Str {
|
|||||||
Str {
|
Str {
|
||||||
field: None,
|
field: None,
|
||||||
error: None,
|
error: None,
|
||||||
downcase: false,
|
action: None,
|
||||||
upcase: false,
|
|
||||||
toint: false,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn actions(&self) -> Vec<bool> {
|
fn apply(&self, input: &str) -> Value {
|
||||||
vec![self.downcase, self.upcase, self.toint]
|
match self.action {
|
||||||
|
Some(Action::Downcase) => Value::string(input.to_ascii_lowercase()),
|
||||||
|
Some(Action::Upcase) => Value::string(input.to_ascii_uppercase()),
|
||||||
|
Some(Action::ToInteger) => match input.trim().parse::<i64>() {
|
||||||
|
Ok(v) => Value::int(v),
|
||||||
|
Err(_) => Value::string(input),
|
||||||
}
|
}
|
||||||
|
None => Value::string(input.to_string()),
|
||||||
fn actions_desired(&self) -> u8 {
|
|
||||||
self.actions()
|
|
||||||
.iter()
|
|
||||||
.fold(0, |acc, &field| if field { acc + 1 } else { acc })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_valid(&self) -> bool {
|
|
||||||
self.at_most_one() || self.none()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn at_most_one(&self) -> bool {
|
|
||||||
self.actions_desired() == 1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn none(&self) -> bool {
|
|
||||||
self.actions_desired() == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
fn log_error(&mut self, message: &str) {
|
|
||||||
self.error = Some(message.to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn for_input(&mut self, field: String) {
|
fn for_input(&mut self, field: String) {
|
||||||
self.field = Some(field);
|
self.field = Some(field);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn for_to_int(&mut self) {
|
fn update(&mut self) {
|
||||||
self.toint = true;
|
if self.action.is_some() {
|
||||||
|
self.log_error("can only apply one");
|
||||||
if !self.is_valid() {
|
|
||||||
self.log_error("can only apply one")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn log_error(&mut self, message: &str) {
|
||||||
|
self.error = Some(message.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn for_to_int(&mut self) {
|
||||||
|
self.update();
|
||||||
|
self.action = Some(Action::ToInteger);
|
||||||
|
}
|
||||||
|
|
||||||
fn for_downcase(&mut self) {
|
fn for_downcase(&mut self) {
|
||||||
self.downcase = true;
|
self.update();
|
||||||
|
self.action = Some(Action::Downcase);
|
||||||
if !self.is_valid() {
|
|
||||||
self.log_error("can only apply one")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn for_upcase(&mut self) {
|
fn for_upcase(&mut self) {
|
||||||
self.upcase = true;
|
self.update();
|
||||||
|
self.action = Some(Action::Upcase);
|
||||||
if !self.is_valid() {
|
|
||||||
self.log_error("can only apply one")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn apply(&self, input: &str) -> Value {
|
|
||||||
if self.downcase {
|
|
||||||
return Value::string(input.to_ascii_lowercase());
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.upcase {
|
|
||||||
return Value::string(input.to_ascii_uppercase());
|
|
||||||
}
|
|
||||||
|
|
||||||
if self.toint {
|
|
||||||
match input.trim().parse::<i64>() {
|
|
||||||
Ok(v) => return Value::int(v),
|
|
||||||
Err(_) => return Value::string(input),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Value::string(input.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &'static str {
|
fn usage(&self) -> &'static str {
|
||||||
@ -282,8 +252,7 @@ mod tests {
|
|||||||
assert!(plugin
|
assert!(plugin
|
||||||
.begin_filter(CallStub::new().with_long_flag("downcase").create())
|
.begin_filter(CallStub::new().with_long_flag("downcase").create())
|
||||||
.is_ok());
|
.is_ok());
|
||||||
assert!(plugin.is_valid());
|
assert!(plugin.action.is_some());
|
||||||
assert!(plugin.downcase);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -293,8 +262,7 @@ mod tests {
|
|||||||
assert!(plugin
|
assert!(plugin
|
||||||
.begin_filter(CallStub::new().with_long_flag("upcase").create())
|
.begin_filter(CallStub::new().with_long_flag("upcase").create())
|
||||||
.is_ok());
|
.is_ok());
|
||||||
assert!(plugin.is_valid());
|
assert!(plugin.action.is_some());
|
||||||
assert!(plugin.upcase);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -304,25 +272,7 @@ mod tests {
|
|||||||
assert!(plugin
|
assert!(plugin
|
||||||
.begin_filter(CallStub::new().with_long_flag("to-int").create())
|
.begin_filter(CallStub::new().with_long_flag("to-int").create())
|
||||||
.is_ok());
|
.is_ok());
|
||||||
assert!(plugin.is_valid());
|
assert!(plugin.action.is_some());
|
||||||
assert!(plugin.toint);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn str_plugin_accepts_only_one_action() {
|
|
||||||
let mut plugin = Str::new();
|
|
||||||
|
|
||||||
assert!(plugin
|
|
||||||
.begin_filter(
|
|
||||||
CallStub::new()
|
|
||||||
.with_long_flag("upcase")
|
|
||||||
.with_long_flag("downcase")
|
|
||||||
.with_long_flag("to-int")
|
|
||||||
.create(),
|
|
||||||
)
|
|
||||||
.is_err());
|
|
||||||
assert!(!plugin.is_valid());
|
|
||||||
assert_eq!(plugin.error, Some("can only apply one".to_string()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user