mirror of
https://github.com/sagiegurari/duckscript
synced 2024-10-06 08:02:06 +00:00
New write_binary_file and read_binary_file commands #78
This commit is contained in:
parent
242eb33551
commit
65cd9db9aa
|
@ -2,6 +2,8 @@
|
|||
|
||||
### v0.1.9
|
||||
|
||||
* New write_binary_file command #78
|
||||
* New read_binary_file command #78
|
||||
* Rename read/write text file commands.
|
||||
* New base64_decode command #75
|
||||
* New base64_encode command #75
|
||||
|
|
60
docs/sdk.md
60
docs/sdk.md
|
@ -53,7 +53,9 @@
|
|||
* [std::fs::List (ls)](#std__fs__List)
|
||||
* [std::fs::MovePath (mv)](#std__fs__MovePath)
|
||||
* [std::fs::Print (cat)](#std__fs__Print)
|
||||
* [std::fs::ReadBytes (readbinfile, read_binary_file)](#std__fs__ReadBytes)
|
||||
* [std::fs::ReadText (readfile, read_text_file)](#std__fs__ReadText)
|
||||
* [std::fs::WriteBytes (writebinfile, write_binary_file)](#std__fs__WriteBytes)
|
||||
* [std::fs::WriteText (writefile, write_text_file)](#std__fs__WriteText)
|
||||
* [std::math::Calc (calc)](#std__math__Calc)
|
||||
* [std::math::GreaterThan (greater_than)](#std__math__GreaterThan)
|
||||
|
@ -2000,6 +2002,33 @@ cat ./docs/sdk.md
|
|||
#### Aliases:
|
||||
cat
|
||||
|
||||
<a name="std__fs__ReadBytes"></a>
|
||||
## std::fs::ReadBytes
|
||||
```sh
|
||||
handle = read_binary_file file
|
||||
```
|
||||
|
||||
Reads a raw file and returns a handle to the binary data.
|
||||
|
||||
#### Parameters
|
||||
|
||||
A single parameter holding the file path.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The binary data handle.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = read_binary_file ./Cargo.toml
|
||||
text = bytes_to_string ${handle}
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
readbinfile, read_binary_file
|
||||
|
||||
<a name="std__fs__ReadText"></a>
|
||||
## std::fs::ReadText
|
||||
```sh
|
||||
|
@ -2026,6 +2055,35 @@ text = readfile ./Cargo.toml
|
|||
#### Aliases:
|
||||
readfile, read_text_file
|
||||
|
||||
<a name="std__fs__WriteBytes"></a>
|
||||
## std::fs::WriteBytes
|
||||
```sh
|
||||
result = write_binary_file file handle
|
||||
```
|
||||
|
||||
This command enables to write binary data of the provided binary handle into the requested file.<br>
|
||||
It will return true/false value based if it was able to write the binary data to the file.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* The target file
|
||||
* The binary data handle
|
||||
|
||||
#### Return Value
|
||||
|
||||
true/false based if it was able to write the binary data to the file.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = string_to_bytes "some text"
|
||||
result = write_binary_file ./target/tests/data.bin ${handle}
|
||||
```
|
||||
|
||||
|
||||
#### Aliases:
|
||||
writebinfile, write_binary_file
|
||||
|
||||
<a name="std__fs__WriteText"></a>
|
||||
## std::fs::WriteText
|
||||
```sh
|
||||
|
@ -2047,7 +2105,7 @@ true/false based if it was able to write the text to the file.
|
|||
#### Examples
|
||||
|
||||
```sh
|
||||
out = writefile ./target/tests/writefile.txt "line 1\nline 2"
|
||||
result = writefile ./target/tests/writefile.txt "line 1\nline 2"
|
||||
```
|
||||
|
||||
|
||||
|
|
|
@ -7,10 +7,12 @@ mod list;
|
|||
mod mkdir;
|
||||
mod mv;
|
||||
mod print;
|
||||
mod read_bytes;
|
||||
mod read_text;
|
||||
mod rm;
|
||||
mod rmdir;
|
||||
mod touch;
|
||||
mod write_bytes;
|
||||
mod write_text;
|
||||
|
||||
use crate::utils::pckg;
|
||||
|
@ -31,10 +33,12 @@ pub(crate) fn load(commands: &mut Commands, parent: &str) -> Result<(), ScriptEr
|
|||
commands.set(mkdir::create(&package))?;
|
||||
commands.set(mv::create(&package))?;
|
||||
commands.set(print::create(&package))?;
|
||||
commands.set(read_bytes::create(&package))?;
|
||||
commands.set(read_text::create(&package))?;
|
||||
commands.set(rm::create(&package))?;
|
||||
commands.set(rmdir::create(&package))?;
|
||||
commands.set(touch::create(&package))?;
|
||||
commands.set(write_bytes::create(&package))?;
|
||||
commands.set(write_text::create(&package))?;
|
||||
|
||||
Ok(())
|
||||
|
|
20
duckscript_sdk/src/sdk/std/fs/read_bytes/help.md
Normal file
20
duckscript_sdk/src/sdk/std/fs/read_bytes/help.md
Normal file
|
@ -0,0 +1,20 @@
|
|||
```sh
|
||||
handle = read_binary_file file
|
||||
```
|
||||
|
||||
Reads a raw file and returns a handle to the binary data.
|
||||
|
||||
#### Parameters
|
||||
|
||||
A single parameter holding the file path.
|
||||
|
||||
#### Return Value
|
||||
|
||||
The binary data handle.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = read_binary_file ./Cargo.toml
|
||||
text = bytes_to_string ${handle}
|
||||
```
|
69
duckscript_sdk/src/sdk/std/fs/read_bytes/mod.rs
Executable file
69
duckscript_sdk/src/sdk/std/fs/read_bytes/mod.rs
Executable file
|
@ -0,0 +1,69 @@
|
|||
use crate::utils::state::put_handle;
|
||||
use crate::utils::{io, pckg};
|
||||
use duckscript::types::command::{Command, CommandResult, Commands};
|
||||
use duckscript::types::instruction::Instruction;
|
||||
use duckscript::types::runtime::StateValue;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[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, "ReadBytes")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["readbinfile".to_string(), "read_binary_file".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 requires_context(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run_with_context(
|
||||
&self,
|
||||
arguments: Vec<String>,
|
||||
state: &mut HashMap<String, StateValue>,
|
||||
_variables: &mut HashMap<String, String>,
|
||||
_output_variable: Option<String>,
|
||||
_instructions: &Vec<Instruction>,
|
||||
_commands: &mut Commands,
|
||||
_line: usize,
|
||||
) -> CommandResult {
|
||||
if arguments.is_empty() {
|
||||
CommandResult::Error("File name not provided.".to_string())
|
||||
} else {
|
||||
let result = io::read_raw_file(&arguments[0]);
|
||||
|
||||
match result {
|
||||
Ok(binary) => {
|
||||
let key = put_handle(state, StateValue::ByteArray(binary));
|
||||
|
||||
CommandResult::Continue(Some(key))
|
||||
}
|
||||
Err(error) => CommandResult::Error(error.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
31
duckscript_sdk/src/sdk/std/fs/read_bytes/mod_test.rs
Normal file
31
duckscript_sdk/src/sdk/std/fs/read_bytes/mod_test.rs
Normal file
|
@ -0,0 +1,31 @@
|
|||
use super::*;
|
||||
use crate::sdk::std::string::bytes_to_string;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_file_provided() {
|
||||
test::run_script_and_error(vec![create("")], "out = readbinfile", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_file_not_exists() {
|
||||
test::run_script_and_error(vec![create("")], "out = readbinfile ./Cargo2.toml", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_valid() {
|
||||
test::run_script_and_validate(
|
||||
vec![create(""), bytes_to_string::create("")],
|
||||
r#"
|
||||
handle = readbinfile ./Cargo.toml
|
||||
out = bytes_to_string ${handle}
|
||||
"#,
|
||||
CommandValidation::Contains("out".to_string(), "duckscript".to_string()),
|
||||
);
|
||||
}
|
22
duckscript_sdk/src/sdk/std/fs/write_bytes/help.md
Normal file
22
duckscript_sdk/src/sdk/std/fs/write_bytes/help.md
Normal file
|
@ -0,0 +1,22 @@
|
|||
```sh
|
||||
result = write_binary_file file handle
|
||||
```
|
||||
|
||||
This command enables to write binary data of the provided binary handle into the requested file.<br>
|
||||
It will return true/false value based if it was able to write the binary data to the file.
|
||||
|
||||
#### Parameters
|
||||
|
||||
* The target file
|
||||
* The binary data handle
|
||||
|
||||
#### Return Value
|
||||
|
||||
true/false based if it was able to write the binary data to the file.
|
||||
|
||||
#### Examples
|
||||
|
||||
```sh
|
||||
handle = string_to_bytes "some text"
|
||||
result = write_binary_file ./target/tests/data.bin ${handle}
|
||||
```
|
81
duckscript_sdk/src/sdk/std/fs/write_bytes/mod.rs
Executable file
81
duckscript_sdk/src/sdk/std/fs/write_bytes/mod.rs
Executable file
|
@ -0,0 +1,81 @@
|
|||
use crate::utils::state::get_handles_sub_state;
|
||||
use crate::utils::{io, pckg};
|
||||
use duckscript::types::command::{Command, CommandResult, Commands};
|
||||
use duckscript::types::instruction::Instruction;
|
||||
use duckscript::types::runtime::StateValue;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[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, "WriteBytes")
|
||||
}
|
||||
|
||||
fn aliases(&self) -> Vec<String> {
|
||||
vec!["writebinfile".to_string(), "write_binary_file".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 requires_context(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run_with_context(
|
||||
&self,
|
||||
arguments: Vec<String>,
|
||||
state: &mut HashMap<String, StateValue>,
|
||||
_variables: &mut HashMap<String, String>,
|
||||
_output_variable: Option<String>,
|
||||
_instructions: &Vec<Instruction>,
|
||||
_commands: &mut Commands,
|
||||
_line: usize,
|
||||
) -> CommandResult {
|
||||
if arguments.is_empty() {
|
||||
CommandResult::Error("File name and text not provided.".to_string())
|
||||
} else if arguments.len() == 1 {
|
||||
CommandResult::Error("Binary data handle not provided.".to_string())
|
||||
} else {
|
||||
let state = get_handles_sub_state(state);
|
||||
|
||||
let key = &arguments[1];
|
||||
|
||||
match state.get(key) {
|
||||
Some(state_value) => match state_value {
|
||||
StateValue::ByteArray(binary) => {
|
||||
let result = io::write_to_file(&arguments[0], &binary, false);
|
||||
|
||||
match result {
|
||||
Ok(_) => CommandResult::Continue(Some("true".to_string())),
|
||||
Err(_) => CommandResult::Continue(Some("false".to_string())),
|
||||
}
|
||||
}
|
||||
_ => CommandResult::Error("Invalid handle provided.".to_string()),
|
||||
},
|
||||
None => CommandResult::Error(
|
||||
format!("Array for handle: {} not found.", key).to_string(),
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn create(package: &str) -> Box<dyn Command> {
|
||||
Box::new(CommandImpl {
|
||||
package: package.to_string(),
|
||||
})
|
||||
}
|
37
duckscript_sdk/src/sdk/std/fs/write_bytes/mod_test.rs
Normal file
37
duckscript_sdk/src/sdk/std/fs/write_bytes/mod_test.rs
Normal file
|
@ -0,0 +1,37 @@
|
|||
use super::*;
|
||||
use crate::sdk::std::string::string_to_bytes;
|
||||
use crate::test;
|
||||
use crate::test::CommandValidation;
|
||||
|
||||
#[test]
|
||||
fn common_functions() {
|
||||
test::test_common_command_functions(create(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_file_provided() {
|
||||
test::run_script_and_error(vec![create("")], "out = write_binary_file", "out");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_no_text_provided() {
|
||||
test::run_script_and_error(
|
||||
vec![create("")],
|
||||
"out = write_binary_file ./target/_duckscript/write/write_binary_file.txt",
|
||||
"out",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_valid() {
|
||||
test::run_script_and_validate(
|
||||
vec![create(""), string_to_bytes::create("")],
|
||||
r#"
|
||||
handle = string_to_bytes "line 1\nline 2"
|
||||
out = write_binary_file ./target/_duckscript/write/write_binary_file.txt ${handle}
|
||||
"#,
|
||||
CommandValidation::Match("out".to_string(), "true".to_string()),
|
||||
);
|
||||
let text = io::read_text_file("./target/_duckscript/write/write_binary_file.txt").unwrap();
|
||||
assert_eq!(text, "line 1\nline 2")
|
||||
}
|
|
@ -17,5 +17,5 @@ true/false based if it was able to write the text to the file.
|
|||
#### Examples
|
||||
|
||||
```sh
|
||||
out = writefile ./target/tests/writefile.txt "line 1\nline 2"
|
||||
result = writefile ./target/tests/writefile.txt "line 1\nline 2"
|
||||
```
|
||||
|
|
|
@ -74,9 +74,31 @@ pub(crate) fn read_text_file(file: &str) -> Result<String, ScriptError> {
|
|||
match File::open(&file_path) {
|
||||
Ok(mut fd) => {
|
||||
let mut content = String::new();
|
||||
fd.read_to_string(&mut content).unwrap();
|
||||
match fd.read_to_string(&mut content) {
|
||||
Ok(_) => Ok(content),
|
||||
Err(error) => Err(ScriptError {
|
||||
info: ErrorInfo::ErrorReadingFile(file.to_string(), Some(error)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
Err(error) => Err(ScriptError {
|
||||
info: ErrorInfo::ErrorReadingFile(file.to_string(), Some(error)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
Ok(content)
|
||||
pub(crate) fn read_raw_file(file: &str) -> Result<Vec<u8>, ScriptError> {
|
||||
let file_path = Path::new(file);
|
||||
|
||||
match File::open(&file_path) {
|
||||
Ok(mut fd) => {
|
||||
let mut content = vec![];
|
||||
match fd.read_to_end(&mut content) {
|
||||
Ok(_) => Ok(content),
|
||||
Err(error) => Err(ScriptError {
|
||||
info: ErrorInfo::ErrorReadingFile(file.to_string(), Some(error)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
Err(error) => Err(ScriptError {
|
||||
info: ErrorInfo::ErrorReadingFile(file.to_string(), Some(error)),
|
||||
|
@ -89,6 +111,10 @@ pub(crate) fn write_text_file(file: &str, text: &str) -> Result<(), ScriptError>
|
|||
}
|
||||
|
||||
pub(crate) fn write_to_text_file(file: &str, text: &str, append: bool) -> Result<(), ScriptError> {
|
||||
write_to_file(file, text.as_bytes(), append)
|
||||
}
|
||||
|
||||
pub(crate) fn write_to_file(file: &str, data: &[u8], append: bool) -> Result<(), ScriptError> {
|
||||
let file_path = Path::new(file);
|
||||
|
||||
// create parent directory
|
||||
|
@ -111,7 +137,7 @@ pub(crate) fn write_to_text_file(file: &str, text: &str, append: bool) -> Result
|
|||
};
|
||||
|
||||
match result {
|
||||
Ok(mut fd) => match fd.write_all(text.as_bytes()) {
|
||||
Ok(mut fd) => match fd.write_all(data) {
|
||||
Err(_) => Err(ScriptError {
|
||||
info: ErrorInfo::Runtime(
|
||||
format!("Error writing to file: {}", file).to_string(),
|
||||
|
|
Loading…
Reference in a new issue