2021-12-10 23:14:28 +00:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
Make plugin commands support examples. (#7984)
# Description
As title, we can't provide examples for plugin commands, this pr would
make it possible
# User-Facing Changes
Take plugin `nu-example-1` as example:
```
❯ nu-example-1 -h
PluginSignature test 1 for plugin. Returns Value::Nothing
Usage:
> nu-example-1 {flags} <a> <b> (opt) ...(rest)
Flags:
-h, --help - Display the help message for this command
-f, --flag - a flag for the signature
-n, --named <String> - named string
Parameters:
a <int>: required integer value
b <string>: required string value
(optional) opt <int>: Optional number
...rest <string>: rest value string
Examples:
running example with an int value and string value
> nu-example-1 3 bb
```
The examples session is newly added.
## Basic idea behind these changes
when nushell query plugin signatures, plugin just returns it's signature
without any examples, so nushell have no idea about the examples of
plugin commands.
To adding the feature, we just making plugin returns it's signature with
examples.
Before:
```
1. get signature
---------------->
Nushell ------------------ Plugin
<-----------------
2. returns Vec<Signature>
```
After:
```
1. get signature
---------------->
Nushell ------------------ Plugin
<-----------------
2. returns Vec<PluginSignature>
```
When writing plugin signature to $nu.plugin-path:
Serialize `<PluginSignature>` rather than `<Signature>`, which would
enable us to serialize examples to `$nu.plugin-path`
## Shortcoming
It's a breaking changes because `Plugin::signature` is changed, and it
requires plugin authors to change their code for new signatures.
Fortunally it should be easy to change, for rust based plugin, we just
need to make a global replace from word `Signature` to word
`PluginSignature` in their plugin project.
Our content of plugin-path is really large, if one plugin have many
examples, it'd results to larger body of $nu.plugin-path, which is not
really scale. A solution would be save register information in other
binary formats rather than `json`. But I think it'd be another story.
# 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 -A
clippy::needless_collect` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass
# 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.
2023-02-08 22:14:18 +00:00
|
|
|
fn examples() -> Vec<Example<'static>> {
|
2021-12-10 23:14:28 +00:00
|
|
|
vec![
|
|
|
|
Example {
|
2022-11-09 21:55:05 +00:00
|
|
|
description: "Return the md5 hash of a string, hex-encoded",
|
2022-12-16 16:51:00 +00:00
|
|
|
example: "'abcdefghijklmnopqrstuvwxyz' | hash md5",
|
2021-12-10 23:14:28 +00:00
|
|
|
result: Some(Value::String {
|
|
|
|
val: "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
|
2021-12-19 07:46:13 +00:00
|
|
|
span: Span::test_data(),
|
2021-12-10 23:14:28 +00:00
|
|
|
}),
|
|
|
|
},
|
2022-06-26 11:50:56 +00:00
|
|
|
Example {
|
2022-11-09 21:55:05 +00:00
|
|
|
description: "Return the md5 hash of a string, as binary",
|
2022-12-16 16:51:00 +00:00
|
|
|
example: "'abcdefghijklmnopqrstuvwxyz' | hash md5 --binary",
|
2022-06-26 11:50:56 +00:00
|
|
|
result: Some(Value::Binary {
|
|
|
|
val: vec![
|
|
|
|
0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c,
|
|
|
|
0xca, 0x67, 0xe1, 0x3b,
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
2021-12-10 23:14:28 +00:00
|
|
|
Example {
|
2022-11-09 21:55:05 +00:00
|
|
|
description: "Return the md5 hash of a file's contents",
|
2021-12-10 23:14:28 +00:00
|
|
|
example: "open ./nu_0_24_1_windows.zip | hash md5",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2022-11-01 11:40:11 +00:00
|
|
|
use crate::hash::generic_digest::{self, Arguments};
|
2021-12-10 23:14:28 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
crate::test_examples(HashMd5::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hash_string() {
|
|
|
|
let binary = Value::String {
|
|
|
|
val: "abcdefghijklmnopqrstuvwxyz".to_owned(),
|
2021-12-19 07:46:13 +00:00
|
|
|
span: Span::test_data(),
|
2021-12-10 23:14:28 +00:00
|
|
|
};
|
|
|
|
let expected = Value::String {
|
|
|
|
val: "c3fcd3d76192e4007dfb496cca67e13b".to_owned(),
|
2021-12-19 07:46:13 +00:00
|
|
|
span: Span::test_data(),
|
2021-12-10 23:14:28 +00:00
|
|
|
};
|
2022-11-01 11:40:11 +00:00
|
|
|
let actual = generic_digest::action::<Md5>(
|
|
|
|
&binary,
|
|
|
|
&Arguments {
|
|
|
|
cell_paths: None,
|
|
|
|
binary: false,
|
|
|
|
},
|
|
|
|
Span::test_data(),
|
|
|
|
);
|
2021-12-10 23:14:28 +00:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hash_bytes() {
|
|
|
|
let binary = Value::Binary {
|
|
|
|
val: vec![0xC0, 0xFF, 0xEE],
|
2021-12-19 07:46:13 +00:00
|
|
|
span: Span::test_data(),
|
2021-12-10 23:14:28 +00:00
|
|
|
};
|
|
|
|
let expected = Value::String {
|
|
|
|
val: "5f80e231382769b0102b1164cf722d83".to_owned(),
|
2021-12-19 07:46:13 +00:00
|
|
|
span: Span::test_data(),
|
2021-12-10 23:14:28 +00:00
|
|
|
};
|
2022-11-01 11:40:11 +00:00
|
|
|
let actual = generic_digest::action::<Md5>(
|
|
|
|
&binary,
|
|
|
|
&Arguments {
|
|
|
|
cell_paths: None,
|
|
|
|
binary: false,
|
|
|
|
},
|
|
|
|
Span::test_data(),
|
|
|
|
);
|
2021-12-10 23:14:28 +00:00
|
|
|
assert_eq!(actual, expected);
|
|
|
|
}
|
|
|
|
}
|