opt.rs


Overview

The `opt.rs` file defines a set of bitflag constants representing configuration options (`Opt`) used throughout the project. These options are encoded as 32-bit unsigned integers (`u32`), where each bit corresponds to a distinct feature toggle or serialization behavior. This design enables efficient combination, storage, and checking of multiple options using bitwise operations.

Primarily, these options control serialization behaviors, formatting preferences, and data handling policies for various data types like datetimes, dataclasses, and subclasses. The file also defines some compound constants for convenience and masks for filtering options.


Detailed Explanation

Type Alias

pub(crate) type Opt = u32;

Constants

Each constant corresponds to a single bit flag in the `Opt` type. The constants are `pub(crate)`, meaning they are public within the crate but not exposed outside.

Constant Name

Bit Value

Description

`INDENT_2`

`1`

Enables indentation with 2 spaces in serialized output.

`NAIVE_UTC`

`1 << 1`

Treats datetime objects as naive UTC dates.

`NON_STR_KEYS`

`1 << 2`

Allows dictionary keys that are not strings.

`OMIT_MICROSECONDS`

`1 << 3`

Omits microseconds in datetime serialization.

`SERIALIZE_NUMPY`

`1 << 4`

Serializes NumPy data types with special handling.

`SORT_KEYS`

`1 << 5`

Sorts dictionary keys during serialization.

`STRICT_INTEGER`

`1 << 6`

Enforces strict integer serialization rules.

`UTC_Z`

`1 << 7`

Uses 'Z' suffix for UTC times in ISO 8601 format.

`PASSTHROUGH_SUBCLASS`

`1 << 8`

Allows subclasses to be passed through serialization without modification.

`PASSTHROUGH_DATETIME`

`1 << 9`

Allows datetime objects to be passed through serialization untouched.

`APPEND_NEWLINE`

`1 << 10`

Appends a newline character at the end of the serialized output.

`PASSTHROUGH_DATACLASS`

`1 << 11`

Allows dataclasses to be passed through serialization without modification.

`SERIALIZE_DATACLASS`

`0`

**Deprecated:** No effect, previously used for dataclass serialization.

`SERIALIZE_UUID`

`0`

**Deprecated:** No effect, previously used for UUID serialization.


Compound and Mask Constants

pub(crate) const SORT_OR_NON_STR_KEYS: Opt = SORT_KEYS | NON_STR_KEYS;
pub(crate) const NOT_PASSTHROUGH: Opt =
    !(PASSTHROUGH_DATETIME | PASSTHROUGH_DATACLASS | PASSTHROUGH_SUBCLASS);

Maximum Options Value

#[allow(clippy::cast_possible_wrap)]
pub(crate) const MAX_OPT: i32 = (APPEND_NEWLINE
    | INDENT_2
    | NAIVE_UTC
    | NON_STR_KEYS
    | OMIT_MICROSECONDS
    | PASSTHROUGH_DATETIME
    | PASSTHROUGH_DATACLASS
    | PASSTHROUGH_SUBCLASS
    | SERIALIZE_DATACLASS
    | SERIALIZE_NUMPY
    | SERIALIZE_UUID
    | SORT_KEYS
    | STRICT_INTEGER
    | UTC_Z) as i32;

Usage Examples

Example: Combining Options

let opts: Opt = INDENT_2 | SORT_KEYS | PASSTHROUGH_DATETIME;

Example: Checking for an Option

if opts & OMIT_MICROSECONDS != 0 {
    // Handle serialization without microseconds
}

Example: Resetting Passthrough Flags

let filtered_opts = opts & NOT_PASSTHROUGH;

Implementation Details


Interaction with Other System Components


Visual Diagram

flowchart TD
    OptType[Opt = u32]

    OptType --> INDENT_2["INDENT_2 (1)"]
    OptType --> NAIVE_UTC["NAIVE_UTC (1 << 1)"]
    OptType --> NON_STR_KEYS["NON_STR_KEYS (1 << 2)"]
    OptType --> OMIT_MICROSECONDS["OMIT_MICROSECONDS (1 << 3)"]
    OptType --> SERIALIZE_NUMPY["SERIALIZE_NUMPY (1 << 4)"]
    OptType --> SORT_KEYS["SORT_KEYS (1 << 5)"]
    OptType --> STRICT_INTEGER["STRICT_INTEGER (1 << 6)"]
    OptType --> UTC_Z["UTC_Z (1 << 7)"]
    OptType --> PASSTHROUGH_SUBCLASS["PASSTHROUGH_SUBCLASS (1 << 8)"]
    OptType --> PASSTHROUGH_DATETIME["PASSTHROUGH_DATETIME (1 << 9)"]
    OptType --> APPEND_NEWLINE["APPEND_NEWLINE (1 << 10)"]
    OptType --> PASSTHROUGH_DATACLASS["PASSTHROUGH_DATACLASS (1 << 11)"]

    SORT_KEYS & NON_STR_KEYS --> SORT_OR_NON_STR_KEYS["SORT_OR_NON_STR_KEYS (SORT_KEYS | NON_STR_KEYS)"]

    PASSTHROUGH_DATETIME & PASSTHROUGH_DATACLASS & PASSTHROUGH_SUBCLASS --> PASSTHROUGH_FLAGS["Passthrough Flags Mask"]

    PASSTHROUGH_FLAGS --> NOT_PASSTHROUGH["NOT_PASSTHROUGH = !Passthrough Flags Mask"]

    AllFlags[All Flags Combined] --> MAX_OPT["MAX_OPT (All flags combined)"]

Summary

The `opt.rs` file centralizes serialization and formatting option flags as bitflags within a 32-bit integer. It facilitates flexible and efficient configuration management across the system, especially for serialization behaviors related to indentation, key handling, datetime formatting, and special type passthroughs. The design is minimalistic but crucial to maintain consistent and performant option handling throughout the crate.