lib.rs (3336B)
1 use account::PublicAccount; 2 use database::DB; 3 use endpoints::Endpoints; 4 5 pub mod account; 6 pub mod data; 7 pub mod database; 8 pub mod endpoints; 9 pub mod node; 10 pub mod validate; 11 12 /// Cyrtophora configuration 13 pub struct CyrtophoraConfig { 14 pub endpoints: Option<Endpoints>, 15 #[cfg(feature = "sqlite")] 16 /// Path to database 17 pub database_path: String, 18 } 19 20 pub struct Cyrtophora<D> 21 where 22 D: DB, 23 { 24 /// Cyrtophora database 25 database: Option<D>, 26 /// API endpoints 27 pub endpoints: Endpoints, 28 #[cfg(feature = "node")] 29 pub node: Option<node::Node>, 30 } 31 32 impl<D: DB> Cyrtophora<D> { 33 #[cfg(feature = "node")] 34 pub fn new_sqlite_node( 35 config: CyrtophoraConfig, 36 ) -> Result<Cyrtophora<database::sqlite::SqliteDB>, database::error::Error> { 37 let mut db = database::sqlite::SqliteDB::new(&config.database_path); 38 db.connect()?; 39 40 // setup API endpoints 41 let endpoints = if let Some(e) = config.endpoints { 42 e 43 } else { 44 Endpoints::default() 45 }; 46 47 // setup node settings 48 let node = Some(node::Node {}); 49 Ok(Cyrtophora { 50 database: Some(db), 51 endpoints, 52 node, 53 }) 54 } 55 56 /// Create a new account 57 pub fn account_create( 58 &mut self, 59 payload: data::AccountCreationInput, 60 ) -> Result<PublicAccount, Box<dyn std::error::Error>> { 61 let account = account::Account::create(payload)?; 62 // TODO: email verification 63 64 // store account in database 65 if cfg!(feature = "sqlite") { 66 if let Some(db) = &self.database { 67 db.account_store(&account)?; 68 } 69 } 70 Ok(account.into()) 71 } 72 73 /// Get account public data, using the username as ID 74 pub fn account_get(&self, username: &str) -> Result<PublicAccount, database::error::Error> { 75 if cfg!(feature = "sqlite") { 76 if let Some(db) = &self.database { 77 let public_account = db.account_get_pub(username)?; 78 Ok(public_account) 79 } else { 80 Err(database::error::Error::Connection) 81 } 82 } else { 83 Err(database::error::Error::Connection) 84 } 85 } 86 } 87 88 #[cfg(test)] 89 mod test { 90 use super::*; 91 92 const TEST_DB_PATH: &str = "test-data/CYRTOPHORA.db"; 93 94 #[test] 95 #[cfg(feature = "node")] 96 fn test_sqlite_node() { 97 remove_test_db(); 98 let config = CyrtophoraConfig { 99 database_path: TEST_DB_PATH.to_string(), 100 endpoints: None, 101 }; 102 103 let mut cyrt = Cyrtophora::<database::sqlite::SqliteDB>::new_sqlite_node(config).unwrap(); 104 let account_input = crate::data::AccountCreationInput { 105 username: "testuser".to_string(), 106 password: "1234567890".to_string(), 107 retyped_password: "1234567890".to_string(), 108 email: None, 109 }; 110 111 cyrt.account_create(account_input).unwrap(); 112 let public_account = cyrt.account_get("testuser").unwrap(); 113 assert_eq!(&public_account.username, "testuser"); 114 } 115 116 fn remove_test_db() { 117 let test_db_path = std::path::Path::new(TEST_DB_PATH); 118 if std::path::Path::exists(test_db_path) { 119 std::fs::remove_file(test_db_path).unwrap(); 120 } 121 } 122 }