Skip to content

Commit

Permalink
test: add small doctest to Operator::from_uri
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgehermo9 committed Dec 30, 2024
1 parent 539e6ea commit 3aba98b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/src/types/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub trait Configurator: Serialize + DeserializeOwned + Debug + 'static {
// TODO: we are not interpreting the host or path params
// the `let op = Operator::from_uri("fs:///tmp/test", vec![])?;` statement from the RFC wont work.
// instead we should use `let op = Operator::from_uri("fs://?root=/tmp/test", vec![])?;` as done
// in `ofs`
// in `ofs`. The `fs` service should override this default implementation if it wants to use the host or path params?

// TODO: should we merge it this way?
let merged_options = url_options.into_iter().chain(options);
Expand Down
8 changes: 8 additions & 0 deletions core/src/types/operator/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ impl Operator {
}

/// TODO: document this.
///
/// TODO: improve those examples
/// # Examples
/// ```
/// let op = Operator::from_uri("fs://?root=/tmp/test", vec![])?
///
/// Ok(())
/// ```
pub fn from_uri(
uri: &str,
options: impl IntoIterator<Item = (String, String)>,
Expand Down
11 changes: 7 additions & 4 deletions core/src/types/operator/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ impl OperatorRegistry {
uri: &str,
options: impl IntoIterator<Item = (String, String)>,
) -> Result<Operator> {
let parsed_uri = http::Uri::try_from(uri).map_err(|err| {
// TODO: we use the `url::Url` struct instead of `http:Uri`, because
// we needed it in `Configurator::from_uri` method.
let parsed_url = url::Url::parse(uri).map_err(|err| {
Error::new(ErrorKind::ConfigInvalid, "uri is invalid")
.with_context("uri", uri)
.set_source(err)
})?;

let scheme = parsed_uri.scheme_str().ok_or_else(|| {
Error::new(ErrorKind::ConfigInvalid, "uri is missing scheme").with_context("uri", uri)
})?;
// TODO: with the `url::Url` struct, we always have the scheme (it is not an Option<str>)
// but with the `http::Uri` crate, it can be missing https://docs.rs/http/latest/http/uri/struct.Uri.html#method.scheme
// which one should we use?
let scheme = parsed_url.scheme();

let factory = self.registry.get(scheme).ok_or_else(|| {
Error::new(
Expand Down

0 comments on commit 3aba98b

Please sign in to comment.