Skip to content

Commit

Permalink
feat: parse all steps with generic parser (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-m committed May 4, 2024
1 parent 358f8ec commit e14dfb0
Show file tree
Hide file tree
Showing 213 changed files with 1,517 additions and 1,550 deletions.
82 changes: 0 additions & 82 deletions src/script_steps/comment.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/script_steps/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub enum ScriptStep {
CommitRecordRequests = 75,
SetField = 76,
FreezeWindow = 79,
RefreshWindow = 80,
NewFile = 82,
SetMultiUser = 84,
AllowUserAbort = 85,
Expand Down
2 changes: 0 additions & 2 deletions src/script_steps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub(crate) mod constants;
pub(crate) mod sanitizer;

mod close_window;
mod comment;
mod exit_script;
mod go_to_field;
mod go_to_layout;
Expand All @@ -17,7 +16,6 @@ mod omit_multiple_records;
mod parameters;
mod perform_find;
mod perform_script;
mod primitive;
mod refresh_object;
mod replace_field_contents;
mod sanitize;
Expand Down
44 changes: 15 additions & 29 deletions src/script_steps/parameters/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use quick_xml::events::{BytesStart, Event};
use quick_xml::Reader;

use crate::script_steps::constants::{id_to_script_step, ScriptStep};
use crate::script_steps::parameters::constants::CommitRecordRequestsOptions;
use crate::script_steps::parameters::constants::{
CommitRecordRequestsOptions, RefreshWindowOptions,
};
use crate::utils::attributes::get_attributes;

#[derive(Debug, Default)]
Expand Down Expand Up @@ -67,40 +69,24 @@ impl Boolean {
Ok(item)
}

pub fn should_drop(&self) -> bool {
let step_id = id_to_script_step(&self.step_id);
let param_id = self.id.unwrap_or(0);
let value = self.value.unwrap_or(false);

matches!(
(step_id, param_id, value),
(
ScriptStep::CommitRecordRequests,
CommitRecordRequestsOptions::SKIP_DATA_ENTRY_VALIDATION,
false,
) | (
ScriptStep::CommitRecordRequests,
CommitRecordRequestsOptions::OVERRIDE_ESS_LOCKING_CONFLICTS,
false,
)
)
}

pub fn should_hide_bool(&self) -> bool {
let step_id = id_to_script_step(&self.step_id);
let param_id = self.id.unwrap_or(0);
let value = self.value.unwrap_or(false);

matches!(
(step_id, param_id, value),
(step_id, param_id),
(
ScriptStep::CommitRecordRequests,
CommitRecordRequestsOptions::SKIP_DATA_ENTRY_VALIDATION,
true,
) | (
ScriptStep::CommitRecordRequests,
CommitRecordRequestsOptions::OVERRIDE_ESS_LOCKING_CONFLICTS,
true,
) | (
ScriptStep::RefreshWindow,
RefreshWindowOptions::FLUSH_CACHED_JOIN_RESULTS,
) | (
ScriptStep::RefreshWindow,
RefreshWindowOptions::FLUSH_CACHED_EXTERNAL_DATA,
)
)
}
Expand All @@ -113,12 +99,12 @@ impl Boolean {
}

pub fn display(&self) -> Option<String> {
if Self::should_drop(self) {
return None;
}

if Self::should_hide_bool(self) {
return self.name.clone();
if !self.value.unwrap_or(false) {
return None;
} else {
return self.name.clone();
}
}

match &self.name {
Expand Down
38 changes: 38 additions & 0 deletions src/script_steps/parameters/comment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use quick_xml::escape::unescape;
use quick_xml::events::{BytesStart, Event};
use quick_xml::Reader;

use crate::utils::attributes::get_attribute;

#[derive(Debug, Default)]
pub struct Comment {}

impl Comment {
pub fn from_xml(reader: &mut Reader<&[u8]>, _: &BytesStart) -> Result<String, String> {
let mut depth = 1;
let mut comment = String::new();
let mut buf: Vec<u8> = Vec::new();
loop {
match reader.read_event_into(&mut buf) {
Err(_) => continue,
Ok(Event::Start(e)) => {
depth += 1;

if e.name().as_ref() == b"Comment" {
comment = get_attribute(&e, "value").unwrap_or_default();
};
}
Ok(Event::End(_)) => {
depth -= 1;
if depth == 0 {
break;
}
}
_ => {}
}
buf.clear();
}

Ok(unescape(&comment).unwrap().to_string())
}
}
8 changes: 7 additions & 1 deletion src/script_steps/parameters/constants.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
pub struct CommitRecordRequestsOptions;

#[allow(dead_code)]
impl CommitRecordRequestsOptions {
pub const WITH_DIALOG: u32 = 128;
pub const SKIP_DATA_ENTRY_VALIDATION: u32 = 256;
pub const OVERRIDE_ESS_LOCKING_CONFLICTS: u32 = 512;
}

pub struct RefreshWindowOptions;
#[allow(dead_code)]
impl RefreshWindowOptions {
pub const FLUSH_CACHED_JOIN_RESULTS: u32 = 256;
pub const FLUSH_CACHED_EXTERNAL_DATA: u32 = 512;
}
3 changes: 3 additions & 0 deletions src/script_steps/parameters/field_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ impl FieldReference {
Ok(Event::Start(e)) => {
depth += 1;
match e.name().as_ref() {
b"FieldReference" => {
item.field_reference = get_attribute(&e, "name");
}
b"TableOccurrenceReference" => {
for attr in get_attributes(&e).unwrap() {
if attr.0 == "name" {
Expand Down
10 changes: 9 additions & 1 deletion src/script_steps/parameters/layout_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ impl LayoutReferenceContainer {
pub fn from_xml(reader: &mut Reader<&[u8]>, e: &BytesStart) -> Result<Self, String> {
let mut depth = 1;
let mut item = LayoutReferenceContainer {
reference_type: get_attribute(e, "value").unwrap().to_string(),
reference_type: get_attribute(e, "value")
.unwrap_or("".to_string())
.to_string(),
..Default::default()
};

Expand All @@ -46,6 +48,12 @@ impl LayoutReferenceContainer {
Ok(Event::Start(e)) => {
depth += 1;
match e.name().as_ref() {
b"LayoutReferenceContainer" => {
item.reference_type = get_attribute(&e, "value")
.unwrap_or("".to_string())
.to_string();
break;
}
b"LayoutReference" => {
item.layout_reference = get_attribute(&e, "name").unwrap().to_string();
break;
Expand Down
1 change: 1 addition & 0 deletions src/script_steps/parameters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub(crate) mod boolean;
pub(crate) mod calculation;
pub(crate) mod comment;
pub(crate) mod constants;
pub(crate) mod field_reference;
pub(crate) mod layout_reference;
Expand Down
43 changes: 41 additions & 2 deletions src/script_steps/parameters/parameter_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use quick_xml::Reader;
use crate::script_steps::constants::{id_to_script_step, ScriptStep};
use crate::script_steps::parameters::boolean::Boolean;
use crate::script_steps::parameters::calculation::Calculation;
use crate::script_steps::parameters::comment::Comment;
use crate::script_steps::parameters::field_reference::FieldReference;
use crate::script_steps::parameters::layout_reference::LayoutReferenceContainer;
use crate::script_steps::parameters::list::List;
use crate::script_steps::parameters::target::Target;
use crate::utils::attributes::get_attribute;
Expand Down Expand Up @@ -33,7 +36,12 @@ impl ParameterValues {
Ok(Event::Start(e)) => {
depth += 1;
if let b"Parameter" = e.name().as_ref() {
let parameter_type = get_attribute(&e, "type").unwrap();
let parameter_type = get_attribute(&e, "type");
if parameter_type.is_none() {
continue;
}

let parameter_type = parameter_type.unwrap();
match parameter_type.as_str() {
"Boolean" => {
if let Ok(param_value) = Boolean::from_xml(reader, &e, step_id) {
Expand Down Expand Up @@ -63,6 +71,12 @@ impl ParameterValues {
}
depth -= 1;
}
"Calculation" => {
if let Ok(param_value) = Calculation::from_xml(reader, &e) {
item.parameters.push(param_value);
}
depth -= 1;
}
"Condition" | "ErrorCode" | "ErrorMessage" | "CustomDebugInfo" => {
if let Ok(param_value) = Calculation::from_xml(reader, &e) {
item.parameters.push(format!(
Expand All @@ -73,8 +87,33 @@ impl ParameterValues {
}
depth -= 1;
}

"LayoutReferenceContainer" => {
if let Ok(param_value) =
LayoutReferenceContainer::from_xml(reader, &e)
{
if let Some(display) = param_value.display() {
item.parameters.push(display);
}
}
}
"FieldReference" => {
if let Ok(param_value) = FieldReference::from_xml(reader, &e) {
if let Some(display) = param_value.display() {
item.parameters.push(display);
}
}
}
"Comment" => {
if let Ok(param_value) = Comment::from_xml(reader, &e) {
item.parameters.push(param_value);
}
depth -= 1;
}
_ => {
item.parameters.push(format!(
r#"⚠️ PARAMETER "{}" NOT PARSED ⚠️"#,
parameter_type
));
eprintln!(
"Unknown parameter \"{}\" in step \"{}\"",
parameter_type,
Expand Down
41 changes: 0 additions & 41 deletions src/script_steps/primitive.rs

This file was deleted.

0 comments on commit e14dfb0

Please sign in to comment.