Skip to content

Commit

Permalink
reimplement rules_config in read_config_files()
Browse files Browse the repository at this point in the history
  • Loading branch information
lmcmicu committed Feb 1, 2024
1 parent 2ab7a75 commit 0f0f139
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 59 deletions.
98 changes: 49 additions & 49 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use crate::{
},
valve::{
ValveColumnConfig, ValveDatatypeConfig, ValveError, ValveForeignConstraint, ValveRow,
ValveSpecialConfig, ValveTableConfig, ValveTableConstraints, ValveTreeConstraint,
ValveUnderConstraint,
ValveRuleConfig, ValveSpecialConfig, ValveTableConfig, ValveTableConstraints,
ValveTreeConstraint, ValveUnderConstraint,
},
valve_grammar::StartParser,
};
Expand Down Expand Up @@ -218,7 +218,7 @@ pub fn read_config_files(
ValveSpecialConfig,
HashMap<String, ValveTableConfig>,
HashMap<String, ValveDatatypeConfig>,
SerdeMap,
HashMap<String, HashMap<String, Vec<ValveRuleConfig>>>,
ValveTableConstraints,
Vec<String>,
HashMap<String, Vec<String>>,
Expand Down Expand Up @@ -408,42 +408,33 @@ pub fn read_config_files(
}

let dt_name = row.get("datatype").and_then(|d| d.as_str()).unwrap();
let html_type = row.get("HTML type").and_then(|s| s.as_str()).unwrap();
let sql_type = row
.get("SQL type")
.and_then(|s| s.as_str())
.and_then(|s| Some(s.to_string()));
let condition = row
.get("condition")
.and_then(|s| s.as_str())
.and_then(|s| Some(s.to_string()));
let description = row.get("description").and_then(|s| s.as_str()).unwrap();
let parent = row
.get("parent")
.and_then(|s| s.as_str())
.and_then(|s| Some(s.to_string()));
let structure = row.get("structure").and_then(|s| s.as_str()).unwrap();
let transform = row.get("transform").and_then(|s| s.as_str()).unwrap();
datatypes_config.insert(
dt_name.to_string(),
ValveDatatypeConfig {
html_type: row
.get("HTML type")
.and_then(|s| s.as_str())
.unwrap()
.to_string(),
sql_type: row
.get("SQL type")
.and_then(|s| s.as_str())
.and_then(|s| Some(s.to_string())),
condition: row
.get("condition")
.and_then(|s| s.as_str())
.and_then(|s| Some(s.to_string())),
html_type: html_type.to_string(),
sql_type: sql_type,
condition: condition,
datatype: dt_name.to_string(),
description: row
.get("description")
.and_then(|s| s.as_str())
.unwrap()
.to_string(),
parent: row
.get("parent")
.and_then(|s| s.as_str())
.and_then(|s| Some(s.to_string())),
structure: row
.get("structure")
.and_then(|s| s.as_str())
.unwrap()
.to_string(),
transform: row
.get("transform")
.and_then(|s| s.as_str())
.unwrap()
.to_string(),
description: description.to_string(),
parent: parent,
structure: structure.to_string(),
transform: transform.to_string(),
},
);
}
Expand Down Expand Up @@ -531,7 +522,7 @@ pub fn read_config_files(
}

