• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ergoplatform / sigma-rust / 19909456309

03 Dec 2025 09:29PM UTC coverage: 86.947% (+8.5%) from 78.463%
19909456309

Pull #838

github

web-flow
Merge 717ebc4b7 into 2f840d387
Pull Request #838: Fix CI, bump dependencies and rust toolchain

20 of 24 new or added lines in 12 files covered. (83.33%)

1614 existing lines in 221 files now uncovered.

27478 of 31603 relevant lines covered (86.95%)

506307.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.67
/ergo-chain-types/src/json/autolykos_solution.rs
1
//! Code to implement `AutolykosSolution` JSON encoding
2

3
use alloc::string::String;
4
use alloc::string::ToString;
5
use alloc::vec::Vec;
6
use core::str::FromStr;
7
use num_bigint::BigUint;
8

9
use num_traits::FromPrimitive;
10
use serde::{Deserialize, Deserializer};
11

12
pub(crate) fn as_base16_string<S>(value: &[u8], serializer: S) -> Result<S::Ok, S::Error>
44✔
13
where
44✔
14
    S: serde::Serializer,
44✔
15
{
16
    serializer.serialize_str(&base16::encode_lower(value))
44✔
17
}
44✔
18

19
pub(crate) fn from_base16_string<'de, D, T: From<Vec<u8>>>(deserializer: D) -> Result<T, D::Error>
644✔
20
where
644✔
21
    D: Deserializer<'de>,
644✔
22
{
23
    use serde::de::Error;
24
    String::deserialize(deserializer)
644✔
25
        .and_then(|string| base16::decode(&string).map_err(|err| Error::custom(err.to_string())))
644✔
26
        .map(From::from)
644✔
27
}
644✔
28

29
/// Serialize `BigInt` as a string
30
pub(crate) fn bigint_as_str<S>(value: &Option<BigUint>, serializer: S) -> Result<S::Ok, S::Error>
22✔
31
where
22✔
32
    S: serde::Serializer,
22✔
33
{
34
    if let Some(value) = value {
22✔
35
        serializer.serialize_str(&value.to_string())
22✔
36
    } else {
37
        serializer.serialize_unit()
×
38
    }
39
}
22✔
40

41
/// Deserialize a `BigInt` instance from either a String or from a `serde_json::Number` value.  We
42
/// need to do this because the JSON specification allows for arbitrarily-large numbers, a feature
43
/// that Autolykos makes use of to encode the PoW-distance (d) parameter. Note that we also need to
44
/// use `serde_json` with the `arbitrary_precision` feature for this to work.
45
pub(crate) fn bigint_from_serde_json_number<'de, D>(
326✔
46
    deserializer: D,
326✔
47
) -> Result<Option<BigUint>, D::Error>
326✔
48
where
326✔
49
    D: Deserializer<'de>,
326✔
50
{
51
    use serde::de::Error;
52

53
    match DeserializeBigIntFrom::deserialize(deserializer) {
326✔
54
        Ok(s) => match s {
326✔
55
            DeserializeBigIntFrom::String(s) => BigUint::from_str(&s)
24✔
56
                .map(Some)
24✔
57
                .map_err(|e| Error::custom(e.to_string())),
24✔
58
            DeserializeBigIntFrom::SerdeJsonNumber(n) => {
302✔
59
                let bigint = if n.is_f64() {
302✔
60
                    let n_f64 = n
2✔
61
                        .as_f64()
2✔
62
                        .ok_or_else(|| Error::custom("failed to convert JSON number to f64"))?;
2✔
63

64
                    BigUint::from_f64(n_f64).ok_or_else(|| {
2✔
65
                        Error::custom("failed to create BigInt from f64".to_string())
×
UNCOV
66
                    })
×
67
                } else {
68
                    BigUint::from_str(&n.to_string()).map_err(|e| Error::custom(e.to_string()))
300✔
69
                };
70

71
                bigint.map(Some)
302✔
72
            }
73
        },
74
        Err(e) => Err(Error::custom(e.to_string())),
×
75
    }
76
}
326✔
77

78
#[derive(Deserialize)]
79
#[serde(untagged)]
80
enum DeserializeBigIntFrom {
81
    String(String),
82
    SerdeJsonNumber(serde_json::Number),
83
}
84

85
#[allow(clippy::unwrap_used)]
86
#[cfg(test)]
87
mod tests {
88
    use serde::de::IntoDeserializer;
89
    use serde_json::Value;
90

91
    use super::bigint_from_serde_json_number;
92

93
    #[test]
94
    fn test_scientific_notion_deser() {
2✔
95
        let pow_d_parameter = r#"4.69094608138843e64"#;
2✔
96
        let j: Value = serde_json::from_str(pow_d_parameter).unwrap();
2✔
97
        let result = bigint_from_serde_json_number(j.into_deserializer()).unwrap();
2✔
98

99
        assert_eq!(
2✔
100
            result.unwrap().to_string(),
2✔
101
            "46909460813884301641411510982628556119846083366464832536248844288"
102
        )
103
    }
2✔
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc