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
use failure::Error;
use std::rc::Rc;
use ::{AnyFunction, DataGenOutput, ProgramContext, RunnableFunction};
use IString;

macro_rules! create_const_type {
    ($const_struct_name:ident, $output_type:ty, $any_type:path) => {
        #[derive(Debug, PartialEq, Clone)]
        pub struct $const_struct_name($output_type);

        impl RunnableFunction<$output_type> for $const_struct_name {
            fn gen_value(&self, _context: &mut ProgramContext) -> Result<$output_type, Error> {
                Ok(self.0.clone())
            }

            fn write_value(
                &self,
                _context: &mut ProgramContext,
                output: &mut DataGenOutput,
            ) -> Result<u64, Error> {
                output.write(&self.0).map_err(Into::into)
            }
        }

        impl $const_struct_name {
            #[allow(dead_code)]
            pub fn new<T: Into<$output_type>>(val: T) -> AnyFunction {
                let fun = Rc::new($const_struct_name(val.into()));
                $any_type(fun)
            }
        }
    };
}

create_const_type!(ConstBoolean, bool, AnyFunction::Boolean);
create_const_type!(ConstChar, char, AnyFunction::Char);
create_const_type!(ConstString, IString, AnyFunction::String);
create_const_type!(ConstUint, u64, AnyFunction::Uint);
create_const_type!(ConstInt, i64, AnyFunction::Int);
create_const_type!(ConstDecimal, f64, AnyFunction::Decimal);
create_const_type!(ConstBin, Vec<u8>, AnyFunction::Bin);

pub trait OutputType {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error>;
}

impl OutputType for char {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error> {
        writer.write_string(self).map_err(Into::into)
    }
}
impl OutputType for i64 {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error> {
        writer.write_string(self).map_err(Into::into)
    }
}
impl OutputType for u64 {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error> {
        writer.write_string(self).map_err(Into::into)
    }
}
impl OutputType for f64 {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error> {
        writer.write_string(self).map_err(Into::into)
    }
}
impl OutputType for bool {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error> {
        writer.write_string(self).map_err(Into::into)
    }
}
impl OutputType for IString {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error> {
        writer.write_string(self).map_err(Into::into)
    }
}
impl OutputType for str {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error> {
        writer.write_string(self).map_err(Into::into)
    }
}
impl OutputType for Vec<u8> {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error> {
        writer.write_bytes(self.as_slice()).map_err(Into::into)
    }
}
impl<'a> OutputType for &'a [u8] {
    fn write_output(&self, writer: &mut DataGenOutput) -> Result<u64, Error> {
        writer.write_bytes(self).map_err(Into::into)
    }
}