pub struct SmolDbClient { /* private fields */ }
Expand description

SmolDbClient struct used for communicating to the database. This struct has implementations that allow for end to end communication with the database server.

Implementations§

source§

impl SmolDbClient

source

pub fn new(ip: &str) -> Result<Self, ClientError>

Creates a new SmolDBClient struct connected to the ip address given.

use smol_db_client::SmolDbClient;

// create the new client
let mut client = SmolDbClient::new("localhost:8222").unwrap();
// client should be functional provided a database server was able to be connected to at the given location
source

pub fn setup_encryption( &mut self ) -> Result<DBSuccessResponse<String>, ClientError>

Requests the server to use encryption for communication. Encryption is done both ways, and is done using RSA with a 2048 bit key This function is slow due to large rsa key size ~1-4 seconds to generate the key Encryption is done invisibly.

use smol_db_client::SmolDbClient;
use smol_db_common::prelude::DBSettings;

let key = "test_key_123";
let mut client = SmolDbClient::new("localhost:8222").unwrap();
client.set_access_key(key.to_string()).unwrap();
client.setup_encryption().unwrap(); // encryption is done invisibly after it is setup, nothing else is needed :)
client.create_db("docsetup_encryption_test",DBSettings::default()).unwrap();
let _ = client.delete_db("docsetup_encryption_test").unwrap();
source

pub fn is_encryption_enabled(&self) -> bool

Returns true if end to end encryption is enabled

source

pub fn reconnect(&mut self) -> Result<(), ClientError>

Reconnects the client, this will reset the session, which can be used to remove any key that was used. Or to reconnect in the event of a loss of connection

use smol_db_client::SmolDbClient;
let mut client = SmolDbClient::new("localhost:8222").unwrap();

// disconnecting is optional between reconnects
client.disconnect().unwrap();
client.reconnect().unwrap();

// as shown here

client.reconnect().unwrap();
source

pub fn get_connected_ip(&self) -> Result<SocketAddr>

Returns a result containing the peer address of this client

source

pub fn disconnect(&self) -> Result<()>

Disconnects the socket from the database.

use smol_db_client::SmolDbClient;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

// disconnect the client
let _ = client.disconnect().expect("Failed to disconnect socket");
source

pub fn delete_data( &mut self, db_name: &str, db_location: &str ) -> Result<DBSuccessResponse<String>, ClientError>

Deletes the data at the given db location, requires permissions to do so.

use smol_db_client::client_error::ClientError;
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_packet_response::DBPacketResponseError;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_delete_data",DBSettings::default()).unwrap();
let _ = client.write_db("doctest_delete_data","cool_data_location","cool_data");
let read_data1 = client.read_db("doctest_delete_data","cool_data_location").unwrap().as_option().unwrap().to_string();
assert_eq!(read_data1.as_str(),"cool_data");

// delete the data at the given location
let _ = client.delete_data("doctest_delete_data","cool_data_location").unwrap();
let read_data2 = client.read_db("doctest_delete_data","cool_data_location");
assert_eq!(read_data2.unwrap_err(),ClientError::DBResponseError(DBPacketResponseError::ValueNotFound)); // is err here means DBResponseError(ValueNotFound)

let _ = client.delete_db("doctest_delete_data").unwrap();
source

pub fn get_role(&mut self, db_name: &str) -> Result<Role, ClientError>

Returns the role of the given client in the given db.

use smol_db_client::SmolDbClient;
use smol_db_common::db::Role;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_get_role",DBSettings::default()).unwrap();

// get the given clients role from a db
let role = client.get_role("doctest_get_role").unwrap();
assert_eq!(role,Role::SuperAdmin);

let _ = client.delete_db("doctest_get_role").unwrap();
source

pub fn get_db_settings( &mut self, db_name: &str ) -> Result<DBSettings, ClientError>

Gets the DBSettings of the given DB. Error on IO error, or when database name does not exist, or when the user lacks permissions to view DBSettings.

use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_get_db_settings",DBSettings::default()).unwrap();

// get the db settings
let settings = client.get_db_settings("doctest_get_db_settings").unwrap();
assert_eq!(settings,DBSettings::default());

let _ = client.delete_db("doctest_get_db_settings").unwrap();
source

pub fn set_db_settings( &mut self, db_name: &str, db_settings: DBSettings ) -> Result<DBSuccessResponse<String>, ClientError>

Sets the DBSettings of a given DB Error on IO Error, or when database does not exist, or when the user lacks permissions to set DBSettings

use std::time::Duration;
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_set_db_settings",DBSettings::default()).unwrap();

// set the new db settings
let new_settings = DBSettings::new(Duration::from_secs(10),(true,false,true),(false,false,false),vec![],vec![]);
let _ = client.set_db_settings("doctest_set_db_settings",new_settings.clone()).unwrap();

let settings = client.get_db_settings("doctest_set_db_settings").unwrap();
assert_eq!(settings,new_settings);

let _ = client.delete_db("doctest_set_db_settings").unwrap();
source

pub fn set_access_key( &mut self, key: String ) -> Result<DBSuccessResponse<String>, ClientError>

