Skip to content

Commit

Permalink
feat: merge parse commit with generic parser (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
bc-m committed May 4, 2024
1 parent 1fbb96e commit 5ae972e
Show file tree
Hide file tree
Showing 14 changed files with 234 additions and 279 deletions.
193 changes: 0 additions & 193 deletions src/script_steps/commit.rs

This file was deleted.

11 changes: 3 additions & 8 deletions src/script_steps/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,10 @@ pub enum ScriptStep {
RevertTransaction = 207,
}

pub fn id_to_script_step(id: &str) -> ScriptStep {
if id.is_empty() {
return ScriptStep::Unknown;
}

let id = id.parse::<u32>().unwrap();
if UNKNOWN_SCRIPT_STEP.contains(&id) {
pub fn id_to_script_step(id: &u32) -> ScriptStep {
if UNKNOWN_SCRIPT_STEP.contains(id) {
ScriptStep::Unknown
} else {
ScriptStep::from_repr(id).unwrap_or(ScriptStep::Unknown)
ScriptStep::from_repr(*id).unwrap_or(ScriptStep::Unknown)
}
}
1 change: 0 additions & 1 deletion src/script_steps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub(crate) mod sanitizer;

mod close_window;
mod comment;
mod commit;
mod exit_script;
mod go_to_field;
mod go_to_layout;
Expand Down
103 changes: 76 additions & 27 deletions src/script_steps/parameters/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
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::utils::attributes::get_attributes;

#[derive(Debug, Default)]
pub struct Boolean {
pub step_id: u32,
pub id: Option<u32>,
pub name: Option<String>,
pub value: Option<bool>,
}
Expand All @@ -13,10 +17,12 @@ impl Boolean {
pub fn from_xml(
reader: &mut Reader<&[u8]>,
_e: &BytesStart,
_step_id: &str,
_step_id: &u32,
) -> Result<Boolean, String> {
let mut depth = 1;
let mut item = Boolean {
step_id: *_step_id,
id: None,
name: None,
value: None,
};
Expand All @@ -31,6 +37,11 @@ impl Boolean {
if let b"Boolean" = e.name().as_ref() {
for attr in get_attributes(&e).unwrap() {
match attr.0.as_str() {
"id" => {
if let Ok(id) = attr.1.parse::<u32>() {
item.id = Some(id);
}
}
"type" => item.name = Some(attr.1),
"value" => match attr.1.as_str() {
"True" => item.value = Some(true),
Expand All @@ -56,32 +67,67 @@ impl Boolean {
Ok(item)
}

pub fn display(&self) -> Option<String> {
let name = match &self.name {
Some(name) => name,
None => {
return match self.value {
None => None,
Some(bool) => {
return Some(match bool {
true => "ON".to_string(),
false => "OFF".to_string(),
});
}
};
}
};
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);

self.value.map(|bool| {
format!(
"{}: {}",
name,
match bool {
true => "ON",
false => "OFF",
}
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),
(
ScriptStep::CommitRecordRequests,
CommitRecordRequestsOptions::SKIP_DATA_ENTRY_VALIDATION,
true,
) | (
ScriptStep::CommitRecordRequests,
CommitRecordRequestsOptions::OVERRIDE_ESS_LOCKING_CONFLICTS,
true,
)
)
}

pub fn bool_to_string(bool: bool) -> String {
match bool {
true => "ON".to_string(),
false => "OFF".to_string(),
}
}

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

if Self::should_hide_bool(self) {
return self.name.clone();
}

match &self.name {
Some(name) => self.value.map(|bool_value| {
let formatted_string = format!("{}: {}", name, Self::bool_to_string(bool_value));
formatted_string
}),
None => self.value.map(Self::bool_to_string),
}
}
}

Expand All @@ -107,8 +153,10 @@ mod tests {
};

let expected_output = "Pause: OFF".to_string();
let script_id: u32 = 0;

assert_eq!(
Boolean::from_xml(&mut reader, &element, "")
Boolean::from_xml(&mut reader, &element, &script_id)
.unwrap()
.display()
.unwrap(),
Expand All @@ -131,8 +179,9 @@ mod tests {
};

let expected_output = "OFF".to_string();
let script_id: u32 = 0;
assert_eq!(
Boolean::from_xml(&mut reader, &element, "")
Boolean::from_xml(&mut reader, &element, &script_id)
.unwrap()
.display()
.unwrap(),
Expand Down
8 changes: 8 additions & 0 deletions src/script_steps/parameters/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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;
}

0 comments on commit 5ae972e

Please sign in to comment.