// Load rule table if it exists
let mut rules_config = SerdeMap::new();
let mut rules_config = HashMap::new();
if let Some(table_name) = &specials_config.rule {
let rows = get_special_config(table_name, &specials_config, &tables_config, path);
for row in rows {
Expand Down Expand Up @@ -559,23 +550,32 @@ pub fn read_config_files(

// Add the rule specified in the given row to the list of rules associated with the
// value of the when column:
let row_when_column = row.get("when column").and_then(|c| c.as_str()).unwrap();
if !rules_config.contains_key(row_table) {
rules_config.insert(String::from(row_table), SerdeValue::Object(SerdeMap::new()));
rules_config.insert(String::from(row_table), HashMap::new());
}

let table_rule_config = rules_config
.get_mut(row_table)
.and_then(|t| t.as_object_mut())
.unwrap();
if !table_rule_config.contains_key(row_when_column) {
table_rule_config.insert(String::from(row_when_column), SerdeValue::Array(vec![]));
let table_rule_config = rules_config.get_mut(row_table).unwrap();

let when_col = row.get("when column").and_then(|c| c.as_str()).unwrap();
if !table_rule_config.contains_key(when_col) {
table_rule_config.insert(String::from(when_col), vec![]);
}
let column_rule_config = table_rule_config
.get_mut(&row_when_column.to_string())
.and_then(|w| w.as_array_mut())
.unwrap();
column_rule_config.push(SerdeValue::Object(row));

let column_rule_config = table_rule_config.get_mut(&when_col.to_string()).unwrap();
let desc = row.get("description").and_then(|c| c.as_str()).unwrap();
let level = row.get("level").and_then(|c| c.as_str()).unwrap();
let when_con = row.get("when condition").and_then(|c| c.as_str()).unwrap();
let then_col = row.get("then column").and_then(|c| c.as_str()).unwrap();
let then_con = row.get("then condition").and_then(|c| c.as_str()).unwrap();
column_rule_config.push(ValveRuleConfig {
description: desc.to_string(),
level: level.to_string(),
table: row_table.to_string(),
then_column: then_col.to_string(),
then_condition: then_con.to_string(),
when_column: when_col.to_string(),
when_condition: when_con.to_string(),
});
}
}

Expand Down
22 changes: 12 additions & 10 deletions src/valve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,14 @@ pub struct ValveDatatypeConfig {

// TODO: Make this struct public; remove unneeded derives.
#[derive(Debug, Default)]
struct _ValveRuleConfig {
pub _description: String,
pub _level: String,
pub _table: String,
pub _then_column: String,
pub _then_condition: String,
pub _when_column: String,
pub _when_condition: String,
pub struct ValveRuleConfig {
pub description: String,
pub level: String,
pub table: String,
pub then_column: String,
pub then_condition: String,
pub when_column: String,
pub when_condition: String,
}

// TODO: Make this struct public; remove unneeded derives.
Expand Down Expand Up @@ -173,7 +173,7 @@ pub struct ValveConfig {
pub special: ValveSpecialConfig,
pub table: HashMap<String, ValveTableConfig>,
pub datatype: HashMap<String, ValveDatatypeConfig>,
//pub rule: HashMap<String, HashMap<String, Vec<ValveRuleConfig>>>,
pub rule: HashMap<String, HashMap<String, Vec<ValveRuleConfig>>>,
pub table_constraints: ValveTableConstraints,
//pub datatype_conditions: HashMap<String, CompiledCondition>,
//pub rule_conditions: HashMap<String, HashMap<String, Vec<ColumnRule>>>,
Expand Down Expand Up @@ -264,7 +264,7 @@ impl Valve {
specials_config,
tables_config,
datatypes_config,
_rules_config_old,
rules_config,
constraints_config,
sorted_table_list,
table_dependencies_in,
Expand All @@ -275,13 +275,15 @@ impl Valve {
special: specials_config,
table: tables_config,
datatype: datatypes_config,
rule: rules_config,
table_constraints: constraints_config,
sorted_table_list: sorted_table_list.clone(),
};
println!("SPECIALS CONFIG: {:#?}", config.special);
println!("TABLES CONFIG: {:#?}", config.table);
println!("DATATYPES CONFIG: {:#?}", config.datatype);
println!("TABLE CONSTRAINTS: {:#?}", config.table_constraints);
println!("RULES CONFIG: {:#?}", config.rule);
println!("SORTED TABLE LIST: {:?}", config.sorted_table_list);

// TODO: Obviously remove this later.
Expand Down

0 comments on commit 0f0f139

Please sign in to comment.