Skip to content

Commit

Permalink
refactor: use anyhow::ensure and Result
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonlevi committed May 30, 2024
1 parent 2c5d453 commit 708212b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ddcommon-ffi/src/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub unsafe extern "C" fn ddog_Vec_Tag_push(
vec.push(tag);
PushTagResult::Ok
}
Err(err) => PushTagResult::Err(Error::from(err.as_ref())),
Err(err) => PushTagResult::Err(Error::from(err.to_string())),
}
}

Expand Down
21 changes: 9 additions & 12 deletions ddcommon/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Display for Tag {

impl Tag {
/// Validates a tag.
fn from_value<'a, IntoCow>(chunk: IntoCow) -> Result<Self, Cow<'static, str>>
fn from_value<'a, IntoCow>(chunk: IntoCow) -> anyhow::Result<Self>
where
IntoCow: Into<Cow<'a, str>>,
{
Expand All @@ -105,25 +105,22 @@ impl Tag {
* are likely to be errors (such as passed in empty string).
*/

if chunk.is_empty() {
return Err("tag is empty".into());
}
anyhow::ensure!(!chunk.is_empty(), "tag is empty");

let mut chars = chunk.chars();
if chars.next() == Some(':') {
return Err(format!("tag '{chunk}' begins with a colon").into());
}
if chars.last() == Some(':') {
return Err(format!("tag '{chunk}' ends with a colon").into());
}
anyhow::ensure!(
chars.next() != Some(':'),
"tag '{chunk}' begins with a colon"
);
anyhow::ensure!(chars.last() != Some(':'), "tag '{chunk}' ends with a colon");

let value = Cow::Owned(chunk.into_owned());
Ok(Tag { value })
}

/// Creates a tag from a key and value. It's preferred to use the `tag!`
/// macro when the key and value are both known at compile-time.
pub fn new<K, V>(key: K, value: V) -> Result<Self, Cow<'static, str>>
pub fn new<K, V>(key: K, value: V) -> anyhow::Result<Self>
where
K: AsRef<str>,
V: AsRef<str>,
Expand Down Expand Up @@ -160,7 +157,7 @@ pub fn parse_tags(str: &str) -> (Vec<Tag>, Option<String>) {
} else {
error_message += ", ";
}
error_message += err.as_ref();
error_message += &err.to_string();
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions sidecar/src/dogstatsd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ mod test {
use crate::dogstatsd::{create_client, Flusher};
#[cfg(unix)]
use ddcommon::connector::uds::socket_path_to_uri;
use ddcommon::tag::Tag;
use ddcommon::{tag, Endpoint};
use http::Uri;
use std::net;
Expand Down Expand Up @@ -194,11 +193,7 @@ mod test {
Distribution("test_distribution".to_string(), 4.2, vec![]),
Gauge("test_gauge".to_string(), 7.6, vec![]),
Histogram("test_histogram".to_string(), 8.0, vec![]),
Set(
"test_set".to_string(),
9,
vec![Tag::new("the", "end").unwrap()],
),
Set("test_set".to_string(), 9, vec![tag!("the", "end")]),
Set("test_neg_set".to_string(), -1, vec![]),
]);

Expand Down

0 comments on commit 708212b

Please sign in to comment.