Copilot commented on code in PR #2220:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2220#discussion_r3646721062


##########
minifi_rust/minifi_native/src/api/property.rs:
##########
@@ -44,69 +58,100 @@ pub struct Property {
     pub is_sensitive: bool,
     pub supports_expr_lang: bool,
     pub default_value: Option<&'static str>,
-    pub validator: StandardPropertyValidator,
-    pub allowed_values: &'static [&'static str],
-    pub allowed_type: Option<&'static str>,
+    pub constraints: PropertyConstraints,
 }
 
-pub trait GetProperty {
-    fn get_property(&self, property: &Property) -> Result<Option<String>, 
MinifiError>;
-    fn get_bool_property(&self, property: &Property) -> Result<Option<bool>, 
MinifiError> {
-        if property.validator != BoolValidator {
-            return Err(MinifiError::validation_err(format!(
-                "to use get_bool_property {:?} must have BoolValidator",
-                property
-            )));
-        }
+pub trait PropertyType {
+    type Output;
+    const EXPECTED_CONSTRAINTS: PropertyConstraints = 
PropertyConstraints::NoConstraints;
 
-        if let Some(property_val) = self.get_property(property)? {
-            Ok(Some(bool::from_str(&property_val)?))
-        } else {
-            Ok(None)
-        }
-    }
+    fn parse(s: &str) -> Result<Self::Output, MinifiError>;
+}
 
-    fn get_duration_property(&self, property: &Property) -> 
Result<Option<Duration>, MinifiError> {
-        if property.validator != TimePeriodValidator {
-            return Err(MinifiError::validation_err(format!(
-                "to use get_duration_property {:?} must have 
TimePeriodValidator",
-                property
-            )));
-        }
+macro_rules! impl_from_str_property {
+    ($t:ty, $validator:expr) => {
+        impl PropertyType for $t {
+            type Output = $t;
+            const EXPECTED_CONSTRAINTS: PropertyConstraints = $validator;
 
-        if let Some(property_val) = self.get_property(property)? {
-            Ok(Some(humantime::parse_duration(property_val.as_str())?))
-        } else {
-            Ok(None)
+            fn parse(s: &str) -> Result<Self::Output, MinifiError> {
+                s.parse::<$t>().map_err(Into::into)
+            }
         }
+    };
+    ($t:ty) => {
+        impl_from_str_property!($t, PropertyConstraints::NoConstraints);
+    };
+}
+
+impl_from_str_property!(String);
+impl_from_str_property!(std::path::PathBuf);
+impl_from_str_property!(f64);
+impl_from_str_property!(f32);
+impl_from_str_property!(i64);
+impl_from_str_property!(i32);
+impl_from_str_property!(bool, PropertyConstraints::Validator(BoolValidator));
+impl_from_str_property!(u64, PropertyConstraints::Validator(U64Validator));
+impl_from_str_property!(u32, PropertyConstraints::Validator(U64Validator));
+impl_from_str_property!(usize, PropertyConstraints::Validator(U64Validator));
+
+impl PropertyType for Duration {
+    type Output = Duration;
+    const EXPECTED_CONSTRAINTS: PropertyConstraints =
+        PropertyConstraints::Validator(TimePeriodValidator);
+    fn parse(s: &str) -> Result<Self::Output, MinifiError> {
+        humantime::parse_duration(s).map_err(Into::into)
+    }
+}
+
+pub struct DataSize;
+impl PropertyType for DataSize {
+    type Output = u64;
+    const EXPECTED_CONSTRAINTS: PropertyConstraints =
+        PropertyConstraints::Validator(DataSizeValidator);
+    fn parse(s: &str) -> Result<Self::Output, MinifiError> {
+        byte_unit::Byte::from_str(s)
+            .map(|b| b.as_u64())
+            .map_err(Into::into)
     }
+}
+
+pub trait GetProperty {
+    fn get_raw_property(&self, property: &Property) -> Result<Option<String>, 
MinifiError>;
 
-    fn get_size_property(&self, property: &Property) -> Result<Option<u64>, 
MinifiError> {
-        if property.validator != DataSizeValidator {
+    fn get_property<T: PropertyType>(
+        &self,
+        property: &Property,
+    ) -> Result<Option<T::Output>, MinifiError> {
+        if T::EXPECTED_CONSTRAINTS != NoConstraints
+            && T::EXPECTED_CONSTRAINTS != property.constraints
+        {

Review Comment:
   `property.constraints` is accessed by value from a borrowed `&Property`, 
which will not compile because it attempts to move a non-`Copy` field out of a 
shared reference. Compare constraints by reference (or make 
`PropertyConstraints` `Copy`).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to