From ff5d672583511f073183dee95e58682fbd143c09 Mon Sep 17 00:00:00 2001 From: "Lei, HUANG" <6406592+v0y4g3r@users.noreply.github.com> Date: Tue, 12 Aug 2025 21:07:01 +0800 Subject: [PATCH] chore: impl cast from primitives to PathType (#6724) chore/impl-cast-to-primitives-for-path-type: ### Add `num_enum` for Enum Conversion and Update `PathType` - **Added `num_enum` Dependency**: Updated `Cargo.lock` and `Cargo.toml` to include `num_enum` for enum conversion functionality. - Files: `Cargo.lock`, `src/store-api/Cargo.toml` - **Enhanced `PathType` Enum**: Implemented `TryFromPrimitive` for `PathType` to enable conversion from primitive types. - Files: `src/store-api/src/region_request.rs` - **Added Unit Tests**: Introduced tests to verify the conversion of `PathType` enum to and from primitive types. - Files: `src/store-api/src/region_request.rs` Signed-off-by: Lei, HUANG --- Cargo.lock | 1 + src/store-api/Cargo.toml | 1 + src/store-api/src/region_request.rs | 23 ++++++++++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 50ec6bb84e..a03d3d19d0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -12394,6 +12394,7 @@ dependencies = [ "futures", "humantime", "lazy_static", + "num_enum 0.7.3", "prometheus", "prost 0.13.5", "serde", diff --git a/src/store-api/Cargo.toml b/src/store-api/Cargo.toml index c6fce64e8c..92ef698c15 100644 --- a/src/store-api/Cargo.toml +++ b/src/store-api/Cargo.toml @@ -26,6 +26,7 @@ derive_builder.workspace = true futures.workspace = true humantime.workspace = true lazy_static.workspace = true +num_enum = "0.7" prometheus.workspace = true prost.workspace = true serde.workspace = true diff --git a/src/store-api/src/region_request.rs b/src/store-api/src/region_request.rs index 779e8ccefa..92c3c34ec1 100644 --- a/src/store-api/src/region_request.rs +++ b/src/store-api/src/region_request.rs @@ -36,6 +36,7 @@ use common_recordbatch::DfRecordBatch; use common_time::{TimeToLive, Timestamp}; use datatypes::prelude::ConcreteDataType; use datatypes::schema::{FulltextOptions, SkippingIndexOptions}; +use num_enum::TryFromPrimitive; use serde::{Deserialize, Serialize}; use snafu::{ensure, OptionExt, ResultExt}; use strum::{AsRefStr, IntoStaticStr}; @@ -56,7 +57,8 @@ use crate::path_utils::table_dir; use crate::storage::{ColumnId, RegionId, ScanRequest}; /// The type of path to generate. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, TryFromPrimitive)] +#[repr(u8)] pub enum PathType { /// A bare path - the original path of an engine. /// @@ -1958,4 +1960,23 @@ mod tests { }; kind.validate(&metadata).unwrap(); } + + #[test] + fn test_cast_path_type_to_primitive() { + assert_eq!(PathType::Bare as u8, 0); + assert_eq!(PathType::Data as u8, 1); + assert_eq!(PathType::Metadata as u8, 2); + assert_eq!( + PathType::try_from(PathType::Bare as u8).unwrap(), + PathType::Bare + ); + assert_eq!( + PathType::try_from(PathType::Data as u8).unwrap(), + PathType::Data + ); + assert_eq!( + PathType::try_from(PathType::Metadata as u8).unwrap(), + PathType::Metadata + ); + } }