Skip to main content

ckb_app_config/configs/
notify.rs

1use serde::{Deserialize, Serialize};
2/// Notify config options.
3#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Default, Eq)]
4#[serde(deny_unknown_fields)]
5pub struct Config {
6    /// An executable script to be called whenever there's a new block in the canonical chain.
7    ///
8    /// The script is called with the block hash as the argument.
9    pub new_block_notify_script: Option<String>,
10    /// An executable script to be called whenever there's a new network alert received.
11    ///
12    /// The script is called with the alert message as the argument.
13    pub network_alert_notify_script: Option<String>,
14
15    /// Notify tx timeout in milliseconds
16    #[serde(default, deserialize_with = "at_least_100")]
17    pub notify_tx_timeout: Option<u64>,
18
19    /// Notify alert timeout in milliseconds
20    #[serde(default, deserialize_with = "at_least_100")]
21    pub notify_alert_timeout: Option<u64>,
22
23    /// Notify alert timeout in milliseconds
24    #[serde(default, deserialize_with = "at_least_100")]
25    pub script_timeout: Option<u64>,
26}
27
28fn at_least_100<'de, D>(d: D) -> Result<Option<u64>, D::Error>
29where
30    D: serde::de::Deserializer<'de>,
31{
32    let op = Option::<u64>::deserialize(d)?;
33
34    if let Some(ref value) = op
35        && value < &100
36    {
37        return Err(serde::de::Error::invalid_value(
38            serde::de::Unexpected::Unsigned(*value),
39            &"a value at least 100",
40        ));
41    }
42    Ok(op)
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_deserialize() {
51        let s = r#"
52        new_block_notify_script = "dasd"
53        network_alert_notify_script = "dasd"
54        script_timeout = 1
55        "#;
56
57        let ret = toml::from_str::<Config>(s);
58        assert!(ret.is_err());
59
60        let s = r#"
61        new_block_notify_script = "dasd"
62        network_alert_notify_script = "dasd"
63        script_timeout = 100
64        "#;
65        let ret = toml::from_str::<Config>(s);
66        assert!(ret.is_ok());
67    }
68}