Skip to content

Commit 65f34b3

Browse files
committed
Change &str args to Into<String>
1 parent 23501b7 commit 65f34b3

File tree

2 files changed

+85
-74
lines changed

2 files changed

+85
-74
lines changed

src/builder.rs

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ pub struct Builder {
2020
// TODO: Exact, planned, estimated count (HEAD verb)
2121
// TODO: Response format
2222
// TODO: Embedded resources
23+
// TODO: Content type (csv, etc.)
2324
impl Builder {
24-
pub fn new(url: &str, schema: Option<String>) -> Self {
25+
pub fn new<S>(url: S, schema: Option<String>) -> Self
26+
where
27+
S: Into<String>,
28+
{
2529
let mut builder = Builder {
2630
method: Method::GET,
27-
url: url.to_string(),
31+
url: url.into(),
2832
schema,
2933
headers: HeaderMap::new(),
3034
..Default::default()
@@ -35,10 +39,13 @@ impl Builder {
3539
builder
3640
}
3741

38-
pub fn auth(mut self, token: &str) -> Self {
42+
pub fn auth<S>(mut self, token: S) -> Self
43+
where
44+
S: Into<String>,
45+
{
3946
self.headers.append(
4047
"Authorization",
41-
HeaderValue::from_str(&format!("Bearer {}", token)).unwrap(),
48+
HeaderValue::from_str(&format!("Bearer {}", token.into())).unwrap(),
4249
);
4350
self
4451
}
@@ -49,19 +56,24 @@ impl Builder {
4956
// TODO: JSON columns
5057
// TODO: Computed (virtual) columns
5158
// TODO: Investigate character corner cases (Unicode, [ .,:()])
52-
pub fn select(mut self, column: &str) -> Self {
59+
pub fn select<S>(mut self, column: S) -> Self
60+
where
61+
S: Into<String>,
62+
{
5363
self.method = Method::GET;
54-
self.queries
55-
.push(("select".to_string(), column.to_string()));
64+
self.queries.push(("select".to_string(), column.into()));
5665
self
5766
}
5867

5968
// TODO: desc/asc
6069
// TODO: nullsfirst/nullslast
6170
// TODO: Multiple columns
6271
// TODO: Computed columns
63-
pub fn order(mut self, column: &str) -> Self {
64-
self.queries.push(("order".to_string(), column.to_string()));
72+
pub fn order<S>(mut self, column: S) -> Self
73+
where
74+
S: Into<String>,
75+
{
76+
self.queries.push(("order".to_string(), column.into()));
6577
self
6678
}
6779

@@ -96,48 +108,56 @@ impl Builder {
96108
// TODO: Write-only tables
97109
// TODO: URL-encoded payload
98110
// TODO: Allow specifying columns
99-
pub fn insert(mut self, body: &str) -> Self {
111+
pub fn insert<S>(mut self, body: S) -> Self
112+
where
113+
S: Into<String>,
114+
{
100115
self.method = Method::POST;
101116
self.headers
102117
.insert("Prefer", HeaderValue::from_static("return=representation"));
103-
self.body = Some(body.to_string());
118+
self.body = Some(body.into());
104119
self
105120
}
106121

107-
pub fn insert_csv(mut self, body: &str) -> Self {
108-
self.headers
109-
.insert("Content-Type", HeaderValue::from_static("text/csv"));
110-
self.insert(body)
111-
}
112-
113122
// TODO: Allow Prefer: resolution=ignore-duplicates
114123
// TODO: on_conflict (make UPSERT work on UNIQUE columns)
115-
pub fn upsert(mut self, body: &str) -> Self {
124+
pub fn upsert<S>(mut self, body: S) -> Self
125+
where
126+
S: Into<String>,
127+
{
116128
self.method = Method::POST;
117129
self.headers.append(
118130
"Prefer",
119131
// Maybe check if this works as intended...
120132
HeaderValue::from_static("return=representation; resolution=merge-duplicates"),
121133
);
122-
self.body = Some(body.to_string());
134+
self.body = Some(body.into());
123135
self
124136
}
125137

126-
pub fn single_upsert(mut self, primary_column: &str, key: &str, body: &str) -> Self {
138+
pub fn single_upsert<S, T, U>(mut self, primary_column: S, key: T, body: U) -> Self
139+
where
140+
S: Into<String>,
141+
T: Into<String>,
142+
U: Into<String>,
143+
{
127144
self.method = Method::PUT;
128145
self.headers
129146
.append("Prefer", HeaderValue::from_static("return=representation"));
130147
self.queries
131-
.push((primary_column.to_string(), format!("eq.{}", key)));
132-
self.body = Some(body.to_string());
148+
.push((primary_column.into(), format!("eq.{}", key.into())));
149+
self.body = Some(body.into());
133150
self
134151
}
135152

136-
pub fn update(mut self, body: &str) -> Self {
153+
pub fn update<S>(mut self, body: S) -> Self
154+
where
155+
S: Into<String>,
156+
{
137157
self.method = Method::PATCH;
138158
self.headers
139159
.append("Prefer", HeaderValue::from_static("return=representation"));
140-
self.body = Some(body.to_string());
160+
self.body = Some(body.into());
141161
self
142162
}
143163

@@ -148,9 +168,12 @@ impl Builder {
148168
self
149169
}
150170

151-
pub fn rpc(mut self, params: &str) -> Self {
171+
pub fn rpc<S>(mut self, params: S) -> Self
172+
where
173+
S: Into<String>,
174+
{
152175
self.method = Method::POST;
153-
self.body = Some(params.to_string());
176+
self.body = Some(params.into());
154177
self.is_rpc = true;
155178
self
156179
}
@@ -178,14 +201,13 @@ impl Builder {
178201
#[cfg(test)]
179202
mod tests {
180203
use super::*;
181-
use crate::Postgrest;
182204

183-
const REST_URL: &str = "http://localhost:3000";
205+
const TABLE_URL: &str = "http://localhost:3000/table";
206+
const RPC_URL: &str = "http://localhost/rpc";
184207

185208
#[test]
186209
fn only_accept_json() {
187-
let client = Postgrest::new(REST_URL);
188-
let builder = client.from("users");
210+
let builder = Builder::new(TABLE_URL, None);
189211
assert_eq!(
190212
builder.headers.get("Accept").unwrap(),
191213
HeaderValue::from_static("application/json")
@@ -194,8 +216,7 @@ mod tests {
194216

195217
#[test]
196218
fn auth_with_token() {
197-
let client = Postgrest::new(REST_URL);
198-
let builder = client.from("users").auth("$Up3rS3crET");
219+
let builder = Builder::new(TABLE_URL, None).auth("$Up3rS3crET");
199220
assert_eq!(
200221
builder.headers.get("Authentication").unwrap(),
201222
HeaderValue::from_static("Bearer $Up3rS3crET")
@@ -204,8 +225,7 @@ mod tests {
204225

205226
#[test]
206227
fn select_assert_query() {
207-
let client = Postgrest::new(REST_URL);
208-
let builder = client.from("users").select("some_table");
228+
let builder = Builder::new(TABLE_URL, None).select("some_table");
209229
assert_eq!(builder.method, Method::GET);
210230
assert_eq!(
211231
builder
@@ -217,8 +237,7 @@ mod tests {
217237

218238
#[test]
219239
fn order_assert_query() {
220-
let client = Postgrest::new(REST_URL);
221-
let builder = client.from("users").order("id");
240+
let builder = Builder::new(TABLE_URL, None).order("id");
222241
assert_eq!(
223242
builder
224243
.queries
@@ -229,8 +248,7 @@ mod tests {
229248

230249
#[test]
231250
fn limit_assert_range_header() {
232-
let client = Postgrest::new(REST_URL);
233-
let builder = client.from("users").limit(20);
251+
let builder = Builder::new(TABLE_URL, None).limit(20);
234252
assert_eq!(
235253
builder.headers.get("Range").unwrap(),
236254
HeaderValue::from_static("0-19")
@@ -239,8 +257,7 @@ mod tests {
239257

240258
#[test]
241259
fn range_assert_range_header() {
242-
let client = Postgrest::new(REST_URL);
243-
let builder = client.from("users").range(10, 20);
260+
let builder = Builder::new(TABLE_URL, None).range(10, 20);
244261
assert_eq!(
245262
builder.headers.get("Range").unwrap(),
246263
HeaderValue::from_static("10-20")
@@ -249,28 +266,16 @@ mod tests {
249266

250267
#[test]
251268
fn single_assert_accept_header() {
252-
let client = Postgrest::new(REST_URL);
253-
let builder = client.from("users").single();
269+
let builder = Builder::new(TABLE_URL, None).single();
254270
assert_eq!(
255271
builder.headers.get("Accept").unwrap(),
256272
HeaderValue::from_static("application/vnd.pgrst.object+json")
257273
);
258274
}
259275

260-
#[test]
261-
fn insert_csv_assert_content_type() {
262-
let client = Postgrest::new(REST_URL);
263-
let builder = client.from("users").insert_csv("ignored");
264-
assert_eq!(
265-
builder.headers.get("Content-Type").unwrap(),
266-
HeaderValue::from_static("text/csv")
267-
);
268-
}
269-
270276
#[test]
271277
fn upsert_assert_prefer_header() {
272-
let client = Postgrest::new(REST_URL);
273-
let builder = client.from("users").upsert("ignored");
278+
let builder = Builder::new(TABLE_URL, None).upsert("ignored");
274279
assert_eq!(
275280
builder.headers.get("Prefer").unwrap(),
276281
HeaderValue::from_static("return=representation; resolution=merge-duplicates")
@@ -279,29 +284,22 @@ mod tests {
279284

280285
#[test]
281286
fn single_upsert_assert_prefer_header() {
282-
let client = Postgrest::new(REST_URL);
283-
let builder = client
284-
.from("users")
285-
.single_upsert("ignored", "ignored", "ignored");
287+
let builder = Builder::new(TABLE_URL, None).single_upsert("ignored", "ignored", "ignored");
286288
assert_eq!(
287289
builder.headers.get("Prefer").unwrap(),
288290
HeaderValue::from_static("return=representation")
289291
);
290292
}
291293

292-
// filters...
293-
294294
#[test]
295295
fn not_rpc_should_not_have_flag() {
296-
let client = Postgrest::new(REST_URL);
297-
let builder = client.from("users").select("column");
296+
let builder = Builder::new(TABLE_URL, None).select("ignored");
298297
assert_eq!(builder.is_rpc, false);
299298
}
300299

301300
#[test]
302301
fn rpc_should_have_body_and_flag() {
303-
let client = Postgrest::new(REST_URL);
304-
let builder = client.from("users").rpc("{\"a\": 1, \"b\": 2}");
302+
let builder = Builder::new(RPC_URL, None).rpc("{\"a\": 1, \"b\": 2}");
305303
assert_eq!(builder.body.unwrap(), "{\"a\": 1, \"b\": 2}");
306304
assert_eq!(builder.is_rpc, true);
307305
}

src/lib.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,39 @@ pub struct Postgrest {
99
}
1010

1111
impl Postgrest {
12-
pub fn new(url: &str) -> Self {
12+
pub fn new<T>(url: T) -> Self
13+
where
14+
T: Into<String>,
15+
{
1316
Postgrest {
14-
url: url.to_string(),
17+
url: url.into(),
1518
schema: None,
1619
}
1720
}
1821

19-
pub fn schema(mut self, schema: &str) -> Self {
20-
self.schema = Some(schema.to_string());
22+
pub fn schema<T>(mut self, schema: T) -> Self
23+
where
24+
T: Into<String>,
25+
{
26+
self.schema = Some(schema.into());
2127
self
2228
}
2329

24-
pub fn from(&self, table: &str) -> Builder {
25-
let url = format!("{}/{}", self.url, table);
26-
Builder::new(&url, self.schema.clone())
30+
pub fn from<T>(&self, table: T) -> Builder
31+
where
32+
T: Into<String>,
33+
{
34+
let url = format!("{}/{}", self.url, table.into());
35+
Builder::new(url, self.schema.clone())
2736
}
2837

29-
pub fn rpc(&self, function: &str, params: &str) -> Builder {
30-
let url = format!("{}/rpc/{}", self.url, function);
31-
Builder::new(&url, self.schema.clone()).rpc(params)
38+
pub fn rpc<T, U>(&self, function: T, params: U) -> Builder
39+
where
40+
T: Into<String>,
41+
U: Into<String>,
42+
{
43+
let url = format!("{}/rpc/{}", self.url, function.into());
44+
Builder::new(url, self.schema.clone()).rpc(params)
3245
}
3346
}
3447

0 commit comments

Comments
 (0)