feat: add convenience `parsed_path!` marco
@@ -132,6 +132,46 @@ impl DirsAndFileName {
}
}
+/// Short-cut macro to create [`DirsAndFileName`] instances.
+///
+/// # Example
+/// ```
+/// use object_store::parsed_path;
+///
+/// // empty path
+/// parsed_path!();
+///
+/// // filename only
+/// parsed_path!("test.txt");
+///
+/// // directories only
+/// parsed_path!(["path", "to"]);
+///
+/// // filename + directories
+/// parsed_path!(["path", "to"], "test.txt");
+/// ```
+#[macro_export]
+macro_rules! parsed_path {
+ ([$($dir:expr),*], $file:expr) => {
+ $crate::path::parsed::DirsAndFileName {
+ directories: vec![$($crate::path::parts::PathPart::from($dir)),*],
+ file_name: Some($crate::path::parts::PathPart::from($file)),
+ }
+ };
+ ([$($dir:expr),*]) => {
+ $crate::path::parsed::DirsAndFileName {
+ directories: vec![$($crate::path::parts::PathPart::from($dir)),*],
+ file_name: None,
+ }
+ };
+ ($file:expr) => {
+ parsed_path!([], $file)
+ };
+ () => {
+ parsed_path!([])
+ };
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -295,4 +335,49 @@ mod tests {
needle
);
}
+
+ #[test]
+ fn test_macro() {
+ let actual = parsed_path!(["foo", "bar"], "baz");
+ let expected = DirsAndFileName {
+ directories: vec![PathPart::from("foo"), PathPart::from("bar")],
+ file_name: Some(PathPart::from("baz")),
+ };
+ assert_eq!(actual, expected);
+
+ let actual = parsed_path!([], "foo");
+ let expected = DirsAndFileName {
+ directories: vec![],
+ file_name: Some(PathPart::from("foo")),
+ };
+ assert_eq!(actual, expected);
+
+ let actual = parsed_path!("foo");
+ let expected = DirsAndFileName {
+ directories: vec![],
+ file_name: Some(PathPart::from("foo")),
+ };
+ assert_eq!(actual, expected);
+
+ let actual = parsed_path!(["foo", "bar"]);
+ let expected = DirsAndFileName {
+ directories: vec![PathPart::from("foo"), PathPart::from("bar")],
+ file_name: None,
+ };
+ assert_eq!(actual, expected);
+
+ let actual = parsed_path!([]);
+ let expected = DirsAndFileName {
+ directories: vec![],
+ file_name: None,
+ };
+ assert_eq!(actual, expected);
+
+ let actual = parsed_path!();
+ let expected = DirsAndFileName {
+ directories: vec![],
+ file_name: None,
+ };
+ assert_eq!(actual, expected);
+ }
}
