Guest User

Untitled

a guest
Jul 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. use mysql::{Error, Pool};
  2. use std::boxed::Box;
  3. use std::string::ToString;
  4.  
  5.  
  6. pub trait DataBase: Debug {}
  7.  
  8. #[derive(Debug)]
  9. pub enum DataBaseType {
  10. Mysql,
  11. }
  12.  
  13. impl DataBaseType {
  14. fn init_connect<TS>(
  15. &self,
  16. host: TS,
  17. port: &u32,
  18. username: TS,
  19. password: TS,
  20. database_name: TS,
  21. ) -> Result<Box<dyn DataBase>, DataBaseError>
  22. where
  23. TS: ToString,
  24. {
  25. match &self {
  26. DataBaseType::Mysql => {
  27. Mysql::new(host, port, username, password, database_name)
  28. // match Mysql::new(host, port, username, password, database_name) {
  29. // Ok(connect) => Ok(connect),
  30. // Err(error) => Err(error),
  31. // }
  32. }
  33. }
  34. }
  35. }
  36.  
  37. #[derive(Fail, Debug)]
  38. pub enum DataBaseError {
  39. Mysql(MysqlError),
  40. }
  41.  
  42. impl Display for DataBaseError {
  43. fn fmt(&self, f: &mut Formatter) -> FmtResult {
  44. match &self {
  45. DataBaseError::Mysql(error) => write!(f, "mysql error: {:?}", error),
  46. }
  47. }
  48. }
  49.  
  50. #[derive(Debug)]
  51. pub struct Mysql {
  52. pool: Pool,
  53. }
  54.  
  55. impl DataBase for Mysql {}
  56.  
  57. impl Mysql {
  58. pub fn new<TS>(
  59. host: TS,
  60. port: &u32,
  61. username: TS,
  62. password: TS,
  63. database_name: TS,
  64. ) -> Result<Box<Self>, DataBaseError>
  65. where
  66. TS: ToString,
  67. {
  68. Pool::new(format!(
  69. "mysql://r{}:{}@{}:{}/{}",
  70. username.to_string(),
  71. password.to_string(),
  72. host.to_string(),
  73. port,
  74. database_name.to_string()
  75. )).map(|pool| Box::new(Mysql { pool: pool }))
  76. .map_err(|error: Error| DataBaseError::Mysql(error))
  77. }
  78. }
  79.  
  80. fn main() {
  81. println!("Hello, world!");
  82. }
Add Comment
Please, Sign In to add comment