diff --git a/src/adapter/mariadb_innodb.rs b/src/adapter/mariadb_innodb.rs index 39961d2..c1310b7 100644 --- a/src/adapter/mariadb_innodb.rs +++ b/src/adapter/mariadb_innodb.rs @@ -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, @@ -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 { +#[derive(Clone, Debug)] +pub struct MariadbInnodbReflectionAdapter> { state: T, connection_string: String, database_name: String, } -impl MariadbInnodbReflectionAdapter { - pub fn new(connection_string: &str) -> MariadbInnodbReflectionAdapter { - MariadbInnodbReflectionAdapter:: { +impl MariadbInnodbReflectionAdapter> { + pub fn new(connection_string: &str) -> MariadbInnodbReflectionAdapter> { + MariadbInnodbReflectionAdapter::> { + state: Uninitialized::new(), connection_string: connection_string.to_string(), - ..Default::default() + database_name: String::new() } } } -impl ReflectionAdapterUninitialized>> - for MariadbInnodbReflectionAdapter +impl ReflectionAdapterUninitialized +for MariadbInnodbReflectionAdapter> { + type ValidAdapter = MariadbInnodbReflectionAdapter>; + fn set_connection_string(&mut self, connection_string: &str) { self.connection_string = connection_string.to_string(); } @@ -66,12 +69,14 @@ impl MariadbInnodbReflectionAdapter> { } } -impl ReflectionAdapter> - for MariadbInnodbReflectionAdapter> +impl ReflectionAdapter +for MariadbInnodbReflectionAdapter> { + type InvalidAdapter = MariadbInnodbReflectionAdapter>; + async fn disconnect( self, - ) -> Result, ReflectionAdapterError> { + ) -> Result>, ReflectionAdapterError> { self.get_connection().close().await; Ok(MariadbInnodbReflectionAdapter::new(&self.connection_string)) diff --git a/src/adapter/reflection_adapter.rs b/src/adapter/reflection_adapter.rs index 40b03e7..a862f6a 100644 --- a/src/adapter/reflection_adapter.rs +++ b/src/adapter/reflection_adapter.rs @@ -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(PhantomData); +impl Uninitialized { + pub fn new() -> Uninitialized { + Uninitialized(PhantomData) + } +} #[derive(Clone, Debug)] pub struct Connected(Pool); -pub trait State {} +pub trait State {} -impl State for Uninitialized {} +impl State for Uninitialized {} -impl State for Connected {} +impl State for Connected {} impl Deref for Connected { type Target = Pool; @@ -51,17 +57,20 @@ impl Display for ReflectionAdapterError { impl std::error::Error for ReflectionAdapterError {} -pub trait ReflectionAdapterUninitialized { +pub trait ReflectionAdapterUninitialized { + type ValidAdapter: ReflectionAdapter; + fn set_connection_string(&mut self, connection_string: &str); fn connect(self) - -> impl std::future::Future> + Send; + -> impl std::future::Future> + Send; } -pub trait ReflectionAdapter { +pub trait ReflectionAdapter { + type InvalidAdapter: ReflectionAdapterUninitialized; fn disconnect( self, - ) -> impl std::future::Future> + Send; + ) -> impl std::future::Future> + Send; fn set_database_name( &mut self,