br_ocr/
lib.rs

1use std::collections::HashMap;
2use std::fs;
3use json::{JsonValue, object};
4use crate::mode::OcrMode;
5
6mod mode;
7
8#[derive(Clone)]
9pub struct Ocr {
10    default: String,
11    connections: HashMap<String, Connection>,
12}
13
14impl Ocr {
15    pub fn new(path: &str) -> Self {
16        let text = fs::read_to_string(path).unwrap_or("".to_string());
17        let json = {
18            if text.is_empty() {
19                let mut def = Ocr::news();
20                let json = def.json();
21                fs::write(path, json.to_string()).unwrap();
22                json
23            } else {
24                json::parse(&text.clone()).unwrap()
25            }
26        };
27        Ocr::from(json)
28    }
29    pub fn from(data: JsonValue) -> Self {
30        let default = data["default"].to_string();
31        let mut connections = HashMap::new();
32        for (key, value) in data["connections"].entries() {
33            let connection = Connection::new().from(value.clone()).clone();
34            connections.insert(key.to_string(), connection.clone());
35        }
36        Self {
37            default,
38            connections,
39        }
40    }
41    pub fn news() -> Self {
42        let mut connections = HashMap::new();
43        connections.insert("my_name".to_string(), Connection::new());
44        Self {
45            default: "my_name".to_string(),
46            connections,
47        }
48    }
49    fn json(&mut self) -> JsonValue {
50        let mut data = object! {};
51        data["default"] = self.default.clone().into();
52        let mut connections = object! {};
53        for (name, connection) in self.connections.iter_mut() {
54            connections[name.clone()] = connection.json().clone();
55        }
56        data["connections"] = connections;
57        data
58    }
59    pub fn connection(&mut self, name: &str) -> &mut Self {
60        self.default = name.to_string();
61        self
62    }
63
64    pub fn ocr(&mut self, file_path: &str, file_b64: &str) -> JsonValue {
65        match self.connections.get(&*self.default).unwrap().mode {
66            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).ocr(file_path, file_b64),
67            Mode::Baidu => mode::baidu::Baidu::new(self.connections.get(&*self.default).unwrap().clone()).ocr(file_path, file_b64)
68        }
69    }
70
71    pub fn get_mail(&mut self, list: JsonValue) -> String {
72        let ocr_res = list.clone();
73        let mut res = String::new();
74
75        for member in ocr_res.members() {
76            let temp = member.to_string();
77            if temp.contains("@") {
78                let item = temp.trim();
79                if item.contains(":") {
80                    let temp_vec: Vec<&str> = item.split(":").collect();
81                    for i in temp_vec {
82                        if i.contains("@") {
83                            res = i.trim().to_string();
84                        }
85                    }
86                    break
87                } else if item.contains(":") {
88                    let temp_vec: Vec<&str> = item.split(":").collect();
89                    for i in temp_vec {
90                        if i.contains("@") {
91                            res = i.trim().to_string();
92                        }
93                    }
94                    break
95                } else {
96                    res = item.to_string();
97                    break
98                }
99            } else {
100                continue
101            }
102        }
103        res
104    }
105
106    pub fn translate(&mut self, text: &str) -> JsonValue {
107        match self.connections.get(&*self.default).unwrap().mode {
108            Mode::Aliyun => mode::aliyun::Aliyun::new(self.connections.get(&*self.default).unwrap().clone()).translate(text),
109            Mode::Baidu => mode::baidu::Baidu::new(self.connections.get(&*self.default).unwrap().clone()).translate(text),
110        }
111    }
112
113}
114
115
116#[derive(Clone)]
117pub struct Connection {
118    pub mode: Mode,
119    pub appid: String,
120    pub secret: String,
121}
122
123impl Default for Connection {
124    fn default() -> Self {
125        Self::new()
126    }
127}
128
129impl Connection {
130    pub fn new() -> Connection {
131        Self {
132            mode: Mode::Aliyun,
133            appid: "".to_string(),
134            secret: "".to_string(),
135        }
136    }
137    pub fn json(&mut self) -> JsonValue {
138        let mut data = object! {};
139        data["mode"] = self.mode.str().into();
140        data["appid"] = self.appid.clone().into();
141        data["secret"] = self.secret.clone().into();
142        data
143    }
144    pub fn from(&mut self, data: JsonValue) -> &mut Connection {
145        self.mode = Mode::from(data["mode"].as_str().unwrap());
146        self.appid = data["appid"].to_string();
147        self.secret = data["secret"].to_string();
148        self
149    }
150}
151
152
153#[derive(Clone)]
154pub enum Mode {
155    Aliyun,
156    Baidu,
157}
158
159impl Mode {
160    pub fn str(&mut self) -> &'static str {
161        match self {
162            Mode::Aliyun => "aliyun",
163            Mode::Baidu => "baidu"
164        }
165    }
166    pub fn from(name: &str) -> Self {
167        match name {
168            "aliyun" => Mode::Aliyun,
169            "baidu" => Mode::Baidu,
170            _ => Mode::Aliyun
171        }
172    }
173}