This commit is contained in:
sagie gur ari 2021-03-08 22:56:31 +00:00
parent 63b55e0961
commit df22d6dad9
13 changed files with 141 additions and 240 deletions

View file

@ -1,5 +1,9 @@
## CHANGELOG ## CHANGELOG
### v0.8.0 (2021-03-08)
* Runtime - \[Breaking Change\] Make script errors chainable.
### v0.7.4 (2021-03-05) ### v0.7.4 (2021-03-05)
* Fix flow control commands scope handling of call info stack * Fix flow control commands scope handling of call info stack

112
Cargo.lock generated
View file

@ -118,14 +118,14 @@ dependencies = [
[[package]] [[package]]
name = "duckscript" name = "duckscript"
version = "0.6.0" version = "0.7.0"
dependencies = [ dependencies = [
"fsio", "fsio",
] ]
[[package]] [[package]]
name = "duckscript_cli" name = "duckscript_cli"
version = "0.7.4" version = "0.8.0"
dependencies = [ dependencies = [
"duckscript", "duckscript",
"duckscriptsdk", "duckscriptsdk",
@ -133,7 +133,7 @@ dependencies = [
[[package]] [[package]]
name = "duckscriptsdk" name = "duckscriptsdk"
version = "0.7.4" version = "0.8.0"
dependencies = [ dependencies = [
"attohttpc", "attohttpc",
"base64", "base64",
@ -147,7 +147,7 @@ dependencies = [
"java-properties", "java-properties",
"meval", "meval",
"num_cpus", "num_cpus",
"rand 0.8.3", "rand",
"semver", "semver",
"serde_json", "serde_json",
"uname", "uname",
@ -271,11 +271,11 @@ checksum = "2022715d62ab30faffd124d40b76f4134a550a87792276512b18d63272333394"
[[package]] [[package]]
name = "fsio" name = "fsio"
version = "0.1.3" version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1fd087255f739f4f1aeea69f11b72f8080e9c2e7645cd06955dad4a178a49e3" checksum = "a50045aa8931ae01afbc5d72439e8f57f326becb8c70d07dfc816778eff3d167"
dependencies = [ dependencies = [
"rand 0.7.3", "rand",
"users", "users",
] ]
@ -290,17 +290,6 @@ dependencies = [
"regex 0.1.80", "regex 0.1.80",
] ]
[[package]]
name = "getrandom"
version = "0.1.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce"
dependencies = [
"cfg-if",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.2" version = "0.2.2"
@ -309,7 +298,7 @@ checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"wasi 0.10.2+wasi-snapshot-preview1", "wasi",
] ]
[[package]] [[package]]
@ -408,9 +397,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.87" version = "0.2.88"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213" checksum = "03b07a082330a35e43f63177cc01689da34fbffa0105e1246cf0311472cac73a"
[[package]] [[package]]
name = "log" name = "log"
@ -621,19 +610,6 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom 0.1.16",
"libc",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc 0.2.0",
]
[[package]] [[package]]
name = "rand" name = "rand"
version = "0.8.3" version = "0.8.3"
@ -641,19 +617,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [ dependencies = [
"libc", "libc",
"rand_chacha 0.3.0", "rand_chacha",
"rand_core 0.6.2", "rand_core",
"rand_hc 0.3.0", "rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core 0.5.1",
] ]
[[package]] [[package]]
@ -663,16 +629,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [ dependencies = [
"ppv-lite86", "ppv-lite86",
"rand_core 0.6.2", "rand_core",
]
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom 0.1.16",
] ]
[[package]] [[package]]
@ -681,16 +638,7 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [ dependencies = [
"getrandom 0.2.2", "getrandom",
]
[[package]]
name = "rand_hc"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core 0.5.1",
] ]
[[package]] [[package]]
@ -699,7 +647,7 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [ dependencies = [
"rand_core 0.6.2", "rand_core",
] ]
[[package]] [[package]]
@ -784,9 +732,9 @@ dependencies = [
[[package]] [[package]]
name = "security-framework" name = "security-framework"
version = "2.1.1" version = "2.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfd318104249865096c8da1dfabf09ddbb6d0330ea176812a62ec75e40c4166" checksum = "d493c5f39e02dfb062cd8f33301f90f9b13b650e8c1b1d0fd75c19dd64bff69d"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"core-foundation", "core-foundation",
@ -825,9 +773,9 @@ dependencies = [
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.123" version = "1.0.124"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f"
[[package]] [[package]]
name = "serde_json" name = "serde_json"
@ -842,9 +790,9 @@ dependencies = [
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.61" version = "1.0.62"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed22b90a0e734a23a7610f4283ac9e5acfb96cbb30dfefa540d66f866f1c09c5" checksum = "123a78a3596b24fee53a6464ce52d8ecbf62241e6294c7e7fe12086cd161f512"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -859,7 +807,7 @@ checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"rand 0.8.3", "rand",
"redox_syscall", "redox_syscall",
"remove_dir_all", "remove_dir_all",
"winapi 0.3.9", "winapi 0.3.9",
@ -991,9 +939,9 @@ dependencies = [
[[package]] [[package]]
name = "users" name = "users"
version = "0.10.0" version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa4227e95324a443c9fcb06e03d4d85e91aabe9a5a02aa818688b6918b6af486" checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"
dependencies = [ dependencies = [
"libc", "libc",
"log", "log",
@ -1022,12 +970,6 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.10.2+wasi-snapshot-preview1" version = "0.10.2+wasi-snapshot-preview1"
@ -1120,9 +1062,9 @@ dependencies = [
[[package]] [[package]]
name = "wildmatch" name = "wildmatch"
version = "1.0.13" version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2399814fda0d6999a6bfe9b5c7660d836334deb162a09db8b73d5b38fd8c40a" checksum = "7f44b95f62d34113cf558c93511ac93027e03e9c29a60dd0fd70e6e025c7270a"
[[package]] [[package]]
name = "winapi" name = "winapi"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "duckscript" name = "duckscript"
version = "0.6.0" version = "0.7.0"
authors = ["Sagie Gur-Ari <sagiegurari@gmail.com>"] authors = ["Sagie Gur-Ari <sagiegurari@gmail.com>"]
description = "Simple, extendable and embeddable scripting language." description = "Simple, extendable and embeddable scripting language."
license = "Apache-2.0" license = "Apache-2.0"
@ -23,7 +23,7 @@ include = [
] ]
[dependencies] [dependencies]
fsio = "^0.1" fsio = "^0.2"
[badges.codecov] [badges.codecov]
branch = "master" branch = "master"

View file

@ -8,7 +8,7 @@
mod parser_test; mod parser_test;
use crate::preprocessor; use crate::preprocessor;
use crate::types::error::{ErrorInfo, ScriptError}; use crate::types::error::ScriptError;
use crate::types::instruction::{ use crate::types::instruction::{
Instruction, InstructionMetaInfo, InstructionType, PreProcessInstruction, ScriptInstruction, Instruction, InstructionMetaInfo, InstructionType, PreProcessInstruction, ScriptInstruction,
}; };
@ -25,9 +25,7 @@ pub fn parse_file(file: &str) -> Result<Vec<Instruction>, ScriptError> {
match read_text_file(file) { match read_text_file(file) {
Ok(text) => parse_lines(&text, meta_info), Ok(text) => parse_lines(&text, meta_info),
Err(error) => Err(ScriptError { Err(error) => Err(ScriptError::ErrorReadingFile(file.to_string(), Some(error))),
info: ErrorInfo::ErrorReadingFile(file.to_string(), Some(error)),
}),
} }
} }
@ -92,9 +90,7 @@ fn parse_pre_process_line(
start_index: usize, start_index: usize,
) -> Result<Instruction, ScriptError> { ) -> Result<Instruction, ScriptError> {
if line_text.is_empty() { if line_text.is_empty() {
Err(ScriptError { Err(ScriptError::PreProcessNoCommandFound(meta_info))
info: ErrorInfo::PreProcessNoCommandFound(meta_info),
})
} else { } else {
let mut command = String::new(); let mut command = String::new();
let mut index = start_index; let mut index = start_index;
@ -113,9 +109,7 @@ fn parse_pre_process_line(
} }
if command.is_empty() { if command.is_empty() {
Err(ScriptError { Err(ScriptError::PreProcessNoCommandFound(meta_info))
info: ErrorInfo::PreProcessNoCommandFound(meta_info),
})
} else { } else {
match parse_arguments(&meta_info, &line_text, index) { match parse_arguments(&meta_info, &line_text, index) {
Ok(arguments) => { Ok(arguments) => {
@ -289,9 +283,7 @@ fn parse_next_value(
in_control = false; in_control = false;
found_variable_prefix = false; found_variable_prefix = false;
} else { } else {
return Err(ScriptError { return Err(ScriptError::ControlWithoutValidValue(meta_info.clone()));
info: ErrorInfo::ControlWithoutValidValue(meta_info.clone()),
});
} }
} else if character == '\\' || character == '"' { } else if character == '\\' || character == '"' {
argument.push(character); argument.push(character);
@ -308,9 +300,7 @@ fn parse_next_value(
} else if character == '$' { } else if character == '$' {
found_variable_prefix = true; found_variable_prefix = true;
} else { } else {
return Err(ScriptError { return Err(ScriptError::ControlWithoutValidValue(meta_info.clone()));
info: ErrorInfo::ControlWithoutValidValue(meta_info.clone()),
});
} }
} else if character == '\\' { } else if character == '\\' {
if control_as_char { if control_as_char {
@ -319,9 +309,7 @@ fn parse_next_value(
in_control = true; in_control = true;
found_variable_prefix = false; found_variable_prefix = false;
} else { } else {
return Err(ScriptError { return Err(ScriptError::InvalidControlLocation(meta_info.clone()));
info: ErrorInfo::InvalidControlLocation(meta_info.clone()),
});
} }
} else if using_quotes && character == '"' { } else if using_quotes && character == '"' {
found_end = true; found_end = true;
@ -351,9 +339,7 @@ fn parse_next_value(
if allow_quotes { if allow_quotes {
using_quotes = true; using_quotes = true;
} else { } else {
return Err(ScriptError { return Err(ScriptError::InvalidQuotesLocation(meta_info.clone()));
info: ErrorInfo::InvalidQuotesLocation(meta_info.clone()),
});
} }
} else if character == '\\' { } else if character == '\\' {
if control_as_char { if control_as_char {
@ -361,9 +347,7 @@ fn parse_next_value(
} else if allow_control { } else if allow_control {
in_control = true; in_control = true;
} else { } else {
return Err(ScriptError { return Err(ScriptError::InvalidControlLocation(meta_info.clone()));
info: ErrorInfo::InvalidControlLocation(meta_info.clone()),
});
} }
} else { } else {
argument.push(character); argument.push(character);
@ -373,13 +357,9 @@ fn parse_next_value(
if in_argument && !found_end && (in_control || using_quotes) { if in_argument && !found_end && (in_control || using_quotes) {
if in_control { if in_control {
Err(ScriptError { Err(ScriptError::ControlWithoutValidValue(meta_info.clone()))
info: ErrorInfo::ControlWithoutValidValue(meta_info.clone()),
})
} else { } else {
Err(ScriptError { Err(ScriptError::MissingEndQuotes(meta_info.clone()))
info: ErrorInfo::MissingEndQuotes(meta_info.clone()),
})
} }
} else if argument.is_empty() { } else if argument.is_empty() {
if using_quotes { if using_quotes {
@ -418,9 +398,7 @@ fn find_label(
match value { match value {
Some(label_value) => { Some(label_value) => {
if label_value.is_empty() { if label_value.is_empty() {
return Err(ScriptError { return Err(ScriptError::EmptyLabel(meta_info.clone()));
info: ErrorInfo::EmptyLabel(meta_info.clone()),
});
} }
let mut text = String::new(); let mut text = String::new();

View file

@ -11,7 +11,7 @@ mod mod_test;
mod include_files_preprocessor; mod include_files_preprocessor;
mod print_preprocessor; mod print_preprocessor;
use crate::types::error::{ErrorInfo, ScriptError}; use crate::types::error::ScriptError;
use crate::types::instruction::{Instruction, InstructionType}; use crate::types::instruction::{Instruction, InstructionType};
pub(crate) fn run(instruction: &Instruction) -> Result<Vec<Instruction>, ScriptError> { pub(crate) fn run(instruction: &Instruction) -> Result<Vec<Instruction>, ScriptError> {
@ -26,13 +26,13 @@ pub(crate) fn run(instruction: &Instruction) -> Result<Vec<Instruction>, ScriptE
&instruction_type.arguments, &instruction_type.arguments,
&instruction.meta_info, &instruction.meta_info,
), ),
_ => Err(ScriptError { _ => Err(ScriptError::UnknownPreProcessorCommand(
info: ErrorInfo::UnknownPreProcessorCommand(instruction.meta_info.clone()), instruction.meta_info.clone(),
}), )),
}, },
None => Err(ScriptError { None => Err(ScriptError::PreProcessNoCommandFound(
info: ErrorInfo::PreProcessNoCommandFound(instruction.meta_info.clone()), instruction.meta_info.clone(),
}), )),
}, },
_ => Ok(vec![]), _ => Ok(vec![]),
} }

View file

@ -10,7 +10,7 @@ mod runner_test;
use crate::expansion::{self, ExpandedValue}; use crate::expansion::{self, ExpandedValue};
use crate::parser; use crate::parser;
use crate::types::command::{CommandResult, Commands, GoToValue}; use crate::types::command::{CommandResult, Commands, GoToValue};
use crate::types::error::{ErrorInfo, ScriptError}; use crate::types::error::ScriptError;
use crate::types::instruction::{ use crate::types::instruction::{
Instruction, InstructionMetaInfo, InstructionType, ScriptInstruction, Instruction, InstructionMetaInfo, InstructionType, ScriptInstruction,
}; };
@ -74,9 +74,10 @@ pub fn repl(mut context: Context) -> Result<Context, ScriptError> {
} }
} }
Err(error) => { Err(error) => {
return Err(ScriptError { return Err(ScriptError::Runtime(
info: ErrorInfo::Runtime(error.to_string(), Some(InstructionMetaInfo::new())), error.to_string(),
}); Some(InstructionMetaInfo::new()),
));
} }
}; };
} }
@ -165,12 +166,10 @@ fn run_instructions(
if let Some(exit_code_str) = output { if let Some(exit_code_str) = output {
if let Ok(exit_code) = exit_code_str.parse::<i32>() { if let Ok(exit_code) = exit_code_str.parse::<i32>() {
if exit_code != 0 { if exit_code != 0 {
return Err(ScriptError { return Err(ScriptError::Runtime(
info: ErrorInfo::Runtime( format!("Exit with error code: {}", exit_code).to_string(),
format!("Exit with error code: {}", exit_code).to_string(), Some(meta_info.clone()),
Some(meta_info.clone()), ));
),
});
} }
} }
} }
@ -195,9 +194,7 @@ fn run_instructions(
meta_info.clone(), meta_info.clone(),
) { ) {
Err(error) => { Err(error) => {
return Err(ScriptError { return Err(ScriptError::Runtime(error, Some(meta_info.clone())));
info: ErrorInfo::Runtime(error, Some(meta_info.clone())),
});
} }
_ => (), _ => (),
}; };
@ -207,9 +204,7 @@ fn run_instructions(
() ()
} }
CommandResult::Crash(error) => { CommandResult::Crash(error) => {
let script_error = ScriptError { let script_error = ScriptError::Runtime(error, Some(meta_info));
info: ErrorInfo::Runtime(error, Some(meta_info)),
};
if repl_mode { if repl_mode {
return Ok((runtime.context, EndReason::Crash(script_error))); return Ok((runtime.context, EndReason::Crash(script_error)));
@ -231,12 +226,10 @@ fn run_instructions(
GoToValue::Label(label) => match runtime.label_to_line.get(&label) { GoToValue::Label(label) => match runtime.label_to_line.get(&label) {
Some(value) => line = *value, Some(value) => line = *value,
None => { None => {
return Err(ScriptError { return Err(ScriptError::Runtime(
info: ErrorInfo::Runtime( format!("Label: {} not found.", label),
format!("Label: {} not found.", label), Some(meta_info),
Some(meta_info), ));
),
});
} }
}, },
GoToValue::Line(line_number) => line = line_number, GoToValue::Line(line_number) => line = line_number,

View file

@ -7,7 +7,7 @@
#[path = "./command_test.rs"] #[path = "./command_test.rs"]
mod command_test; mod command_test;
use crate::types::error::{ErrorInfo, ScriptError}; use crate::types::error::ScriptError;
use crate::types::instruction::Instruction; use crate::types::instruction::Instruction;
use crate::types::runtime::StateValue; use crate::types::runtime::StateValue;
use std::collections::HashMap; use std::collections::HashMap;
@ -123,19 +123,18 @@ impl Commands {
let aliases = command.aliases(); let aliases = command.aliases();
if self.commands.contains_key(&name) { if self.commands.contains_key(&name) {
return Err(ScriptError { return Err(ScriptError::Initialization(format!(
info: ErrorInfo::Initialization(format!("Command: {} already defined.", &name)), "Command: {} already defined.",
}); &name
)));
} }
for alias in &aliases { for alias in &aliases {
if self.aliases.contains_key(alias) { if self.aliases.contains_key(alias) {
return Err(ScriptError { return Err(ScriptError::Initialization(format!(
info: ErrorInfo::Initialization(format!( "Alias: {} for command: {} already defined.",
"Alias: {} for command: {} already defined.", &alias, &name
&alias, &name )));
)),
});
} }
} }

View file

@ -5,6 +5,7 @@
use crate::types::instruction::InstructionMetaInfo; use crate::types::instruction::InstructionMetaInfo;
use fsio::error::FsIOError; use fsio::error::FsIOError;
use std::error::Error;
use std::fmt; use std::fmt;
use std::fmt::Display; use std::fmt::Display;
@ -31,7 +32,7 @@ fn format_error_message(
#[derive(Debug)] #[derive(Debug)]
/// Holds the error information /// Holds the error information
pub enum ErrorInfo { pub enum ScriptError {
/// Error Info Type /// Error Info Type
ErrorReadingFile(String, Option<FsIOError>), ErrorReadingFile(String, Option<FsIOError>),
/// Error Info Type /// Error Info Type
@ -58,59 +59,74 @@ pub enum ErrorInfo {
UnknownPreProcessorCommand(InstructionMetaInfo), UnknownPreProcessorCommand(InstructionMetaInfo),
} }
#[derive(Debug)]
/// Script error struct
pub struct ScriptError {
/// Holds the error information
pub info: ErrorInfo,
}
impl Display for ScriptError { impl Display for ScriptError {
/// Formats the script error using the given formatter. /// Formats the script error using the given formatter.
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self.info { match self {
ErrorInfo::ErrorReadingFile(ref file, ref cause) => { Self::ErrorReadingFile(ref file, ref cause) => {
writeln!(formatter, "Error reading file: {}", file)?; writeln!(formatter, "Error reading file: {}", file)?;
match cause { match cause {
Some(cause_err) => cause_err.fmt(formatter), Some(cause_err) => cause_err.fmt(formatter),
None => Ok(()), None => Ok(()),
} }
} }
ErrorInfo::Initialization(ref message) => write!(formatter, "{}", message), Self::Initialization(ref message) => write!(formatter, "{}", message),
ErrorInfo::Runtime(ref message, ref meta_info) => { Self::Runtime(ref message, ref meta_info) => {
let empty_meta_data = InstructionMetaInfo::new(); let empty_meta_data = InstructionMetaInfo::new();
let meta_info_value = meta_info.as_ref().unwrap_or(&empty_meta_data); let meta_info_value = meta_info.as_ref().unwrap_or(&empty_meta_data);
format_error_message(formatter, &meta_info_value, message) format_error_message(formatter, &meta_info_value, message)
} }
ErrorInfo::PreProcessNoCommandFound(ref meta_info) => { Self::PreProcessNoCommandFound(ref meta_info) => {
format_error_message(formatter, &meta_info, "preprocessor is missing a command") format_error_message(formatter, &meta_info, "preprocessor is missing a command")
} }
ErrorInfo::ControlWithoutValidValue(ref meta_info) => format_error_message( Self::ControlWithoutValidValue(ref meta_info) => format_error_message(
formatter, formatter,
&meta_info, &meta_info,
"control character found without a valid value", "control character found without a valid value",
), ),
ErrorInfo::InvalidControlLocation(ref meta_info) => { Self::InvalidControlLocation(ref meta_info) => {
format_error_message(formatter, &meta_info, "invalid control character location") format_error_message(formatter, &meta_info, "invalid control character location")
} }
ErrorInfo::MissingEndQuotes(ref meta_info) => { Self::MissingEndQuotes(ref meta_info) => {
format_error_message(formatter, &meta_info, "missing end quotes") format_error_message(formatter, &meta_info, "missing end quotes")
} }
ErrorInfo::MissingOutputVariableName(ref meta_info) => { Self::MissingOutputVariableName(ref meta_info) => {
format_error_message(formatter, &meta_info, "missing variable name") format_error_message(formatter, &meta_info, "missing variable name")
} }
ErrorInfo::InvalidEqualsLocation(ref meta_info) => { Self::InvalidEqualsLocation(ref meta_info) => {
format_error_message(formatter, &meta_info, "invalid equals sign location") format_error_message(formatter, &meta_info, "invalid equals sign location")
} }
ErrorInfo::InvalidQuotesLocation(ref meta_info) => { Self::InvalidQuotesLocation(ref meta_info) => {
format_error_message(formatter, &meta_info, "invalid quotes location") format_error_message(formatter, &meta_info, "invalid quotes location")
} }
ErrorInfo::EmptyLabel(ref meta_info) => { Self::EmptyLabel(ref meta_info) => {
format_error_message(formatter, &meta_info, "empty lable found") format_error_message(formatter, &meta_info, "empty lable found")
} }
ErrorInfo::UnknownPreProcessorCommand(ref meta_info) => { Self::UnknownPreProcessorCommand(ref meta_info) => {
format_error_message(formatter, &meta_info, "unknow preprocessor command") format_error_message(formatter, &meta_info, "unknow preprocessor command")
} }
} }
} }
} }
impl Error for ScriptError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::ErrorReadingFile(_, error) => error.as_ref().map(|fsio_error| {
let std_error: &dyn Error = fsio_error;
std_error
}),
Self::Initialization(_) => None,
Self::Runtime(_, _) => None,
Self::PreProcessNoCommandFound(_) => None,
Self::ControlWithoutValidValue(_) => None,
Self::InvalidControlLocation(_) => None,
Self::MissingEndQuotes(_) => None,
Self::MissingOutputVariableName(_) => None,
Self::InvalidEqualsLocation(_) => None,
Self::InvalidQuotesLocation(_) => None,
Self::EmptyLabel(_) => None,
Self::UnknownPreProcessorCommand(_) => None,
}
}
}

View file

@ -2,104 +2,78 @@ use super::*;
#[test] #[test]
fn display_error_reading_file() { fn display_error_reading_file() {
let error = ScriptError { let error = ScriptError::ErrorReadingFile("test".to_string(), None);
info: ErrorInfo::ErrorReadingFile("test".to_string(), None),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_initialization() { fn display_initialization() {
let error = ScriptError { let error = ScriptError::Initialization("test".to_string());
info: ErrorInfo::Initialization("test".to_string()),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_runtime_with_meta_info() { fn display_runtime_with_meta_info() {
let error = ScriptError { let error = ScriptError::Runtime("test".to_string(), Some(InstructionMetaInfo::new()));
info: ErrorInfo::Runtime("test".to_string(), Some(InstructionMetaInfo::new())),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_runtime_without_meta_info() { fn display_runtime_without_meta_info() {
let error = ScriptError { let error = ScriptError::Runtime("test".to_string(), None);
info: ErrorInfo::Runtime("test".to_string(), None),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_pre_process_no_command_found() { fn display_pre_process_no_command_found() {
let error = ScriptError { let error = ScriptError::PreProcessNoCommandFound(InstructionMetaInfo::new());
info: ErrorInfo::PreProcessNoCommandFound(InstructionMetaInfo::new()),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_control_without_valid_value() { fn display_control_without_valid_value() {
let error = ScriptError { let error = ScriptError::ControlWithoutValidValue(InstructionMetaInfo::new());
info: ErrorInfo::ControlWithoutValidValue(InstructionMetaInfo::new()),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_invalid_control_location() { fn display_invalid_control_location() {
let error = ScriptError { let error = ScriptError::InvalidControlLocation(InstructionMetaInfo::new());
info: ErrorInfo::InvalidControlLocation(InstructionMetaInfo::new()),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_missing_end_quotes() { fn display_missing_end_quotes() {
let error = ScriptError { let error = ScriptError::MissingEndQuotes(InstructionMetaInfo::new());
info: ErrorInfo::MissingEndQuotes(InstructionMetaInfo::new()),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_missing_output_variable_name() { fn display_missing_output_variable_name() {
let error = ScriptError { let error = ScriptError::MissingOutputVariableName(InstructionMetaInfo::new());
info: ErrorInfo::MissingOutputVariableName(InstructionMetaInfo::new()),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_invalid_equals_location() { fn display_invalid_equals_location() {
let error = ScriptError { let error = ScriptError::InvalidEqualsLocation(InstructionMetaInfo::new());
info: ErrorInfo::InvalidEqualsLocation(InstructionMetaInfo::new()),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_invalid_quotes_location() { fn display_invalid_quotes_location() {
let error = ScriptError { let error = ScriptError::InvalidQuotesLocation(InstructionMetaInfo::new());
info: ErrorInfo::InvalidQuotesLocation(InstructionMetaInfo::new()),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_empty_label() { fn display_empty_label() {
let error = ScriptError { let error = ScriptError::EmptyLabel(InstructionMetaInfo::new());
info: ErrorInfo::EmptyLabel(InstructionMetaInfo::new()),
};
println!("{}", error); println!("{}", error);
} }
#[test] #[test]
fn display_unknown_pre_processor_command() { fn display_unknown_pre_processor_command() {
let error = ScriptError { let error = ScriptError::UnknownPreProcessorCommand(InstructionMetaInfo::new());
info: ErrorInfo::UnknownPreProcessorCommand(InstructionMetaInfo::new()),
};
println!("{}", error); println!("{}", error);
} }

View file

@ -1,6 +1,6 @@
[package] [package]
name = "duckscript_cli" name = "duckscript_cli"
version = "0.7.4" version = "0.8.0"
authors = ["Sagie Gur-Ari <sagiegurari@gmail.com>"] authors = ["Sagie Gur-Ari <sagiegurari@gmail.com>"]
description = "The duckscript command line executable." description = "The duckscript command line executable."
license = "Apache-2.0" license = "Apache-2.0"
@ -27,8 +27,8 @@ name = "duck"
path = "src/main.rs" path = "src/main.rs"
[dependencies] [dependencies]
duckscript = { version = "^0.6.0", path = "../duckscript" } duckscript = { version = "^0.7", path = "../duckscript" }
duckscriptsdk = { version = "^0.7.0", path = "../duckscript_sdk" } duckscriptsdk = { version = "^0.8", path = "../duckscript_sdk" }
[badges.codecov] [badges.codecov]
branch = "master" branch = "master"

View file

@ -1,5 +1,5 @@
use duckscript::parser; use duckscript::parser;
use duckscript::types::error::{ErrorInfo, ScriptError}; use duckscript::types::error::ScriptError;
use duckscript::types::instruction::{Instruction, InstructionType, ScriptInstruction}; use duckscript::types::instruction::{Instruction, InstructionType, ScriptInstruction};
pub(crate) fn lint_file(file: &str) -> Result<(), ScriptError> { pub(crate) fn lint_file(file: &str) -> Result<(), ScriptError> {
@ -28,9 +28,10 @@ fn lint_instructions(instructions: Vec<Instruction>) -> Result<(), ScriptError>
match result { match result {
Err(error) => { Err(error) => {
return Err(ScriptError { return Err(ScriptError::Runtime(
info: ErrorInfo::Runtime(error, Some(instruction.meta_info.clone())), error,
}) Some(instruction.meta_info.clone()),
))
} }
_ => (), _ => (),
} }

View file

@ -1,6 +1,6 @@
[package] [package]
name = "duckscriptsdk" name = "duckscriptsdk"
version = "0.7.4" version = "0.8.0"
authors = ["Sagie Gur-Ari <sagiegurari@gmail.com>"] authors = ["Sagie Gur-Ari <sagiegurari@gmail.com>"]
description = "The duckscript SDK." description = "The duckscript SDK."
license = "Apache-2.0" license = "Apache-2.0"
@ -26,9 +26,9 @@ include = [
attohttpc = "^0.16" attohttpc = "^0.16"
base64 = "^0.13" base64 = "^0.13"
cfg-if = "^1.0" cfg-if = "^1.0"
duckscript = { version = "^0.6.0", path = "../duckscript" } duckscript = { version = "^0.7", path = "../duckscript" }
fs_extra = "^1" fs_extra = "^1"
fsio = { version = "^0.1.3", features = ["temp-path"] } fsio = { version = "^0.2", features = ["temp-path"] }
ftp = "^3" ftp = "^3"
glob = "^0.3" glob = "^0.3"
home = "^0.5" home = "^0.5"

View file

@ -1,4 +1,4 @@
use duckscript::types::error::{ErrorInfo, ScriptError}; use duckscript::types::error::ScriptError;
use fsio; use fsio;
use fsio::file::{append_file, ensure_exists, read_file, write_file}; use fsio::file::{append_file, ensure_exists, read_file, write_file};
@ -9,18 +9,14 @@ mod io_test;
pub(crate) fn read_text_file(file: &str) -> Result<String, ScriptError> { pub(crate) fn read_text_file(file: &str) -> Result<String, ScriptError> {
match fsio::file::read_text_file(file) { match fsio::file::read_text_file(file) {
Ok(content) => Ok(content), Ok(content) => Ok(content),
Err(error) => Err(ScriptError { Err(error) => Err(ScriptError::ErrorReadingFile(file.to_string(), Some(error))),
info: ErrorInfo::ErrorReadingFile(file.to_string(), Some(error)),
}),
} }
} }
pub(crate) fn read_raw_file(file: &str) -> Result<Vec<u8>, ScriptError> { pub(crate) fn read_raw_file(file: &str) -> Result<Vec<u8>, ScriptError> {
match read_file(file) { match read_file(file) {
Ok(content) => Ok(content), Ok(content) => Ok(content),
Err(error) => Err(ScriptError { Err(error) => Err(ScriptError::ErrorReadingFile(file.to_string(), Some(error))),
info: ErrorInfo::ErrorReadingFile(file.to_string(), Some(error)),
}),
} }
} }
@ -41,9 +37,7 @@ pub(crate) fn write_to_file(file: &str, data: &[u8], append: bool) -> Result<(),
match result { match result {
Ok(content) => Ok(content), Ok(content) => Ok(content),
Err(error) => Err(ScriptError { Err(error) => Err(ScriptError::Runtime(error.to_string(), None)),
info: ErrorInfo::Runtime(error.to_string(), None),
}),
} }
} }