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
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone)]
pub enum PrimitiveTy {
    /// handle
    Fd,
    /// number
    Number,
    /// zero-terminated string
    ZString,
    /// Buffer
    Buffer,
    /// Signal ID
    Signal,
    /// Address
    Address,
}

/// Data type
#[derive(Debug, Clone)]
pub enum Ty {
    /// not used in this syscall
    Null,
    Primitive(PrimitiveTy),
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "kind", content = "data")]
#[serde(rename_all = "kebab-case")]
// TODO: #[non_exhaustive]
pub enum Value {
    Integral(i64),
    Handle(u32 /*raw fd value*/, Option<u64> /* ray id*/),
    String(String),
    Buffer(Box<[u8]>),
    Flags(u64, Vec<String>),
    Signal(i32, Option<String>),
    Address(u64),
    Error(i32, String),
    Unknown,
    #[doc(hidden)]
    __NonExhaustive,
}

impl Value {
    pub fn is_known(&self) -> bool {
        match self {
            Value::Unknown => false,
            _ => true,
        }
    }

    pub fn is_scalar(&self) -> bool {
        true // revisit when implement structs
    }

    pub fn project<'a>(
        &'a self,
        mut path: impl Iterator<Item = impl AsRef<str>>,
    ) -> Option<&'a Value> {
        match self {
            smth if smth.is_scalar() => {
                if path.next().is_none() {
                    Some(self)
                } else {
                    None
                }
            }
            _ => None,
        }
    }
}