Skip to content

Commit 14c99f0

Browse files
committed
feat: add Client::{get_option, set_option}
1 parent 95200ac commit 14c99f0

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

src/insert.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<T> Insert<T> {
129129
/// If called after the request is started, e.g., after [`Insert::write`].
130130
#[track_caller]
131131
pub fn with_option(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
132-
self.insert.expect_client_mut().add_option(name, value);
132+
self.insert.expect_client_mut().set_option(name, value);
133133
self
134134
}
135135

src/insert_formatted.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl InsertFormatted {
195195
/// If called after the request is started, e.g., after [`InsertFormatted::send`].
196196
#[track_caller]
197197
pub fn with_option(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
198-
self.state.expect_client_mut().add_option(name, value);
198+
self.state.expect_client_mut().set_option(name, value);
199199
self
200200
}
201201

src/inserter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ where
213213
/// If you have already begun writing data, you may call [`Inserter::force_commit`]
214214
/// to end the current `INSERT` so this takes effect on the next call to [`Inserter::write`].
215215
pub fn with_option(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
216-
self.client.add_option(name, value);
216+
self.client.set_option(name, value);
217217
self
218218
}
219219

src/lib.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,22 @@ impl Client {
383383
self
384384
}
385385

386+
/// Set an option on this instance of [`Client`].
387+
///
388+
/// Returns the previous value for the option, if one was set.
389+
pub fn set_option(
390+
&mut self,
391+
name: impl Into<String>,
392+
value: impl Into<String>,
393+
) -> Option<String> {
394+
self.options.insert(name.into(), value.into())
395+
}
396+
397+
/// Get an option that was previously set on this `Client`.
398+
pub fn get_option(&mut self, name: impl AsRef<str>) -> Option<&str> {
399+
self.options.get(name.as_ref()).map(String::as_str)
400+
}
401+
386402
/// Starts a new INSERT statement.
387403
///
388404
/// # Validation
@@ -503,12 +519,6 @@ impl Client {
503519
self.validation
504520
}
505521

506-
/// Used internally to modify the options map of an _already cloned_
507-
/// [`Client`] instance.
508-
pub(crate) fn add_option(&mut self, name: impl Into<String>, value: impl Into<String>) {
509-
self.options.insert(name.into(), value.into());
510-
}
511-
512522
pub(crate) fn set_roles(&mut self, roles: impl IntoIterator<Item = impl Into<String>>) {
513523
self.clear_roles();
514524
self.roles.extend(roles.into_iter().map(Into::into));
@@ -823,4 +833,16 @@ mod client_tests {
823833
let client = client.with_option("async_insert", "1");
824834
assert_ne!(client.options, client_clone.options,);
825835
}
836+
837+
#[test]
838+
fn it_gets_options() {
839+
let mut client = Client::default();
840+
841+
client.set_option("foo", "foo");
842+
client.set_option("bar", "bar");
843+
844+
assert_eq!(client.get_option("foo"), Some("foo"));
845+
assert_eq!(client.get_option("bar"), Some("bar"));
846+
assert_eq!(client.get_option("baz"), None);
847+
}
826848
}

src/query.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl Query {
236236

237237
/// Similar to [`Client::with_option`], but for this particular query only.
238238
pub fn with_option(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
239-
self.client.add_option(name, value);
239+
self.client.set_option(name, value);
240240
self
241241
}
242242

0 commit comments

Comments
 (0)