Rename compute->list_modified_fields, and slight refactor

This commit is contained in:
Sosthene 2024-11-29 08:43:43 +01:00 committed by Nicolas Cantu
parent 390b8d176f
commit b205e7ecda

View File

@ -31,26 +31,26 @@ impl ProcessState {
Value::Object(fields2plain) Value::Object(fields2plain)
} }
fn compute_modified_fields(&self, previous_state: Option<&ProcessState>) -> Vec<String> { fn list_modified_fields(&self, previous_state: Option<&ProcessState>) -> Vec<String> {
let new_state = &self.pcd_commitment; let new_state = &self.pcd_commitment;
// Ensure the new state is a JSON object // Ensure the new state is a JSON object
let new_state_obj = new_state let new_state_commitments = new_state
.as_object() .as_object()
.expect("New state should be a JSON object"); .expect("New state should be a JSON object");
if let Some(prev_state) = previous_state { if let Some(prev_state) = previous_state {
// Previous state exists; compute differences // Previous state exists; compute differences
let previous_state_obj = prev_state let previous_state_commitments = prev_state
.pcd_commitment .pcd_commitment
.as_object() .as_object()
.expect("Previous state should be a JSON object"); .expect("Previous state should be a JSON object");
// Compute modified fields by comparing with previous state // Compute modified fields by comparing with previous state
new_state_obj new_state_commitments
.iter() .iter()
.filter_map(|(key, value)| { .filter_map(|(key, value)| {
let previous_value = previous_state_obj.get(key); let previous_value = previous_state_commitments.get(key);
if previous_value.is_none() || value != previous_value.unwrap() { if previous_value.is_none() || value != previous_value.unwrap() {
Some(key.clone()) Some(key.clone())
} else { } else {
@ -60,7 +60,7 @@ impl ProcessState {
.collect() .collect()
} else { } else {
// No previous state; all fields are considered modified // No previous state; all fields are considered modified
new_state_obj.keys().cloned().collect() new_state_commitments.keys().cloned().collect()
} }
} }
@ -72,7 +72,7 @@ impl ProcessState {
} }
// Compute modified fields // Compute modified fields
let modified_fields = self.compute_modified_fields(previous_state); let modified_fields = self.list_modified_fields(previous_state);
if modified_fields.is_empty() { if modified_fields.is_empty() {
return Err(anyhow::anyhow!("State is identical to the previous state")); return Err(anyhow::anyhow!("State is identical to the previous state"));