query_curve/
utils.rs

1use crate::base62::from_base62;
2use crate::constants::ENCODING_SCALE_FACTOR;
3use crate::types::ScaledBezierChain;
4use num_traits::ToPrimitive;
5use regex::Regex;
6
7pub fn decode(chain: &str) -> Result<ScaledBezierChain, String> {
8    let re = Regex::new(r"--?[0-9A-Za-z]+").unwrap();
9    let chain_with_leading_dash = format!("-{}", chain);
10    let matches: Vec<&str> = re
11        .find_iter(&chain_with_leading_dash)
12        .map(|mat| mat.as_str())
13        .collect();
14
15    let mut result = Vec::new();
16
17    for link in matches {
18        let is_negative = link.starts_with("--");
19        let number_str = link.trim_start_matches('-');
20        let transformed = from_base62(number_str)?;
21        let transformed_f64 = transformed
22            .to_f64()
23            .ok_or("Failed to convert BigUint to f64")?;
24        let value = if is_negative {
25            -transformed_f64
26        } else {
27            transformed_f64
28        } / ENCODING_SCALE_FACTOR;
29        result.push(value);
30    }
31
32    Ok(result)
33}