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
use crate::random::CurrentData;
use std::any::Any;
use std::collections::HashMap;
use std::fmt::Display;

#[derive(Debug)]
pub enum MapAnyValue {
    String(String),
    Usize(usize),
}

#[derive(Debug, Eq, PartialEq)]
pub enum IsWithinErrorType {
    Regular,
    MakePriority,
}

pub trait RuleTrait: RuleTraitClone + Display + std::fmt::Debug {
    fn is_within_range(
        &self,
        current_data: &CurrentData,
    ) -> std::result::Result<(), (IsWithinErrorType, String)>;

    fn as_any(&self) -> &dyn Any;

    fn is_match(
        &self,
        current_data: &CurrentData,
    ) -> std::result::Result<(), String>;

    fn get_numbers(
        &self,
        current_data: &CurrentData,
    ) -> std::result::Result<Vec<usize>, String>;

    fn share_data(
        &self,
        current_data: &CurrentData,
    ) -> Option<HashMap<String, MapAnyValue>>;

    fn check_count(
        &self,
        count: usize,
    ) -> std::result::Result<bool, String>;

    fn name(&self) -> String;
}

pub trait RuleTraitClone {
    fn clone_box(&self) -> Box<dyn RuleTrait>;
}

impl<T> RuleTraitClone for T
where
    T: 'static + RuleTrait + Clone,
{
    fn clone_box(&self) -> Box<dyn RuleTrait> {
        Box::new(self.clone())
    }
}


impl Clone for Box<dyn RuleTrait> {
    fn clone(&self) -> Box<dyn RuleTrait> {
        self.clone_box()
    }
}