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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
macro_rules! define_struct {
    (
        $(#[$odoc: meta])*
        pub struct $name: ident $(<$lt: tt>)? {
            $(
                $(#[$idoc: meta])*
                $item_name: ident : $ty: ty,
            )+
        }
    ) => {
        $(#[$odoc])*
        #[derive(Debug, PartialEq, Clone)]
        pub struct $name $(<$lt>)? {
            $(
                $item_name : $ty,
            )+
        }

        impl$(<$lt>)? $name$(<$lt>)? {
            $(
                $(#[$idoc])*
                pub fn $item_name(&self) -> &$ty {
                    &self.$item_name
                }
            )*
        }
    }
}

macro_rules! list_impl {
    (
        $(#[$k: meta])*
        $fn_name: ident, $path: expr, $return_type: ty, $sep: expr, $skip: literal
    ) => {
        #[doc="Return parsed content of "]
        #[doc=$path]
        #[doc=".\n\n See it's return type for details."]
        $(#[$k])*
        pub fn $fn_name() -> Result<Vec<$return_type>, crate::ProcErr> {
            let content = std::fs::read_to_string($path)?;
            let mut ret = vec![];
            for block in content.trim().split($sep).skip($skip) {
                let v: $return_type = block.parse()?;
                ret.push(v);
            }
            Ok(ret)
        }

        test_impl!($fn_name);
    }
}

macro_rules! instance_impl {
    (
        $(#[$k: meta])*
        $fn_name: ident, $path: expr, $return_type: ty
    ) => {
        #[doc="Return parsed content of "]
        #[doc=$path]
        #[doc=".\n\n See it's return type for details."]
        $(#[$k])*
        pub fn $fn_name() -> Result<$return_type, crate::ProcErr> {
            let content = std::fs::read_to_string($path)?;
            content.trim().parse()
        }

        test_impl!($fn_name);
    }
}

macro_rules! pid_instance_impl {
    (
        $(#[$k: meta])*
        $fn_name: ident, $file_name: expr, $return_type: ty,
        $self_fn_name: ident, $of_task_fn_name: ident, $self_task_fn_name: ident
    ) => {
        #[doc="Return parsed content of `/proc/[pid]/"]
        #[doc=$file_name]
        #[doc="`.\n\n See it's return type for details."]
        $(#[$k])*
        pub fn $fn_name(pid: u32) -> Result<$return_type, crate::ProcErr> {
            let path = format!(concat!("/proc/{}/", $file_name), pid);
            let content = std::fs::read_to_string(path)?;
            content.trim().parse()
        }

        #[doc="Return parsed content of `/proc/self/"]
        #[doc=$file_name]
        #[doc="`.\n\n See it's return type for details.\n\n"]
        $(#[$k])*
        pub fn $self_fn_name() -> Result<$return_type, crate::ProcErr> {
            let path = concat!("/proc/self/", $file_name);
            let content = std::fs::read_to_string(path)?;
            content.trim().parse()
        }

        test_impl!($self_fn_name);

        #[doc="Return parsed content of `/proc/[pid]/task/[tid]/"]
        #[doc=$file_name]
        #[doc="`.\n\n See it's return type for details.\n\n"]
        $(#[$k])*
        pub fn $of_task_fn_name(pid: u32, tid: u32) -> Result<$return_type, crate::ProcErr> {
            let path = format!(concat!("/proc/{}/task/{}/", $file_name), pid, tid);
            let content = std::fs::read_to_string(path)?;
            content.trim().parse()
        }

        #[doc="Return parsed content of `/proc/self/task/[tid]/"]
        #[doc=$file_name]
        #[doc="`.\n\n See it's return type for details.\n\n"]
        $(#[$k])*
        pub fn $self_task_fn_name(tid: u32) -> Result<$return_type, crate::ProcErr> {
            let path = format!(concat!("/proc/self/task/{}/", $file_name), tid);
            let content = std::fs::read_to_string(path)?;
            content.trim().parse()
        }
    }
}

macro_rules! define_modules {
    (
        $(
            $mod_name: ident;
        )*
    ) => {
        $(
            #[doc(hidden)]
            pub mod $mod_name;
            #[doc(inline)]
            pub use $mod_name::*;
        )*
    };
}

macro_rules! test_impl {
    ($fn_name: ident) => {
        #[cfg(test)]
        #[test]
        fn test_impl() {
            use std::io::ErrorKind;

            let ret = $fn_name();
            if let Err(e) = ret {
                match e {
                    $crate::ProcErr::IO(inner_err) => {
                        match inner_err.kind() {
                            ErrorKind::NotFound => (),
                            _ => Err(inner_err).unwrap()
                        }
                    },
                    _ => Err(e).unwrap()
                }
            }
        }
    };
}