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
64
65
66
67
68
69
70
71
72
73
74
75
76
//! fsadqqqqqqqqqq
//!
//!
//!
//! ssssssssssssasdfasdf
//! asdfsad
//! # examples
//! asdfjklasdf
use std::time::Duration;

use serde::{Deserialize, Serialize};
use serde_json::Value;

/// this is a req
#[derive(Serialize, Deserialize, Debug)]
pub struct Req {
    pub id: u16,
    pub method: String,
    pub params: Vec<Value>,
}

impl Req {
    pub fn with_id(id: u16, method: String, params: Vec<Value>) -> Req {
        Req { id, method, params }
    }
    pub fn new(method: String, params: Vec<Value>) -> Req {
        let id = fastrand::u16(..);
        Req { id, method, params }
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Transition {
    Sudden,
    Smooth {
        // minimum 30ms
        duration: Duration
    },
}

impl Transition {
    pub fn sudden() -> Transition {
        Self::Sudden
    }
    pub fn smooth(duration: Duration) -> Option<Transition> {
        if duration < Duration::from_millis(30) || duration.as_millis() > u64::MAX as u128 {
            None
        } else {
            Some(Self::Smooth { duration })
        }
    }

    pub fn text(&self) -> &'static str {
        match self {
            Self::Sudden => "sudden",
            Self::Smooth { .. } => "smooth"
        }
    }

    pub fn value(&self) -> u64 {
        match self {
            // is ignored anyway
            Self::Sudden => 0,
            Self::Smooth { duration } => duration.as_millis() as u64
        }
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test() {}
}