Skip to content

Commit

Permalink
trait bounds rework
Browse files Browse the repository at this point in the history
  • Loading branch information
iganev committed Feb 24, 2024
1 parent e04676c commit bb66659
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
29 changes: 17 additions & 12 deletions src/adapter/mariadb_innodb.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::marker::PhantomData;
use crate::adapter::reflection_adapter::ReflectionAdapterError::DatabaseError;
use crate::adapter::reflection_adapter::{
Connected, ReflectionAdapter, ReflectionAdapterError, ReflectionAdapterUninitialized, State,
Expand All @@ -13,27 +14,29 @@ use crate::reflection::{Column, Constraint, Database, DefaultValue, Index, SqlDa
use serde_json::Value;
use sqlx::mysql::MySqlPoolOptions;
use sqlx::{MySql, Pool};
use std::future::Future;

#[derive(Clone, Default, Debug)]
pub struct MariadbInnodbReflectionAdapter<T: State> {
#[derive(Clone, Debug)]
pub struct MariadbInnodbReflectionAdapter<T: State<MySql>> {
state: T,
connection_string: String,
database_name: String,
}

impl MariadbInnodbReflectionAdapter<Uninitialized> {
pub fn new(connection_string: &str) -> MariadbInnodbReflectionAdapter<Uninitialized> {
MariadbInnodbReflectionAdapter::<Uninitialized> {
impl MariadbInnodbReflectionAdapter<Uninitialized<MySql>> {
pub fn new(connection_string: &str) -> MariadbInnodbReflectionAdapter<Uninitialized<MySql>> {
MariadbInnodbReflectionAdapter::<Uninitialized<MySql>> {
state: Uninitialized::new(),
connection_string: connection_string.to_string(),
..Default::default()
database_name: String::new()
}
}
}

impl ReflectionAdapterUninitialized<MariadbInnodbReflectionAdapter<Connected<MySql>>>
for MariadbInnodbReflectionAdapter<Uninitialized>
impl ReflectionAdapterUninitialized<MySql>
for MariadbInnodbReflectionAdapter<Uninitialized<MySql>>
{
type ValidAdapter = MariadbInnodbReflectionAdapter<Connected<MySql>>;

fn set_connection_string(&mut self, connection_string: &str) {
self.connection_string = connection_string.to_string();
}
Expand Down Expand Up @@ -66,12 +69,14 @@ impl MariadbInnodbReflectionAdapter<Connected<MySql>> {
}
}

impl ReflectionAdapter<MariadbInnodbReflectionAdapter<Uninitialized>>
for MariadbInnodbReflectionAdapter<Connected<MySql>>
impl ReflectionAdapter<MySql>
for MariadbInnodbReflectionAdapter<Connected<MySql>>
{
type InvalidAdapter = MariadbInnodbReflectionAdapter<Uninitialized<MySql>>;

async fn disconnect(
self,
) -> Result<MariadbInnodbReflectionAdapter<Uninitialized>, ReflectionAdapterError> {
) -> Result<MariadbInnodbReflectionAdapter<Uninitialized<MySql>>, ReflectionAdapterError> {
self.get_connection().close().await;

Ok(MariadbInnodbReflectionAdapter::new(&self.connection_string))
Expand Down
25 changes: 17 additions & 8 deletions src/adapter/reflection_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use crate::reflection::{Database, Table};
use sqlx::{Error, Pool};
use std::fmt::{Display, Formatter};
use std::marker::PhantomData;
use std::ops::Deref;

#[derive(Clone, Default, Debug)]
pub struct Uninitialized;
pub struct Uninitialized<DB: sqlx::Database>(PhantomData<DB>);
impl<DB: sqlx::Database> Uninitialized<DB> {
pub fn new() -> Uninitialized<DB> {
Uninitialized(PhantomData)
}
}
#[derive(Clone, Debug)]
pub struct Connected<DB: sqlx::Database>(Pool<DB>);

pub trait State {}
pub trait State<DB: sqlx::Database> {}

impl State for Uninitialized {}
impl<DB: sqlx::Database> State<DB> for Uninitialized<DB> {}

impl<DB: sqlx::Database> State for Connected<DB> {}
impl<DB: sqlx::Database> State<DB> for Connected<DB> {}
impl<DB: sqlx::Database> Deref for Connected<DB> {
type Target = Pool<DB>;

Expand Down Expand Up @@ -51,17 +57,20 @@ impl Display for ReflectionAdapterError {

impl std::error::Error for ReflectionAdapterError {}

pub trait ReflectionAdapterUninitialized<T> {
pub trait ReflectionAdapterUninitialized<T: sqlx::Database> {
type ValidAdapter: ReflectionAdapter<T>;

fn set_connection_string(&mut self, connection_string: &str);

fn connect(self)
-> impl std::future::Future<Output = Result<T, ReflectionAdapterError>> + Send;
-> impl std::future::Future<Output = Result<Self::ValidAdapter, ReflectionAdapterError>> + Send;
}

pub trait ReflectionAdapter<T> {
pub trait ReflectionAdapter<T: sqlx::Database> {
type InvalidAdapter: ReflectionAdapterUninitialized<T>;
fn disconnect(
self,
) -> impl std::future::Future<Output = Result<T, ReflectionAdapterError>> + Send;
) -> impl std::future::Future<Output = Result<Self::InvalidAdapter, ReflectionAdapterError>> + Send;

fn set_database_name(
&mut self,
Expand Down

0 comments on commit bb66659

Please sign in to comment.