Skip to main content

video_interop/
modifier.rs

1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
2pub enum Modifier {
3    Implicit,
4    Explicit(u64),
5}
6
7impl Modifier {
8    pub const fn linear() -> Self {
9        Self::Explicit(0)
10    }
11
12    pub const fn explicit(self) -> Option<u64> {
13        match self {
14            Self::Implicit => None,
15            Self::Explicit(value) => Some(value),
16        }
17    }
18}
19
20#[cfg(feature = "rustler")]
21mod rustler_impl {
22    use rustler::{Decoder, Encoder, Env, Error, NifResult, Term};
23
24    use super::Modifier;
25
26    mod atoms {
27        rustler::atoms! {
28            implicit
29        }
30    }
31
32    impl<'a> Decoder<'a> for Modifier {
33        fn decode(term: Term<'a>) -> NifResult<Self> {
34            if let Ok(atom) = term.decode::<rustler::Atom>() {
35                return if atom == atoms::implicit() {
36                    Ok(Self::Implicit)
37                } else {
38                    Err(Error::BadArg)
39                };
40            }
41
42            term.decode::<u64>().map(Self::Explicit)
43        }
44    }
45
46    impl Encoder for Modifier {
47        fn encode<'a>(&self, env: Env<'a>) -> Term<'a> {
48            match self {
49                Self::Implicit => atoms::implicit().encode(env),
50                Self::Explicit(value) => value.encode(env),
51            }
52        }
53    }
54}