use crate::nodes::{AssignmentKind, Block, FunctionAssignment, VariableAssignment};
use crate::process::{DefaultVisitor, NodeProcessor, NodeVisitor};
use crate::rules::{
Context, FlawlessRule, RuleConfiguration, RuleConfigurationError, RuleMetadata, RuleProperties,
};
use serde::ser::{Serialize, Serializer};
use super::verify_no_rule_properties;
#[derive(Debug, Default)]
struct Processor;
impl NodeProcessor for Processor {
fn process_local_assign_statement(&mut self, assign: &mut VariableAssignment) {
assign.set_assignment_kind(AssignmentKind::Local);
}
fn process_local_function_statement(&mut self, function: &mut FunctionAssignment) {
function.set_assignment_kind(AssignmentKind::Local);
}
}
pub const MAKE_ASSIGNMENT_LOCAL_RULE_NAME: &str = "make_assignment_local";
#[derive(Debug, Default, PartialEq, Eq)]
pub struct MakeAssignmentLocal {
metadata: RuleMetadata,
}
impl FlawlessRule for MakeAssignmentLocal {
fn flawless_process(&self, block: &mut Block, _: &Context) {
let mut processor = Processor;
DefaultVisitor::visit_block(block, &mut processor);
}
}
impl RuleConfiguration for MakeAssignmentLocal {
fn configure(&mut self, properties: RuleProperties) -> Result<(), RuleConfigurationError> {
verify_no_rule_properties(&properties)?;
Ok(())
}
fn get_name(&self) -> &'static str {
MAKE_ASSIGNMENT_LOCAL_RULE_NAME
}
fn serialize_to_properties(&self) -> RuleProperties {
RuleProperties::new()
}
fn set_metadata(&mut self, metadata: RuleMetadata) {
self.metadata = metadata;
}
fn metadata(&self) -> &RuleMetadata {
&self.metadata
}
}
impl Serialize for MakeAssignmentLocal {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(MAKE_ASSIGNMENT_LOCAL_RULE_NAME)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::rules::Rule;
use insta::assert_json_snapshot;
fn new_rule() -> MakeAssignmentLocal {
MakeAssignmentLocal::default()
}
#[test]
fn serialize_default_rule() {
assert_json_snapshot!(new_rule(), @r###""make_assignment_local""###);
}
#[test]
fn configure_with_extra_field_error() {
let result = json5::from_str::<Box<dyn Rule>>(
r#"{
rule: 'make_assignment_local',
prop: "something",
}"#,
);
insta::assert_snapshot!(result.unwrap_err().to_string(), @"unexpected field 'prop' at line 1 column 1")
}
}