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
//! 提供读取数据 trait

use std::fmt::Display;

use crate::{App, AppData, FQLType};

pub trait Reader {
    fn index(&self, index: usize) -> Option<&AppData>;

    fn find<T: Display>(&self, key: T, val: FQLType) -> Option<&AppData>;

    fn find_index<T: Display>(&self, key: T, val: FQLType) -> Option<usize>;

    fn find_index_from_data(&self, data: &AppData) -> Option<usize>;

    fn find_all<T: Display>(&self, key: T, val: FQLType) -> Vec<&AppData>;

    fn find_all_index<T: Display>(&self, key: T, val: FQLType) -> Vec<usize>;

    fn find_by<T: Display>(&self, key: T, f: impl Fn(&FQLType) -> bool) -> Vec<&AppData>;
    fn find_index_by<T: Display>(&self, key: T, f: impl Fn(&FQLType) -> bool) -> Vec<usize>;
}

impl Reader for App {
    /// 查找指定序号的数据
    fn index(&self, index: usize) -> Option<&AppData> {
        self.data.get(index)
    }

    /// 查找第一个符合条件的数据
    fn find<T: Display>(&self, key: T, val: FQLType) -> Option<&AppData> {
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if val.eq(v) {
                    return Some(item);
                }
            }
        }

        None
    }

    /// 查找第一个符合条件的数据的序号
    fn find_index<T: Display>(&self, key: T, val: FQLType) -> Option<usize> {
        let mut index = 0;
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if val.eq(v) {
                    return Some(index);
                }
            }

            index += 1;
        }

        None
    }

    /// 获取所有符合条件的数据
    fn find_all<T: Display>(&self, key: T, val: FQLType) -> Vec<&AppData> {
        let mut res = Vec::new();
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if val.eq(v) {
                    res.push(item);
                }
            }
        }

        res
    }

    /// 获取所有符合条件的数据序号
    fn find_all_index<T: Display>(&self, key: T, val: FQLType) -> Vec<usize> {
        let mut index = 0;
        let mut res = Vec::new();
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if val.eq(v) {
                    res.push(index);
                }
            }

            index += 1;
        }
        res
    }

    /// 获取所有符合过滤函数的数据
    fn find_by<T: Display>(&self, key: T, f: impl Fn(&FQLType) -> bool) -> Vec<&AppData> {
        let mut res = Vec::new();
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if f(v) {
                    res.push(item);
                }
            }
        }

        res
    }

    // 获取所有符合过滤函数的数据
    fn find_index_by<T: Display>(&self, key: T, f: impl Fn(&FQLType) -> bool) -> Vec<usize> {
        let mut index = 0;
        let mut res = Vec::new();
        for item in &self.data {
            if let Some(v) = item.get(&key.to_string()) {
                if f(v) {
                    res.push(index);
                }
            }

            index += 1;
        }

        res
    }

    fn find_index_from_data(&self, data: &AppData) -> Option<usize> {
        let mut index = 0;
        for item in &self.data {
            if item.eq(data) {
                return Some(index);
            }

            index += 1;
        }

        None
    }
}