# jopohl/sigma-rust architecture

> Architecture diagram of the GitHub repository jopohl/sigma-rust, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jopohl/sigma-rust
- Repository: https://github.com/jopohl/sigma-rust

## Overview

- Parser & Lexer:
- Modules such as src/detection/lexer.rs and src/detection/ast.rs indicate the use of a lexer (tokenizer) and an abstract syntax tree builder. This is the entry point for parsing the rule syntax.
- Rule Evaluation Engine:
- Core modules such as src/rule.rs, src/detection.rs, and src/lib.rs orchestrate the rule evaluation logic using Pratt parsing and strong type checking.
- Field Management:
- Components under src/field (including modifier.rs, transformation.rs, value.rs) encapsulate how fields and their modifications are processed.
- Error Handling:
- src/error.rs manages error output during parsing and rule matching.
- Event Representation:
- Files like src/event.rs model how input events (e.g., JSON events) are represented and accessed for rule matching.
- Utility Components:
- Base value support (src/basevalue.rs) and selection logic (src/selection.rs) enhance the robustness of matching and type integrity.
- Wildcard Support:
- src/wildcard.rs encapsulates functionality to handle string wildcards in σ‑rules.

##### Understand the Relationships and Interactions
- The Lexer feeds tokens into an AST builder found under the detection subdirectory.
- The parsed AST is used by the rule evaluation engine to construct rule objects and then interact with events.
- Event data flows into the evaluation routines where field access (and modifiers) come into play for an accurate match.
- Error handling is interleaved throughout these processes to ensure that misformatted inputs or incompatible types are caught.
- The library’s API (exposed via lib.rs) allows external consumers (see examples/ and benches/) to feed in rules and event data.

##### Architectural Patterns and Design Principles
- Modular design: the separation of concerns is clear, with dedicated modules for parsing, rule definition, field handling, and error management.
- Extensibility: the design anticipates adding more modifiers or field types in future enhancements.
- Testability: the inclusion of tests and benchmarks indicates a commitment to correctness and performance.

##### Relevant Technologies & Tools
- Written in Rust: note that all components adhere to Rust’s safety and concurrency principles.
- CI/CD: highlight the integration with GitHub Workflows (in .github/workflows) for continuous integration and security audits.

##### Final Advice
- That any external interactions (APIs exposed to the user, integration with test cases) are clearly noted.

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TB
    A["Input: Rule YAML / Test Events"]:::input

    subgraph "Parsing"
        B["Lexer"]:::parser
        C["AST Builder"]:::parser
    end

    subgraph "Rule Evaluation Engine"
        D["Rule Logic"]:::engine
        E["Rule Processor"]:::engine
        F["Library API"]:::engine
    end

    subgraph "Field Processing"
        G["Field Module"]:::field
        H["Field Modifier"]:::field
        I["Field Transformation"]:::field
        J["Field Value"]:::field
    end

    K["Event Model"]:::event
    L["Error Handler"]:::error

    subgraph "Utility Components"
        M["Base Value"]:::utility
        N["Selection Logic"]:::utility
    end

    O["Wildcard Handler"]:::wildcard
    P["CI/CD Pipeline"]:::cicd
    Q["Strong Type Checking"]:::crosscut

    %% Main data flow
    A -->|"input"| B
    B -->|"token_stream"| C
    C -->|"parsed_AST"| D
    D -->|"process_rules"| E
    E -->|"evaluate_rules"| F
    K -->|"event_input"| F
    E -->|"field_lookup"| G
    H -->|"apply_modifier"| G
    I -->|"transform_field"| G
    J -->|"provide_value"| G
    D -->|"utilizes"| M
    D -->|"utilizes"| N
    D -->|"handles_wildcards"| O

    %% Error handling flows
    B -- "error" --> L
    C -- "error" --> L
    D -- "error" --> L
    G -- "error" --> L

    %% External integrations
    P --- F

    %% Cross-cutting concerns
    Q --- D
    Q --- G

    %% Styling
    classDef input fill:#f9f,stroke:#333,stroke-width:2px;
    classDef parser fill:#ccf,stroke:#333,stroke-width:2px;
    classDef engine fill:#cfc,stroke:#333,stroke-width:2px;
    classDef field fill:#ffc,stroke:#333,stroke-width:2px;
    classDef event fill:#fdd,stroke:#333,stroke-width:2px;
    classDef error fill:#fcc,stroke:#333,stroke-width:2px;
    classDef utility fill:#d9f,stroke:#333,stroke-width:2px;
    classDef wildcard fill:#c9f,stroke:#333,stroke-width:2px;
    classDef cicd fill:#eee,stroke:#333,stroke-width:2px;
    classDef crosscut fill:#ddd,stroke:#333,stroke-dasharray: 5,5;

    %% Click events for component mappings
    click B "https://github.com/jopohl/sigma-rust/blob/master/src/detection/lexer.rs"
    click C "https://github.com/jopohl/sigma-rust/blob/master/src/detection/ast.rs"
    click D "https://github.com/jopohl/sigma-rust/blob/master/src/rule.rs"
    click E "https://github.com/jopohl/sigma-rust/blob/master/src/detection.rs"
    click F "https://github.com/jopohl/sigma-rust/blob/master/src/lib.rs"
    click G "https://github.com/jopohl/sigma-rust/blob/master/src/field.rs"
    click H "https://github.com/jopohl/sigma-rust/blob/master/src/field/modifier.rs"
    click I "https://github.com/jopohl/sigma-rust/blob/master/src/field/transformation.rs"
    click J "https://github.com/jopohl/sigma-rust/blob/master/src/field/value.rs"
    click L "https://github.com/jopohl/sigma-rust/blob/master/src/error.rs"
    click K "https://github.com/jopohl/sigma-rust/blob/master/src/event.rs"
    click M "https://github.com/jopohl/sigma-rust/blob/master/src/basevalue.rs"
    click N "https://github.com/jopohl/sigma-rust/blob/master/src/selection.rs"
    click O "https://github.com/jopohl/sigma-rust/blob/master/src/wildcard.rs"
```
