Skip to main content

TaggedEnumSchema

Struct TaggedEnumSchema 

Source
pub struct TaggedEnumSchema<F>
where F: Fn(&str) -> Option<&'static [&'static str]>,
{ pub tag_field: &'static str, pub valid_tags: &'static [&'static str], pub fields_for_tag: F, pub enum_arrays: Vec<(&'static str, &'static [&'static str])>, pub nested_objects: Vec<(&'static str, &'static [&'static str])>, }
Expand description

Schema for a tagged enum (discriminated union)

Used for types with a discriminator field (e.g., tag: “type”, “kind”)

§Example

use fuzzy_parser::TaggedEnumSchema;

let schema = TaggedEnumSchema::new(
    "type",
    &["AddDerive", "RemoveDerive"],
    |tag| match tag {
        "AddDerive" | "RemoveDerive" => Some(&["target", "derives"][..]),
        _ => None,
    },
)
.with_enum_array("derives", &["Debug", "Clone", "Serialize"])
.with_nested_object("config", &["timeout", "retries"]);

Fields§

§tag_field: &'static str

The discriminator field name (e.g., “type”, “kind”)

§valid_tags: &'static [&'static str]

Valid tag values (e.g., [“AddDerive”, “RenameIdent”, …])

§fields_for_tag: F

Function to get valid fields for a given tag value

§enum_arrays: Vec<(&'static str, &'static [&'static str])>

Fields that contain arrays of enum values: (field_name, valid_values)

§nested_objects: Vec<(&'static str, &'static [&'static str])>

Fields that contain nested objects: (field_name, valid_fields)

Implementations§

Source§

impl<F> TaggedEnumSchema<F>
where F: Fn(&str) -> Option<&'static [&'static str]>,

Source

pub fn new( tag_field: &'static str, valid_tags: &'static [&'static str], fields_for_tag: F, ) -> TaggedEnumSchema<F>

Create a new tagged enum schema

Source

pub fn with_enum_array( self, field: &'static str, valid_values: &'static [&'static str], ) -> TaggedEnumSchema<F>

Add an enum array field for repair

Values in this array field will be fuzzy-matched against valid_values.

§Example
use fuzzy_parser::TaggedEnumSchema;

let schema = TaggedEnumSchema::new("type", &["AddDerive"], |_| Some(&["derives"][..]))
    .with_enum_array("derives", &["Debug", "Clone", "Serialize"]);
// Now "Debg" in derives array will be corrected to "Debug"
Source

pub fn with_nested_object( self, field: &'static str, valid_fields: &'static [&'static str], ) -> TaggedEnumSchema<F>

Add a nested object field for repair

Field names in this nested object will be fuzzy-matched against valid_fields.

§Example
use fuzzy_parser::TaggedEnumSchema;

let schema = TaggedEnumSchema::new("type", &["Configure"], |_| Some(&["config"][..]))
    .with_nested_object("config", &["timeout", "retries", "enabled"]);
// Now "timout" in config object will be corrected to "timeout"
Source

pub fn is_valid_tag(&self, tag: &str) -> bool

Check if a tag value is valid

Source

pub fn get_fields(&self, tag: &str) -> Option<&'static [&'static str]>

Get valid fields for a tag value

Source

pub fn get_enum_array_values( &self, field: &str, ) -> Option<&'static [&'static str]>

Get valid enum values for an array field

Source

pub fn get_nested_object_fields( &self, field: &str, ) -> Option<&'static [&'static str]>

Get valid fields for a nested object

Auto Trait Implementations§

§

impl<F> Freeze for TaggedEnumSchema<F>
where F: Freeze,

§

impl<F> RefUnwindSafe for TaggedEnumSchema<F>
where F: RefUnwindSafe,

§

impl<F> Send for TaggedEnumSchema<F>
where F: Send,

§

impl<F> Sync for TaggedEnumSchema<F>
where F: Sync,

§

impl<F> Unpin for TaggedEnumSchema<F>
where F: Unpin,

§

impl<F> UnwindSafe for TaggedEnumSchema<F>
where F: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,