use crate::bareun::{CustomDictionary, DictSet};
use crate::custom_dict_client::CustomDictionaryServiceClient;
use crate::error::{BareunError, Result};
use std::collections::HashSet;
use std::fs::File;
use std::io::{BufRead, BufReader};
pub fn read_dic_file(user_dict_path: &str) -> HashSet<String> {
let mut dict_set = HashSet::new();
let file = File::open(user_dict_path).expect("Unable to open file");
let reader = BufReader::new(file);
for line in reader.lines() {
if let Ok(w) = line {
if !w.starts_with("#") {
let w2 = w.trim();
if !w2.is_empty() {
dict_set.insert(w2.to_string());
}
}
}
}
dict_set
}
pub fn pb_map_to_set(ds: &DictSet) -> HashSet<String> {
let mut ret = HashSet::new();
for k in ds.items.keys() {
ret.insert(k.clone());
}
ret
}
pub struct CustomDict {
pub domain: String,
pub cp_set: HashSet<String>,
pub np_set: HashSet<String>,
pub cp_caret_set: HashSet<String>,
pub vv_set: HashSet<String>,
pub va_set: HashSet<String>,
pub mm_set: HashSet<String>,
pub mag_set: HashSet<String>,
pub ic_set: HashSet<String>,
pub apikey: String,
pub host: String,
pub port: i32,
}
impl CustomDict {
pub fn new(domain: &str) -> Self {
if domain.is_empty() {
panic!("domain name must be specified.");
}
CustomDict {
domain: domain.to_string(),
cp_set: HashSet::new(),
np_set: HashSet::new(),
cp_caret_set: HashSet::new(),
vv_set: HashSet::new(),
va_set: HashSet::new(),
mm_set: HashSet::new(),
mag_set: HashSet::new(),
ic_set: HashSet::new(),
apikey: String::new(),
host: String::new(),
port: 0,
}
}
pub fn with_connection(domain: &str, apikey: &str, host: &str, port: i32) -> Self {
let mut d = Self::new(domain);
d.apikey = apikey.to_string();
d.host = host.to_string();
d.port = port;
d
}
pub fn set_connection(&mut self, apikey: &str, host: &str, port: i32) {
self.apikey = apikey.to_string();
self.host = host.to_string();
self.port = port;
}
pub fn read_np_set_from_file(&mut self, user_dict_path: &str) {
self.np_set = read_dic_file(user_dict_path);
}
pub fn read_cp_set_from_file(&mut self, user_dict_path: &str) {
self.cp_set = read_dic_file(user_dict_path);
}
pub fn read_cp_caret_set_from_file(&mut self, user_dict_path: &str) {
self.cp_caret_set = read_dic_file(user_dict_path);
}
pub fn read_vv_set_from_file(&mut self, user_dict_path: &str) {
self.vv_set = read_dic_file(user_dict_path);
}
pub fn read_va_set_from_file(&mut self, user_dict_path: &str) {
self.va_set = read_dic_file(user_dict_path);
}
pub fn read_mm_set_from_file(&mut self, user_dict_path: &str) {
self.mm_set = read_dic_file(user_dict_path);
}
pub fn read_mag_set_from_file(&mut self, user_dict_path: &str) {
self.mag_set = read_dic_file(user_dict_path);
}
pub fn read_ic_set_from_file(&mut self, user_dict_path: &str) {
self.ic_set = read_dic_file(user_dict_path);
}
pub fn copy_np_set(&mut self, dict_set: HashSet<String>) {
self.np_set = dict_set;
}
pub fn copy_cp_set(&mut self, dict_set: HashSet<String>) {
self.cp_set = dict_set;
}
pub fn copy_cp_caret_set(&mut self, dict_set: HashSet<String>) {
self.cp_caret_set = dict_set;
}
pub fn copy_vv_set(&mut self, dict_set: HashSet<String>) {
self.vv_set = dict_set;
}
pub fn copy_va_set(&mut self, dict_set: HashSet<String>) {
self.va_set = dict_set;
}
pub fn copy_mm_set(&mut self, dict_set: HashSet<String>) {
self.mm_set = dict_set;
}
pub fn copy_mag_set(&mut self, dict_set: HashSet<String>) {
self.mag_set = dict_set;
}
pub fn copy_ic_set(&mut self, dict_set: HashSet<String>) {
self.ic_set = dict_set;
}
pub async fn update(&self) -> Result<bool> {
if self.apikey.is_empty() || self.host.is_empty() {
return Err(BareunError::InvalidArgument {
message: "Connection information not set. Use set_connection() first.".to_string(),
});
}
let mut client =
CustomDictionaryServiceClient::new(&self.apikey, &self.host, self.port).await?;
client
.update(
&self.domain,
&self.np_set,
&self.cp_set,
&self.cp_caret_set,
&self.vv_set,
&self.va_set,
&self.mm_set,
&self.mag_set,
&self.ic_set,
)
.await
}
pub async fn get(&self) -> Result<CustomDictionary> {
if self.apikey.is_empty() || self.host.is_empty() {
return Err(BareunError::InvalidArgument {
message: "Connection information not set. Use set_connection() first.".to_string(),
});
}
let mut client =
CustomDictionaryServiceClient::new(&self.apikey, &self.host, self.port).await?;
client.get(&self.domain).await
}
pub async fn load(&mut self) -> Result<()> {
if self.apikey.is_empty() || self.host.is_empty() {
return Err(BareunError::InvalidArgument {
message: "Connection information not set. Use set_connection() first.".to_string(),
});
}
let mut client =
CustomDictionaryServiceClient::new(&self.apikey, &self.host, self.port).await?;
let d = client.get(&self.domain).await?;
if let Some(np_set) = d.np_set {
self.np_set = pb_map_to_set(&np_set);
}
if let Some(cp_set) = d.cp_set {
self.cp_set = pb_map_to_set(&cp_set);
}
if let Some(cp_caret_set) = d.cp_caret_set {
self.cp_caret_set = pb_map_to_set(&cp_caret_set);
}
if let Some(vv_set) = d.vv_set {
self.vv_set = pb_map_to_set(&vv_set);
}
if let Some(va_set) = d.va_set {
self.va_set = pb_map_to_set(&va_set);
}
if let Some(mm_set) = d.mm_set {
self.mm_set = pb_map_to_set(&mm_set);
}
if let Some(mag_set) = d.mag_set {
self.mag_set = pb_map_to_set(&mag_set);
}
if let Some(ic_set) = d.ic_set {
self.ic_set = pb_map_to_set(&ic_set);
}
Ok(())
}
pub async fn clear(&mut self) -> Result<Vec<String>> {
if self.apikey.is_empty() || self.host.is_empty() {
return Err(BareunError::InvalidArgument {
message: "Connection information not set. Use set_connection() first.".to_string(),
});
}
self.np_set.clear();
self.cp_set.clear();
self.cp_caret_set.clear();
self.vv_set.clear();
self.va_set.clear();
self.mm_set.clear();
self.mag_set.clear();
self.ic_set.clear();
let mut client =
CustomDictionaryServiceClient::new(&self.apikey, &self.host, self.port).await?;
client.remove(&[self.domain.clone()]).await
}
}