Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use mysql::{Error, Pool};
- use std::boxed::Box;
- use std::string::ToString;
- pub trait DataBase: Debug {}
- #[derive(Debug)]
- pub enum DataBaseType {
- Mysql,
- }
- impl DataBaseType {
- fn init_connect<TS>(
- &self,
- host: TS,
- port: &u32,
- username: TS,
- password: TS,
- database_name: TS,
- ) -> Result<Box<dyn DataBase>, DataBaseError>
- where
- TS: ToString,
- {
- match &self {
- DataBaseType::Mysql => {
- Mysql::new(host, port, username, password, database_name)
- // match Mysql::new(host, port, username, password, database_name) {
- // Ok(connect) => Ok(connect),
- // Err(error) => Err(error),
- // }
- }
- }
- }
- }
- #[derive(Fail, Debug)]
- pub enum DataBaseError {
- Mysql(MysqlError),
- }
- impl Display for DataBaseError {
- fn fmt(&self, f: &mut Formatter) -> FmtResult {
- match &self {
- DataBaseError::Mysql(error) => write!(f, "mysql error: {:?}", error),
- }
- }
- }
- #[derive(Debug)]
- pub struct Mysql {
- pool: Pool,
- }
- impl DataBase for Mysql {}
- impl Mysql {
- pub fn new<TS>(
- host: TS,
- port: &u32,
- username: TS,
- password: TS,
- database_name: TS,
- ) -> Result<Box<Self>, DataBaseError>
- where
- TS: ToString,
- {
- Pool::new(format!(
- "mysql://r{}:{}@{}:{}/{}",
- username.to_string(),
- password.to_string(),
- host.to_string(),
- port,
- database_name.to_string()
- )).map(|pool| Box::new(Mysql { pool: pool }))
- .map_err(|error: Error| DataBaseError::Mysql(error))
- }
- }
- fn main() {
- println!("Hello, world!");
- }
Add Comment
Please, Sign In to add comment