| 1 | // Annoying how deprecation warnings trigger in the same module as the deprecated item. |
| 2 | #![allow(deprecated)] |
| 3 | // Cargo features are broken in this file. |
| 4 | // `AnyKind` may return at some point but it won't be a simple enum. |
| 5 | #![allow(unexpected_cfgs)] |
| 6 | |
| 7 | use crate::error::Error; |
| 8 | use std::str::FromStr; |
| 9 | |
| 10 | #[deprecated = "not used or returned by any API"] |
| 11 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] |
| 12 | pub enum AnyKind { |
| 13 | #[cfg(feature = "postgres")] |
| 14 | Postgres, |
| 15 | |
| 16 | #[cfg(feature = "mysql")] |
| 17 | MySql, |
| 18 | |
| 19 | #[cfg(feature = "_sqlite")] |
| 20 | Sqlite, |
| 21 | |
| 22 | #[cfg(feature = "mssql")] |
| 23 | Mssql, |
| 24 | } |
| 25 | |
| 26 | impl FromStr for AnyKind { |
| 27 | type Err = Error; |
| 28 | |
| 29 | fn from_str(url: &str) -> Result<Self, Self::Err> { |
| 30 | match url { |
| 31 | #[cfg(feature = "postgres")] |
| 32 | _ if url.starts_with("postgres:") || url.starts_with("postgresql:") => { |
| 33 | Ok(AnyKind::Postgres) |
| 34 | } |
| 35 | |
| 36 | #[cfg(not(feature = "postgres"))] |
| 37 | _ if url.starts_with("postgres:") || url.starts_with("postgresql:") => { |
| 38 | Err(Error::Configuration("database URL has the scheme of a PostgreSQL database but the `postgres` feature is not enabled".into())) |
| 39 | } |
| 40 | |
| 41 | #[cfg(feature = "mysql")] |
| 42 | _ if url.starts_with("mysql:") || url.starts_with("mariadb:") => { |
| 43 | Ok(AnyKind::MySql) |
| 44 | } |
| 45 | |
| 46 | #[cfg(not(feature = "mysql"))] |
| 47 | _ if url.starts_with("mysql:") || url.starts_with("mariadb:") => { |
| 48 | Err(Error::Configuration("database URL has the scheme of a MySQL database but the `mysql` feature is not enabled".into())) |
| 49 | } |
| 50 | |
| 51 | #[cfg(feature = "_sqlite")] |
| 52 | _ if url.starts_with("sqlite:") => { |
| 53 | Ok(AnyKind::Sqlite) |
| 54 | } |
| 55 | |
| 56 | #[cfg(not(feature = "_sqlite"))] |
| 57 | _ if url.starts_with("sqlite:") => { |
| 58 | Err(Error::Configuration("database URL has the scheme of a SQLite database but the `sqlite` feature is not enabled".into())) |
| 59 | } |
| 60 | |
| 61 | #[cfg(feature = "mssql")] |
| 62 | _ if url.starts_with("mssql:") || url.starts_with("sqlserver:") => { |
| 63 | Ok(AnyKind::Mssql) |
| 64 | } |
| 65 | |
| 66 | #[cfg(not(feature = "mssql"))] |
| 67 | _ if url.starts_with("mssql:") || url.starts_with("sqlserver:") => { |
| 68 | Err(Error::Configuration("database URL has the scheme of a MSSQL database but the `mssql` feature is not enabled".into())) |
| 69 | } |
| 70 | |
| 71 | _ => Err(Error::Configuration(format!("unrecognized database url: {url:?}").into())) |
| 72 | } |
| 73 | } |
| 74 | } |