lib.rs (3552B)
1 #![deny(unsafe_code, clippy::unwrap_used)] 2 #![warn( 3 clippy::cognitive_complexity, 4 clippy::branches_sharing_code, 5 clippy::imprecise_flops, 6 clippy::missing_const_for_fn, 7 clippy::mutex_integer, 8 clippy::path_buf_push_overwrite, 9 clippy::redundant_pub_crate, 10 clippy::pedantic, 11 clippy::dbg_macro, 12 clippy::todo, 13 clippy::fallible_impl_from, 14 clippy::filetype_is_file, 15 clippy::suboptimal_flops, 16 clippy::fn_to_numeric_cast_any, 17 clippy::if_then_some_else_none, 18 clippy::imprecise_flops, 19 clippy::lossy_float_literal, 20 clippy::panic_in_result_fn, 21 clippy::clone_on_ref_ptr 22 )] 23 #![allow(clippy::missing_panics_doc, clippy::panic_in_result_fn)] 24 // I am lazy. Dont blame me! 25 #![allow(missing_docs)] 26 27 use std::{collections::BTreeSet, fs::OpenOptions, io::Read, path::PathBuf}; 28 29 pub use indradb; 30 pub use indradb_proto; 31 use serde::{Deserialize, Serialize}; 32 use tokio::time::{sleep, Duration}; 33 use tracing::instrument; 34 35 pub async fn get_client( 36 endpoint: String, 37 ) -> Result<indradb_proto::Client, indradb_proto::ClientError> { 38 let mut client = indradb_proto::Client::new(endpoint.try_into()?).await?; 39 client.ping().await?; 40 Ok(client) 41 } 42 43 pub async fn get_client_retrying( 44 endpoint: String, 45 ) -> Result<indradb_proto::Client, indradb_proto::ClientError> { 46 let mut retry_count = 10u8; 47 let mut last_err = Option::<indradb_proto::ClientError>::None; 48 49 while retry_count > 0 { 50 match get_client(endpoint.clone()).await { 51 Ok(client) => return Ok(client), 52 Err(err) => { 53 last_err = Some(err); 54 if retry_count == 0 { 55 break; 56 } 57 sleep(Duration::from_secs(1)).await; 58 retry_count -= 1; 59 } 60 } 61 } 62 63 Err(last_err.expect("We didnt get an error even though connection failed")) 64 } 65 66 fn get_identifier_config() -> PathBuf { 67 let config_path = dirs::config_dir(); 68 if let Some(config_path) = config_path { 69 config_path.join("knowledge-search/identifier.json") 70 } else { 71 panic!("System not supported.") 72 } 73 } 74 75 #[derive(Debug, Serialize, Deserialize)] 76 struct IdentifierConfig { 77 identifiers: BTreeSet<String>, 78 } 79 80 pub fn get_identifier() -> Result<BTreeSet<String>, std::io::Error> { 81 let identifier_file = OpenOptions::new() 82 .read(true) 83 .create(true) 84 .open(get_identifier_config())?; 85 let identifier_config: IdentifierConfig = serde_json::from_reader(identifier_file)?; 86 Ok(identifier_config.identifiers) 87 } 88 89 #[instrument] 90 pub fn add_identifiers(identifiers: &mut BTreeSet<String>) -> Result<(), std::io::Error> { 91 let config_path = get_identifier_config(); 92 let prefix = config_path.parent().expect("No parent folder found"); 93 std::fs::create_dir_all(prefix)?; 94 95 let mut identifier_file = OpenOptions::new() 96 .read(true) 97 .write(true) 98 .truncate(true) 99 .create(true) 100 .open(config_path)?; 101 let mut s = String::new(); 102 identifier_file 103 .read_to_string(&mut s) 104 .expect("Unable to read file"); 105 106 let mut identifier_config: IdentifierConfig; 107 if s.is_empty() { 108 identifier_config = IdentifierConfig { 109 identifiers: BTreeSet::new(), 110 }; 111 } else { 112 identifier_config = serde_json::from_str(&s)?; 113 } 114 identifier_config.identifiers.append(identifiers); 115 serde_json::to_writer_pretty(&identifier_file, &identifier_config)?; 116 identifier_file.sync_all()?; 117 118 Ok(()) 119 }