JT 6cdfee3573
Move Value to helpers, separate span call (#10121)
# Description

As part of the refactor to split spans off of Value, this moves to using
helper functions to create values, and using `.span()` instead of
matching span out of Value directly.

Hoping to get a few more helping hands to finish this, as there are a
lot of commands to update :)

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
Co-authored-by: WindSoilder <windsoilder@outlook.com>
2023-09-03 07:27:29 -07:00

88 lines
2.5 KiB
Rust

use super::generic_digest::{GenericDigest, HashDigest};
use ::md5::Md5;
use nu_protocol::{Example, Span, Value};
pub type HashMd5 = GenericDigest<Md5>;
impl HashDigest for Md5 {
fn name() -> &'static str {
"md5"
}
fn examples() -> Vec<Example<'static>> {
vec![
Example {
description: "Return the md5 hash of a string, hex-encoded",
example: "'abcdefghijklmnopqrstuvwxyz' | hash md5",
result: Some(Value::string(
"c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
Span::test_data(),
)),
},
Example {
description: "Return the md5 hash of a string, as binary",
example: "'abcdefghijklmnopqrstuvwxyz' | hash md5 --binary",
result: Some(Value::binary(
vec![
0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c,
0xca, 0x67, 0xe1, 0x3b,
],
Span::test_data(),
)),
},
Example {
description: "Return the md5 hash of a file's contents",
example: "open ./nu_0_24_1_windows.zip | hash md5",
result: None,
},
]
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::hash::generic_digest::{self, Arguments};
#[test]
fn test_examples() {
crate::test_examples(HashMd5::default())
}
#[test]
fn hash_string() {
let binary = Value::string("abcdefghijklmnopqrstuvwxyz".to_owned(), Span::test_data());
let expected = Value::string(
"c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
Span::test_data(),
);
let actual = generic_digest::action::<Md5>(
&binary,
&Arguments {
cell_paths: None,
binary: false,
},
Span::test_data(),
);
assert_eq!(actual, expected);
}
#[test]
fn hash_bytes() {
let binary = Value::binary(vec![0xC0, 0xFF, 0xEE], Span::test_data());
let expected = Value::string(
"5f80e231382769b0102b1164cf722d83".to_owned(),
Span::test_data(),
);
let actual = generic_digest::action::<Md5>(
&binary,
&Arguments {
cell_paths: None,
binary: false,
},
Span::test_data(),
);
assert_eq!(actual, expected);
}
}