Sets this clients access key within the DB Server. The server will persist the key until the session is disconnected, or connection is lost.

use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

// sets the access key of the given client
let _ = client.set_access_key("test_key_123".to_string()).unwrap();
source

pub fn create_db( &mut self, db_name: &str, db_settings: DBSettings ) -> Result<DBSuccessResponse<String>, ClientError>

Creates a db through the client with the given name. Error on IO Error, or when the user lacks permissions to create a DB

use std::time::Duration;
use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_create_db",DBSettings::default()).unwrap();

let _ = client.delete_db("doctest_create_db").unwrap();
source

pub fn write_db( &mut self, db_name: &str, db_location: &str, data: &str ) -> Result<DBSuccessResponse<String>, ClientError>

Writes to a db at the location specified, with the data given as a string. Returns the data in the location that was over written if there was any. Requires permissions to write to the given DB

use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_write_data",DBSettings::default()).unwrap();

// write the given data to the given location within the specified db
let _ = client.write_db("doctest_write_data","cool_data_location","cool_data");

let read_data1 = client.read_db("doctest_write_data","cool_data_location").unwrap().as_option().unwrap().to_string();
assert_eq!(read_data1.as_str(),"cool_data");

let _ = client.delete_db("doctest_write_data").unwrap();
source

pub fn read_db( &mut self, db_name: &str, db_location: &str ) -> Result<DBSuccessResponse<String>, ClientError>

Reads from a db at the location specific. Returns an error if there is no data in the location. Requires permissions to read from the given DB

use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_read_db",DBSettings::default()).unwrap();

let _ = client.write_db("doctest_read_db","cool_data_location","cool_data");

// read the given database at the given location
let read_data1 = client.read_db("doctest_read_db","cool_data_location").unwrap().as_option().unwrap().to_string();
assert_eq!(read_data1.as_str(),"cool_data");

let _ = client.delete_db("doctest_read_db").unwrap();
source

pub fn delete_db( &mut self, db_name: &str ) -> Result<DBSuccessResponse<String>, ClientError>

Deletes the given db by name. Requires super admin privileges on the given DB Server

use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_delete_db",DBSettings::default()).unwrap();

// delete the db with the given name
let _ = client.delete_db("doctest_delete_db").unwrap();
source

pub fn list_db(&mut self) -> Result<Vec<DBPacketInfo>, ClientError>

Lists all the current databases available by name from the server Only error on IO Error

use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_packet_info::DBPacketInfo;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_list_db1",DBSettings::default()).unwrap();

// get list of databases currently on the server
let list_of_dbs1 = client.list_db().unwrap();
assert!(list_of_dbs1.contains(&DBPacketInfo::new("doctest_list_db1")));
assert!(!list_of_dbs1.contains(&DBPacketInfo::new("doctest_list_db2")));

let _ = client.create_db("doctest_list_db2",DBSettings::default()).unwrap();

// newly created databases show up after getting another copy of the list
let list_of_dbs2 = client.list_db().unwrap();
assert!(list_of_dbs2.contains(&DBPacketInfo::new("doctest_list_db2")));
assert!(list_of_dbs2.contains(&DBPacketInfo::new("doctest_list_db1")));

let _ = client.delete_db("doctest_list_db1").unwrap();
let _ = client.delete_db("doctest_list_db2").unwrap();
source

pub fn list_db_contents( &mut self, db_name: &str ) -> Result<HashMap<String, String>, ClientError>

Get the hashmap of the contents of a database. Contents are always String:String for the hashmap. Requires list permissions on the given DB

use smol_db_client::SmolDbClient;
use smol_db_common::db_packets::db_settings::DBSettings;

let mut client = SmolDbClient::new("localhost:8222").unwrap();

let _ = client.set_access_key("test_key_123".to_string()).unwrap();
let _ = client.create_db("doctest_list_cont_db",DBSettings::default()).unwrap();

let _ = client.write_db("doctest_list_cont_db","cool_data_location","cool_data");

let contents = client.list_db_contents("doctest_list_cont_db").unwrap();
assert_eq!(contents.len(),1);
assert_eq!(contents.get("cool_data_location").unwrap().as_str(),"cool_data");

let _ = client.delete_db("doctest_list_cont_db").unwrap();
source

pub fn list_db_contents_generic<T>( &mut self, db_name: &str ) -> Result<HashMap<String, T>, ClientError>
where for<'a> T: Serialize + Deserialize<'a>,

Lists the given db’s contents, deserializing the contents into a hash map.

source

pub fn write_db_generic<T>( &mut self, db_name: &str, db_location: &str, data: T ) -> Result<DBSuccessResponse<T>, ClientError>
where for<'a> T: Serialize + Deserialize<'a>,

Writes to the db while serializing the given data, returning the data at the location given and deserialized to the same type.

source

pub fn read_db_generic<T>( &mut self, db_name: &str, db_location: &str ) -> Result<DBSuccessResponse<T>, ClientError>
where for<'a> T: Serialize + Deserialize<'a>,

Reads from db and tries to deserialize the content at the location to the given generic

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V