mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 11:25:41 +02:00
Refactor: finish refactor on commands which take optional cell paths. (#6961)
* refactor on conversions module * finish refactor on strings command * simplify code * rename from ArgumentsCp to CellPathOnlyArgs * fmt code * refactor on hash relative commands
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
use crate::input_handler::{operate, CmdArgument};
|
||||
use nu_engine::CallExt;
|
||||
use nu_protocol::ast::{Call, CellPath};
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::Span;
|
||||
use nu_protocol::{Example, PipelineData, ShellError, Signature, SyntaxShape, Value};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
@ -26,6 +28,17 @@ impl<D: HashDigest> Default for GenericDigest<D> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct Arguments {
|
||||
pub(super) cell_paths: Option<Vec<CellPath>>,
|
||||
pub(super) binary: bool,
|
||||
}
|
||||
|
||||
impl CmdArgument for Arguments {
|
||||
fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
|
||||
self.cell_paths.take()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> Command for GenericDigest<D>
|
||||
where
|
||||
D: HashDigest + Send + Sync + 'static,
|
||||
@ -66,31 +79,19 @@ where
|
||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||
let binary = call.has_flag("binary");
|
||||
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
||||
|
||||
input.map(
|
||||
move |v| {
|
||||
if cell_paths.is_empty() {
|
||||
action::<D>(binary, &v)
|
||||
} else {
|
||||
let mut v = v;
|
||||
for path in &cell_paths {
|
||||
let ret = v.update_cell_path(
|
||||
&path.members,
|
||||
Box::new(move |old| action::<D>(binary, old)),
|
||||
);
|
||||
if let Err(error) = ret {
|
||||
return Value::Error { error };
|
||||
}
|
||||
}
|
||||
v
|
||||
}
|
||||
},
|
||||
let cell_paths = (!cell_paths.is_empty()).then(|| cell_paths);
|
||||
let args = Arguments { binary, cell_paths };
|
||||
operate(
|
||||
action::<D>,
|
||||
args,
|
||||
input,
|
||||
call.head,
|
||||
engine_state.ctrlc.clone(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn action<D>(binary: bool, input: &Value) -> Value
|
||||
pub(super) fn action<D>(input: &Value, args: &Arguments, _span: Span) -> Value
|
||||
where
|
||||
D: HashDigest,
|
||||
digest::Output<D>: core::fmt::LowerHex,
|
||||
@ -119,7 +120,7 @@ where
|
||||
|
||||
let digest = D::digest(bytes);
|
||||
|
||||
if binary {
|
||||
if args.binary {
|
||||
Value::Binary {
|
||||
val: digest.to_vec(),
|
||||
span,
|
||||
|
@ -42,7 +42,7 @@ impl HashDigest for Md5 {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::hash::generic_digest;
|
||||
use crate::hash::generic_digest::{self, Arguments};
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
@ -59,7 +59,14 @@ mod tests {
|
||||
val: "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let actual = generic_digest::action::<Md5>(false, &binary);
|
||||
let actual = generic_digest::action::<Md5>(
|
||||
&binary,
|
||||
&Arguments {
|
||||
cell_paths: None,
|
||||
binary: false,
|
||||
},
|
||||
Span::test_data(),
|
||||
);
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
@ -73,7 +80,14 @@ mod tests {
|
||||
val: "5f80e231382769b0102b1164cf722d83".to_owned(),
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let actual = generic_digest::action::<Md5>(false, &binary);
|
||||
let actual = generic_digest::action::<Md5>(
|
||||
&binary,
|
||||
&Arguments {
|
||||
cell_paths: None,
|
||||
binary: false,
|
||||
},
|
||||
Span::test_data(),
|
||||
);
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ impl HashDigest for Sha256 {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::hash::generic_digest;
|
||||
use crate::hash::generic_digest::{self, Arguments};
|
||||
|
||||
#[test]
|
||||
fn test_examples() {
|
||||
@ -61,7 +61,14 @@ mod tests {
|
||||
val: "71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73".to_owned(),
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let actual = generic_digest::action::<Sha256>(false, &binary);
|
||||
let actual = generic_digest::action::<Sha256>(
|
||||
&binary,
|
||||
&Arguments {
|
||||
cell_paths: None,
|
||||
binary: false,
|
||||
},
|
||||
Span::test_data(),
|
||||
);
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
|
||||
@ -75,7 +82,14 @@ mod tests {
|
||||
val: "c47a10dc272b1221f0380a2ae0f7d7fa830b3e378f2f5309bbf13f61ad211913".to_owned(),
|
||||
span: Span::test_data(),
|
||||
};
|
||||
let actual = generic_digest::action::<Sha256>(false, &binary);
|
||||
let actual = generic_digest::action::<Sha256>(
|
||||
&binary,
|
||||
&Arguments {
|
||||
cell_paths: None,
|
||||
binary: false,
|
||||
},
|
||||
Span::test_data(),
|
||||
);
|
||||
assert_eq!(actual, expected);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user