summaryrefslogtreecommitdiff
path: root/src/with_index.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/with_index.rs')
-rw-r--r--src/with_index.rs9
1 files changed, 9 insertions, 0 deletions
diff --git a/src/with_index.rs b/src/with_index.rs
index 90e06be..adce169 100644
--- a/src/with_index.rs
+++ b/src/with_index.rs
@@ -3,12 +3,16 @@ use std::fmt::{Debug, Display};
use anyhow::{bail, Result};
use reqwest::Response;
+/// Wraps the returned value of an [API call with blocking
+/// possibility](https://developer.hashicorp.com/consul/api-docs/features/blocking) with the
+/// returned Consul index
pub struct WithIndex<T> {
value: T,
index: usize,
}
impl<T> WithIndex<T> {
+ /// (for internal use, mostly)
pub fn index_from(resp: &Response) -> Result<WithIndexBuilder<T>> {
let index = match resp.headers().get("X-Consul-Index") {
Some(v) => v.to_str()?.parse::<usize>()?,
@@ -20,10 +24,13 @@ impl<T> WithIndex<T> {
})
}
+ /// Returns the inner value, discarding the index
pub fn into_inner(self) -> T {
self.value
}
+ /// Returns the Consul index, to be used in future calls to the same API endpoint to make them
+ /// blocking
pub fn index(&self) -> usize {
self.index
}
@@ -60,12 +67,14 @@ impl<T: Display> Display for WithIndex<T> {
}
}
+/// (for internal use, mostly)
pub struct WithIndexBuilder<T> {
_phantom: std::marker::PhantomData<T>,
index: usize,
}
impl<T> WithIndexBuilder<T> {
+ /// (for internal use, mostly)
pub fn value(self, value: T) -> WithIndex<T> {
WithIndex {
value,