Skip to content

Commit

Permalink
fix: parse long strings
Browse files Browse the repository at this point in the history
  • Loading branch information
remiroyc committed Sep 13, 2023
1 parent a75c3a0 commit 38e4998
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions crates/ark-metadata/src/cairo_strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,20 @@ pub fn parse_cairo_long_string(long_string: Vec<FieldElement>) -> Result<String>
None => return Err(anyhow!("No value found")),

Check warning on line 20 in crates/ark-metadata/src/cairo_strings.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> crates/ark-metadata/src/cairo_strings.rs:20:21 | 20 | None => return Err(anyhow!("No value found")), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 20 | None => Err(anyhow!("No value found")), | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
},
_ => {
let first_element = long_string.first().unwrap();
let max_array_length = FieldElement::from_dec_str("255").unwrap();

if first_element <= &max_array_length {
// TODO

// // Take the first_element items from the long_string
// let mut short_string = Vec::new();
// for i in 1..(first_element.add(FieldElement::ONE)) {
// short_string.push(long_string[i as usize].clone());
// }

return Ok("".to_string());
} else {
let mut result = String::new();
for field_element in long_string {
match parse_cairo_short_string(&field_element) {
Ok(value) => {
result.push_str(&value);
}
Err(_) => {
return Err(anyhow!("Error parsing short string"));
}
// let array_length_field_element = long_string.first().unwrap();
let mut result = String::new();
for i in 1..long_string.len() {
let field_element = long_string[i as usize].clone();

Check warning on line 26 in crates/ark-metadata/src/cairo_strings.rs

View workflow job for this annotation

GitHub Actions / clippy

casting to the same type is unnecessary (`usize` -> `usize`)

warning: casting to the same type is unnecessary (`usize` -> `usize`) --> crates/ark-metadata/src/cairo_strings.rs:26:49 | 26 | let field_element = long_string[i as usize].clone(); | ^^^^^^^^^^ help: try: `i` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast = note: `#[warn(clippy::unnecessary_cast)]` on by default

Check warning on line 26 in crates/ark-metadata/src/cairo_strings.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `FieldElement` which implements the `Copy` trait

warning: using `clone` on type `FieldElement` which implements the `Copy` trait --> crates/ark-metadata/src/cairo_strings.rs:26:37 | 26 | let field_element = long_string[i as usize].clone(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `long_string[i as usize]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy = note: `#[warn(clippy::clone_on_copy)]` on by default
match parse_cairo_short_string(&field_element) {
Ok(value) => {
result.push_str(&value);
}
Err(_) => {
return Err(anyhow!("Error parsing short string"));
}
}
return Ok(result);
}
Ok(result)
}
}
}
Expand All @@ -57,8 +44,9 @@ mod tests {
use starknet::core::types::FieldElement;

#[test]
fn should_parse_field_elements_to_url_without_considering_array_length() {
fn should_parse_field_elements_with_array_length() {
let long_string = vec![
FieldElement::from_hex_be("0x4").unwrap(),
FieldElement::from_hex_be(
"0x68747470733a2f2f6170692e627269712e636f6e737472756374696f6e",
)
Expand All @@ -70,14 +58,16 @@ mod tests {

let result = parse_cairo_long_string(long_string);
assert!(result.is_ok());
assert!(
result.unwrap() == "https://api.briq.construction/v1/uri/set/starknet-mainnet/.json"
);

let value = result.unwrap();
println!("Value: {}", value);
assert!(value == "https://api.briq.construction/v1/uri/set/starknet-mainnet/.json");
}

#[test]
fn should_parse_one_field_element_per_character_to_url() {
let long_string = vec![
FieldElement::from_hex_be("0x2d").unwrap(),
FieldElement::from_hex_be("0x68").unwrap(),
FieldElement::from_hex_be("0x74").unwrap(),
FieldElement::from_hex_be("0x74").unwrap(),
Expand Down

0 comments on commit 38e4998

Please sign in to comment.