mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-14 11:53:05 +00:00
New hex_decode and hex_encode command #165
This commit is contained in:
parent
88569da7ad
commit
b6e498279b
24
duckscript_sdk/src/sdk/std/math/hex_decode/help.md
Normal file
24
duckscript_sdk/src/sdk/std/math/hex_decode/help.md
Normal file
|
@ -0,0 +1,24 @@
|
|||
```sh
|
||||
num = hex_decode str
|
||||
```
|
||||
|
||||
Decode a hexadecimal string to the corresponding integer number.<br>
|
||||
Not support negative numbers.
|
||||
|
||||
#### Parameters
|
||||
|
||||
A hexadecimal string.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The corresponding integer number.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
hex_num = set "0xff"
|
||||
num = hex_decode ${hex_num}
|
||||
res = calc ${num} + 1
|
||||
|
||||
assert_eq ${res} "256"
|
||||
```
|
46
duckscript_sdk/src/sdk/std/math/hex_decode/mod.rs
Normal file
46
duckscript_sdk/src/sdk/std/math/hex_decode/mod.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use crate::utils::pckg;
|
||||
use duckscript::types::command::{Command, CommandResult};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "HexDecode")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["hex_decode".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn clone_and_box(&self) -> Box<dyn Command> {
|
||||
Box::new((*self).clone())
|
||||
}
|
||||
|
||||
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||
if arguments.is_empty() {
|
||||
CommandResult::Error("Value not provided.".to_string())
|
||||
} else {
|
||||
match u64::from_str_radix(arguments[0].trim_start_matches("0x"), 16) {
|
||||
Ok(num) => CommandResult::Continue(Some(num.to_string())),
|
||||
Err(error) => CommandResult::Error(error.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
26
duckscript_sdk/src/sdk/std/math/hex_decode/mod_test.rs
Normal file
26
duckscript_sdk/src/sdk/std/math/hex_decode/mod_test.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
use super::*;
|
||||
use crate::sdk::std::math::calc;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_args() {
|
||||
test::run_script_and_error(vec![create("")], "out = hex_decode", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_valid() {
|
||||
test::run_script_and_validate(
|
||||
vec![create(""), calc::create("")],
|
||||
r#"
|
||||
num = hex_decode 0xFF
|
||||
out = calc ${num} + 1
|
||||
"#,
|
||||
CommandValidation::Match("out".to_string(), "256".to_string()),
|
||||
);
|
||||
}
|
22
duckscript_sdk/src/sdk/std/math/hex_encode/help.md
Normal file
22
duckscript_sdk/src/sdk/std/math/hex_encode/help.md
Normal file
|
@ -0,0 +1,22 @@
|
|||
```sh
|
||||
str = hex_encode num
|
||||
```
|
||||
|
||||
Converts an integer number to the corresponding hexadecimal string.<br>
|
||||
Not support negative numbers.
|
||||
|
||||
#### Parameters
|
||||
|
||||
An integer number.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The corresponding hexadecimal string.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
str = hex_encode 255
|
||||
|
||||
assert_eq ${str} "0xff"
|
||||
```
|
46
duckscript_sdk/src/sdk/std/math/hex_encode/mod.rs
Normal file
46
duckscript_sdk/src/sdk/std/math/hex_encode/mod.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
use crate::utils::pckg;
|
||||
use duckscript::types::command::{Command, CommandResult};
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "./mod_test.rs"]
|
||||
mod mod_test;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct CommandImpl {
|
||||
package: String,
|
||||
}
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> String {
|
||||
pckg::concat(&self.package, "HexEncode")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["hex_encode".to_string()]
|
||||
}
|
||||
|
||||
fn help(&self) -> String {
|
||||
include_str!("help.md").to_string()
|
||||
}
|
||||
|
||||
fn clone_and_box(&self) -> Box<dyn Command> {
|
||||
Box::new((*self).clone())
|
||||
}
|
||||
|
||||
fn run(&self, arguments: Vec<String>) -> CommandResult {
|
||||
if arguments.is_empty() {
|
||||
CommandResult::Error("Value not provided.".to_string())
|
||||
} else {
|
||||
match &arguments[0].parse::<u64>() {
|
||||
Ok(num) => CommandResult::Continue(Some(format!("{:#x}", num))),
|
||||
Err(error) => CommandResult::Error(error.to_string())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
22
duckscript_sdk/src/sdk/std/math/hex_encode/mod_test.rs
Normal file
22
duckscript_sdk/src/sdk/std/math/hex_encode/mod_test.rs
Normal file
|
@ -0,0 +1,22 @@
|
|||
use super::*;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_args() {
|
||||
test::run_script_and_error(vec![create("")], "out = hex_encode", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_valid() {
|
||||
test::run_script_and_validate(
|
||||
vec![create("")],
|
||||
"out = hex_encode 255",
|
||||
CommandValidation::Match("out".to_string(), "0xff".to_string()),
|
||||
);
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
mod calc;
|
||||
mod greater_than;
|
||||
mod hex_decode;
|
||||
mod hex_encode;
|
||||
mod less_than;
|
||||
|
||||
use crate::utils::pckg;
|
||||
|
@ -13,6 +15,8 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
|
||||
commands.set(calc::create(&package))?;
|
||||
commands.set(greater_than::create(&package))?;
|
||||
commands.set(hex_encode::create(&package))?;
|
||||
commands.set(hex_decode::create(&package))?;
|
||||
commands.set(less_than::create(&package))?;
|
||||
|
||||
Ok(())
|
||||
|
|
12
test/std/math/hex_decode_test.ds
Normal file
12
test/std/math/hex_decode_test.ds
Normal file
|
@ -0,0 +1,12 @@
|
|||
|
||||
fn test_hex_decode
|
||||
result = hex_decode 0xff
|
||||
|
||||
assert_eq ${result} 255
|
||||
end
|
||||
|
||||
fn test_hex_decode_no_prefix
|
||||
result = hex_decode ff
|
||||
|
||||
assert_eq ${result} 255
|
||||
end
|
6
test/std/math/hex_encode_test.ds
Normal file
6
test/std/math/hex_encode_test.ds
Normal file
|
@ -0,0 +1,6 @@
|
|||
|
||||
fn test_hex_encode
|
||||
result = hex_encode 255
|
||||
|
||||
assert_eq ${result} 0xff
|
||||
end
|
Loading…
Reference in a new issue