1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use futures_core::future::BoxFuture;
use rbdc::db::{ConnectOptions, Connection};
use rbdc::Error;
use serde::{Deserialize, Serialize};
use std::any::Any;

use crate::connection::OracleConnection;

#[derive(Serialize, Deserialize, Debug)]
pub struct OracleConnectOptions {
    pub username: String,
    pub password: String,
    pub connect_string: String,
}

impl ConnectOptions for OracleConnectOptions {
    fn connect(&self) -> BoxFuture<Result<Box<dyn Connection>, Error>> {
        Box::pin(async move {
            let v = OracleConnection::establish(self)
                .await
                .map_err(|e| Error::from(e.to_string()))?;
            Ok(Box::new(v) as Box<dyn Connection>)
        })
    }

    fn set_uri(&mut self, url: &str) -> Result<(), Error> {
        *self = OracleConnectOptions::from_str(url)?;
        Ok(())
    }

    fn uppercase_self(&self) -> &(dyn Any + Send + Sync) {
        self
    }
}

impl Default for OracleConnectOptions {
    fn default() -> Self {
        Self {
            username: "scott".to_owned(),
            password: "tiger".to_owned(),
            connect_string: "//localhost/XE".to_owned(),
        }
    }
}

impl OracleConnectOptions {
    pub fn from_str(s: &str) -> Result<Self, Error> {
        serde_json::from_str(s).map_err(|e| Error::from(e.to_string()))
    }
}