Skip to content

Commit

Permalink
Refactor Registry::new_custom() to accept AsRef<str> instead of `…
Browse files Browse the repository at this point in the history
…String` for improved flexibility

Signed-off-by: Jerry Wang <j3rry.wan9@gmail.com>
  • Loading branch information
j3rrywan9 committed Jun 2, 2024
1 parent f0c9bc2 commit a1f6e2f
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,34 @@ impl Registry {
}

/// Create a new registry, with optional custom prefix and labels.
pub fn new_custom(
prefix: Option<String>,
labels: Option<HashMap<String, String>>,
) -> Result<Registry> {
pub fn new_custom<T, U>(prefix: Option<T>, labels: Option<HashMap<U, U>>) -> Result<Registry>
where
T: AsRef<str>,
U: AsRef<str>,
{
if let Some(ref namespace) = prefix {
if namespace.is_empty() {
if namespace.as_ref().is_empty() {
return Err(Error::Msg("empty prefix namespace".to_string()));
}
}

let reg = Registry::default();
{
let mut core = reg.r.write();
core.prefix = prefix;
core.labels = labels;

core.prefix = match prefix {
Some(s) => Some(s.as_ref().to_string()),
None => None,
};

core.labels = match labels {
Some(m) => Some(
m.into_iter()
.map(|(k, v)| (k.as_ref().to_string(), v.as_ref().to_string()))
.collect(),
),
None => None,
};
}
Ok(reg)
}
Expand Down

0 comments on commit a1f6e2f

Please sign in to comment.