layoutcss_parser/
harmonic.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
pub fn get_harmonic(value: &str, harmonic: f64) -> String {
    // if its a css variable
    if value.starts_with("--") {
        return format!("var({})", value);
    }

    if value == "none" {
        return "0.0".to_string();
    }

    if let Ok(x) = value.parse::<f64>() {
        let value =  harmonic.powf(x).to_string();
        return format!("{value:.5}rem").to_string();
    };

    // if the value is not a unit less number
    // so not a harmonic number
    // we return it as it is
    value.to_string()
}

#[cfg(test)]
mod tests {
    use super::*; // Bring the function into scope for the tests

    // Use 1.618 as the harmonic value for all tests
    const HARMONIC: f64 = 1.618;

    #[test]
    fn test_numeric_values() {
        // Test harmonic calculation for numeric values with harmonic = 1.618
        assert_eq!(get_harmonic("2", HARMONIC), "2.617rem"); // 1.618^2
        assert_eq!(get_harmonic("2.5", HARMONIC), "3.330rem"); // 1.618^2.5
    }

    #[test]
    fn test_none_value() {
        // Test for the "none" value, which should return "0.0"
        assert_eq!(get_harmonic("none", HARMONIC), "0.0");
    }

    #[test]
    fn test_css_variable() {
        // Test for CSS variable values, they should be returned wrapped in var()
        assert_eq!(get_harmonic("--main-color", HARMONIC), "var(--main-color)");
        assert_eq!(get_harmonic("--font-size", HARMONIC), "var(--font-size)");
    }

    #[test]
    fn test_non_numeric_values() {
        // Test for non-numeric values (with units or other characters)
        assert_eq!(get_harmonic("16px", HARMONIC), "16px");
        assert_eq!(get_harmonic("1rem", HARMONIC), "1rem");
        assert_eq!(get_harmonic("invalid", HARMONIC), "invalid");
    }

    #[test]
    fn test_empty_string() {
        // Test for empty string, should return an empty string as is
        assert_eq!(get_harmonic("", HARMONIC), "");
    }
}