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
158
159
160
161
use std::convert::TryInto;

use rand::{random, thread_rng, Rng};

use crate::{Error, Result, Tool, ToolInfo, Value, ValueType};

pub struct RandomBoolean;

impl Tool for RandomBoolean {
    fn info(&self) -> ToolInfo<'static> {
        ToolInfo {
            identifier: "random_boolean",
            description: "Create a random boolean.",
            group: "random",
            inputs: vec![ValueType::Empty],
        }
    }

    fn run(&self, argument: &Value) -> Result<Value> {
        let argument = self.check_type(argument)?;

        if let Value::Empty = argument {
            let boolean = rand::thread_rng().gen();

            Ok(Value::Boolean(boolean))
        } else {
            self.fail(argument)
        }
    }
}

pub struct RandomInteger;

impl Tool for RandomInteger {
    fn info(&self) -> ToolInfo<'static> {
        ToolInfo {
            identifier: "random_integer",
            description: "Create a random integer.",
            group: "random",
            inputs: vec![
                ValueType::Empty,
                ValueType::Integer,
                ValueType::ListExact(vec![ValueType::Integer, ValueType::Integer]),
            ],
        }
    }

    fn run(&self, argument: &Value) -> Result<Value> {
        match argument {
            Value::Integer(max) => {
                let integer = rand::thread_rng().gen_range(0..*max);

                Ok(Value::Integer(integer))
            }
            Value::List(min_max) => {
                Error::expect_function_argument_amount(self.info().identifier, min_max.len(), 2)?;

                let min = min_max[0].as_int()?;
                let max = min_max[1].as_int()? + 1;
                let integer = rand::thread_rng().gen_range(min..max);

                Ok(Value::Integer(integer))
            }
            Value::Empty => Ok(crate::Value::Integer(random())),
            _ => self.fail(argument),
        }
    }
}

pub struct RandomString;

impl Tool for RandomString {
    fn info(&self) -> ToolInfo<'static> {
        ToolInfo {
            identifier: "random_string",
            description: "Generate a random string.",
            group: "random",
            inputs: vec![ValueType::Empty, ValueType::Integer],
        }
    }

    fn run(&self, argument: &Value) -> Result<Value> {
        let argument = self.check_type(argument)?;

        if let Value::Integer(length) = argument {
            let length: usize = length.unsigned_abs().try_into().unwrap_or(0);
            let mut random = String::with_capacity(length);

            for _ in 0..length {
                let random_char = thread_rng().gen_range('A'..='z').to_string();

                random.push_str(&random_char);
            }

            return Ok(Value::String(random));
        }

        if let Value::Empty = argument {
            let mut random = String::with_capacity(10);

            for _ in 0..10 {
                let random_char = thread_rng().gen_range('A'..='z').to_string();

                random.push_str(&random_char);
            }

            return Ok(Value::String(random));
        }

        self.fail(argument)
    }
}

pub struct RandomFloat;

impl Tool for RandomFloat {
    fn info(&self) -> ToolInfo<'static> {
        ToolInfo {
            identifier: "random_float",
            description: "Generate a random floating point value between 0 and 1.",
            group: "random",
            inputs: vec![ValueType::Empty],
        }
    }

    fn run(&self, argument: &Value) -> Result<Value> {
        let argument = self.check_type(argument)?;

        if argument.is_empty() {
            Ok(Value::Float(random()))
        } else {
            self.fail(argument)
        }
    }
}

pub struct Random;

impl Tool for Random {
    fn info(&self) -> ToolInfo<'static> {
        ToolInfo {
            identifier: "random",
            description: "Select a random item from a list.",
            group: "random",
            inputs: vec![ValueType::List],
        }
    }

    fn run(&self, argument: &Value) -> Result<Value> {
        let argument = self.check_type(argument)?;

        if let Value::List(list) = argument {
            let random_index = thread_rng().gen_range(0..list.len());
            let random_item = list.get(random_index).unwrap();

            Ok(random_item.clone())
        } else {
            self.fail(argument)
        }
    }
}