From 708212bc99fe09898d6f48b29bf7881198604748 Mon Sep 17 00:00:00 2001 From: Levi Morrison Date: Thu, 30 May 2024 10:07:31 -0600 Subject: [PATCH] refactor: use anyhow::ensure and Result --- ddcommon-ffi/src/tags.rs | 2 +- ddcommon/src/tag.rs | 21 +++++++++------------ sidecar/src/dogstatsd.rs | 7 +------ 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/ddcommon-ffi/src/tags.rs b/ddcommon-ffi/src/tags.rs index df2799d12..d2bee8941 100644 --- a/ddcommon-ffi/src/tags.rs +++ b/ddcommon-ffi/src/tags.rs @@ -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())), } } diff --git a/ddcommon/src/tag.rs b/ddcommon/src/tag.rs index 54cf5a32b..17f2b691b 100644 --- a/ddcommon/src/tag.rs +++ b/ddcommon/src/tag.rs @@ -90,7 +90,7 @@ impl Display for Tag { impl Tag { /// Validates a tag. - fn from_value<'a, IntoCow>(chunk: IntoCow) -> Result> + fn from_value<'a, IntoCow>(chunk: IntoCow) -> anyhow::Result where IntoCow: Into>, { @@ -105,17 +105,14 @@ 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 }) @@ -123,7 +120,7 @@ impl Tag { /// 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(key: K, value: V) -> Result> + pub fn new(key: K, value: V) -> anyhow::Result where K: AsRef, V: AsRef, @@ -160,7 +157,7 @@ pub fn parse_tags(str: &str) -> (Vec, Option) { } else { error_message += ", "; } - error_message += err.as_ref(); + error_message += &err.to_string(); } } } diff --git a/sidecar/src/dogstatsd.rs b/sidecar/src/dogstatsd.rs index 2fe020aa9..65171ff5d 100644 --- a/sidecar/src/dogstatsd.rs +++ b/sidecar/src/dogstatsd.rs @@ -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; @@ -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![]), ]);