Skip to main content

ConfigValidator

Derive Macro ConfigValidator 

Source
#[derive(ConfigValidator)]
{
    // Attributes available to this derive:
    #[validate]
}
Expand description

Derive macro for implementing ConfigValidator trait.

This macro generates a ConfigValidator implementation for configuration structs. Validation rules are specified using the #[validate(...)] attribute on fields.

§Supported Validators

  • #[validate(range(min = value, max = value))] - Validates that the field value is within [min, max]
  • #[validate(min = value)] - Validates that the field value is at least value
  • #[validate(max = value)] - Validates that the field value is at most value
  • #[validate(optional_range(min = value, max = value))] - Like range, but for Option<T> fields
  • #[validate(path)] - Validates that the path exists (for PathBuf fields)
  • #[validate(optional_path)] - Like path, but for Option<PathBuf> fields

§Example

use oar_ocr_derive::ConfigValidator;

#[derive(ConfigValidator, Default)]
pub struct TextDetectionConfig {
    #[validate(range(min = 0.0, max = 1.0))]
    pub score_threshold: f32,

    #[validate(range(min = 0.0, max = 1.0))]
    pub box_threshold: f32,

    #[validate(min = 0.0)]
    pub unclip_ratio: f32,

    #[validate(min = 1)]
    pub max_candidates: usize,

    // Fields without #[validate] are not validated
    pub limit_side_len: Option<u32>,
}