sqlx_core_oldapi/any/
kind.rs1use crate::error::Error;
2use std::str::FromStr;
3
4#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5pub enum AnyKind {
6 #[cfg(feature = "postgres")]
7 Postgres,
8
9 #[cfg(feature = "mysql")]
10 MySql,
11
12 #[cfg(feature = "sqlite")]
13 Sqlite,
14
15 #[cfg(feature = "mssql")]
16 Mssql,
17}
18
19impl FromStr for AnyKind {
20 type Err = Error;
21
22 fn from_str(url: &str) -> Result<Self, Self::Err> {
23 match url {
24 #[cfg(feature = "postgres")]
25 _ if url.starts_with("postgres:") || url.starts_with("postgresql:") => {
26 Ok(AnyKind::Postgres)
27 }
28
29 #[cfg(not(feature = "postgres"))]
30 _ if url.starts_with("postgres:") || url.starts_with("postgresql:") => {
31 Err(Error::Configuration("database URL has the scheme of a PostgreSQL database but the `postgres` feature is not enabled".into()))
32 }
33
34 #[cfg(feature = "mysql")]
35 _ if url.starts_with("mysql:") || url.starts_with("mariadb:") => {
36 Ok(AnyKind::MySql)
37 }
38
39 #[cfg(not(feature = "mysql"))]
40 _ if url.starts_with("mysql:") || url.starts_with("mariadb:") => {
41 Err(Error::Configuration("database URL has the scheme of a MySQL database but the `mysql` feature is not enabled".into()))
42 }
43
44 #[cfg(feature = "sqlite")]
45 _ if url.starts_with("sqlite:") => {
46 Ok(AnyKind::Sqlite)
47 }
48
49 #[cfg(not(feature = "sqlite"))]
50 _ if url.starts_with("sqlite:") => {
51 Err(Error::Configuration("database URL has the scheme of a SQLite database but the `sqlite` feature is not enabled".into()))
52 }
53
54 #[cfg(feature = "mssql")]
55 _ if url.starts_with("mssql:") || url.starts_with("sqlserver:") => {
56 Ok(AnyKind::Mssql)
57 }
58
59 #[cfg(not(feature = "mssql"))]
60 _ if url.starts_with("mssql:") || url.starts_with("sqlserver:") => {
61 Err(Error::Configuration("database URL has the scheme of a MSSQL database but the `mssql` feature is not enabled".into()))
62 }
63
64 _ => Err(Error::Configuration(format!("unrecognized database url: {:?}", url).into()))
65 }
66 }
67}