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;
Description: Defines
Optas a 32-bit unsigned integer.Purpose: Represents a set of bitflags combined into a single integer value.
Usage: Used throughout the codebase to pass or store combinations of options efficiently.
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;
Combines
SORT_KEYSandNON_STR_KEYSoptions for convenience.
pub(crate) const NOT_PASSTHROUGH: Opt =
!(PASSTHROUGH_DATETIME | PASSTHROUGH_DATACLASS | PASSTHROUGH_SUBCLASS);
A mask that excludes all passthrough flags.
Useful for filtering or asserting which options do not allow passthrough serialization.
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;
Represents the combined mask of all valid option bits.
Cast to signed 32-bit integer (
i32) for compatibility with APIs or functions expecting signed integers.Suppresses clippy lint about possible wrap due to the cast.
Usage Examples
Example: Combining Options
let opts: Opt = INDENT_2 | SORT_KEYS | PASSTHROUGH_DATETIME;
Combines indentation, key sorting, and passthrough datetime behavior.
Example: Checking for an Option
if opts & OMIT_MICROSECONDS != 0 {
// Handle serialization without microseconds
}
Checks if the
OMIT_MICROSECONDSflag is set in the options.
Example: Resetting Passthrough Flags
let filtered_opts = opts & NOT_PASSTHROUGH;
Clears all passthrough-related flags from the options.
Implementation Details
The use of bitflags allows efficient storage and manipulation of multiple boolean options within a single integer.
Each flag corresponds to a unique bit position, enabling bitwise operations (
|,&,!) for combining, checking, and masking.Deprecated constants (
SERIALIZE_DATACLASS,SERIALIZE_UUID) are set to zero to maintain backward compatibility but have no effect.The
MAX_OPTconstant serves as a comprehensive mask of all defined flags for validation or range checks.
Interaction with Other System Components
This file is likely imported and used by serialization modules to toggle behaviors such as formatting, key sorting, and type-specific serialization.
Other components that handle datetime, dataclasses, NumPy objects, or subclasses will refer to these flags to determine how to process or pass through these types.
The bitflag constants facilitate consistent option management across the codebase, ensuring all modules interpret serialization options uniformly.
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.