forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.rs
More file actions
109 lines (89 loc) 路 3.41 KB
/
Copy pathparse.rs
File metadata and controls
109 lines (89 loc) 路 3.41 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::error::Error;
use crate::sqlite::SqliteConnectOptions;
use percent_encoding::percent_decode_str;
use std::borrow::Cow;
use std::path::Path;
use std::str::FromStr;
// https://www.sqlite.org/uri.html
impl FromStr for SqliteConnectOptions {
type Err = Error;
fn from_str(mut uri: &str) -> Result<Self, Self::Err> {
let mut options = Self::new();
// remove scheme from the URI
uri = uri
.trim_start_matches("sqlite://")
.trim_start_matches("sqlite:");
let mut database_and_params = uri.splitn(2, '?');
let database = database_and_params.next().unwrap_or_default();
if database == ":memory:" {
options.in_memory = true;
} else {
// % decode to allow for `?` or `#` in the filename
options.filename = Cow::Owned(
Path::new(
&*percent_decode_str(database)
.decode_utf8()
.map_err(Error::config)?,
)
.to_path_buf(),
);
}
if let Some(params) = database_and_params.next() {
for (key, value) in url::form_urlencoded::parse(params.as_bytes()) {
match &*key {
// The mode query parameter determines if the new database is opened read-only,
// read-write, read-write and created if it does not exist, or that the
// database is a pure in-memory database that never interacts with disk,
// respectively.
"mode" => {
match &*value {
"ro" => {
options.read_only = true;
}
// default
"rw" => {}
"rwc" => {
options.create_if_missing = true;
}
"memory" => {
options.in_memory = true;
}
_ => {
return Err(Error::Configuration(
format!("unknown value {:?} for `mode`", value).into(),
));
}
}
}
_ => {
return Err(Error::Configuration(
format!(
"unknown query parameter `{}` while parsing connection URI",
key
)
.into(),
));
}
}
}
}
Ok(options)
}
}
#[test]
fn test_parse_in_memory() -> Result<(), Error> {
let options: SqliteConnectOptions = "sqlite::memory:".parse()?;
assert!(options.in_memory);
let options: SqliteConnectOptions = "sqlite://?mode=memory".parse()?;
assert!(options.in_memory);
let options: SqliteConnectOptions = "sqlite://:memory:".parse()?;
assert!(options.in_memory);
Ok(())
}
#[test]
fn test_parse_read_only() -> Result<(), Error> {
let options: SqliteConnectOptions = "sqlite://a.db?mode=ro".parse()?;
assert!(options.read_only);
assert_eq!(&*options.filename.to_string_lossy(), "a.db");
Ok(())
}