runmod/
macros.rs

1macro_rules! make_runner {
2    ($func:ident, $varient: ident, $type:ty) => {
3        /// Reads the source file andf gets a new value.
4        /// I recommend that you disable autosave on your
5        /// text editor/IDE (especily if it is high frequency),
6        /// as this can cause unexpected panics.
7        /// 
8        /// This will update every time you call it, witch means
9        /// it will open the file, read it, parse the line, then close it.
10        #[inline(always)]
11        pub fn $func(&mut self) -> Option<$type> {
12            let line = BufReader::new(File::open(&self.file_name).unwrap())
13                .lines()
14                .nth(self.line_number as usize - 1)
15                .unwrap().unwrap();
16            
17            if let RunVar::$varient(x) = self.value {
18                let num = lexical_core::parse(Self::extract_runvar_arg(&line).expect(&format!("line: {}", line)).as_bytes()).unwrap_or(x);
19                if num != x {
20                    self.value = RunVar::$varient(num);
21                }
22                Some(num)
23            } else {
24                None
25            }
26        }
27    };
28}
29
30macro_rules! try_into_impl {
31    ($type:ty, $varient:ident) => {
32        ///the only time that this will fail is when doing something like the following:
33        /// 
34        /// ```
35        /// use runmod::RunVar;
36        /// 
37        /// let val = RunVar::I32(10);
38        /// assert_eq!(TryInto::<i64>::try_into(val).is_err(), true);
39        /// ```
40        impl TryInto<$type> for RunVar {
41            type Error = ();
42            
43            fn try_into(self) -> Result<$type, Self::Error> {
44                if let Self::$varient(x) = self {
45                    Ok(x)
46                } else {
47                    Err(())
48                }
49            }
50        }
51    };
52}
53
54macro_rules! from_impl {
55    ($type:ty, $varient:ident) => {
56        impl From<$type> for RunVar {
57            fn from(value: $type) -> Self {
58                Self::$varient(value)
59            }
60        }
61    };
62}