diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c605e7234..4fc42c5c86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -126,6 +126,8 @@ segments.write_arg(ids.blake2s_ptr_end, padding) %} +* Add `Program::iter_identifiers(&self) -> Iterator` to get an iterator over the program's identifiers [#1079](https://github.com/lambdaclass/cairo-rs/pull/1079) + * Implement hint on `assert_le_felt` for versions 0.6.0 and 0.8.2 [#1047](https://github.com/lambdaclass/cairo-rs/pull/1047): `BuiltinHintProcessor` now supports the following hints: diff --git a/src/types/program.rs b/src/types/program.rs index 3683bb3cfe..3d29aeaa60 100644 --- a/src/types/program.rs +++ b/src/types/program.rs @@ -124,6 +124,13 @@ impl Program { pub fn get_identifier(&self, id: &str) -> Option<&Identifier> { self.shared_program_data.identifiers.get(id) } + + pub fn iter_identifiers(&self) -> impl Iterator { + self.shared_program_data + .identifiers + .iter() + .map(|(cairo_type, identifier)| (cairo_type.as_str(), identifier)) + } } impl Default for Program { @@ -429,6 +436,70 @@ mod tests { ); } + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn iter_identifiers() { + let reference_manager = ReferenceManager { + references: Vec::new(), + }; + + let builtins: Vec = Vec::new(); + + let data: Vec = vec![ + mayberelocatable!(5189976364521848832), + mayberelocatable!(1000), + mayberelocatable!(5189976364521848832), + mayberelocatable!(2000), + mayberelocatable!(5201798304953696256), + mayberelocatable!(2345108766317314046), + ]; + + let mut identifiers: HashMap = HashMap::new(); + + identifiers.insert( + String::from("__main__.main"), + Identifier { + pc: Some(0), + type_: Some(String::from("function")), + value: None, + full_name: None, + members: None, + cairo_type: None, + }, + ); + + identifiers.insert( + String::from("__main__.main.SIZEOF_LOCALS"), + Identifier { + pc: None, + type_: Some(String::from("const")), + value: Some(Felt252::zero()), + full_name: None, + members: None, + cairo_type: None, + }, + ); + + let program = Program::new( + builtins, + data, + None, + HashMap::new(), + reference_manager, + identifiers.clone(), + Vec::new(), + None, + ) + .unwrap(); + + let collected_identifiers: HashMap<_, _> = program + .iter_identifiers() + .map(|(cairo_type, identifier)| (cairo_type.to_string(), identifier.clone())) + .collect(); + + assert_eq!(collected_identifiers, identifiers); + } + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn new_program_with_invalid_identifiers() {