# vic/deco architecture

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

- Interactive diagram: https://gitdiagram.com/vic/deco
- Repository: https://github.com/vic/deco

## Overview

##### Project type and purpose
- Deco is an open-source Elixir library that provides compile-time function decorators (around-advice) via a single macro. It is not a full-stack application but a developer tool / metaprogramming library.

##### File structure
- Top-level files:
- mix.exs / mix.lock – Mix project definitions and dependencies
- README.md, .travis.yml, LICENSE – documentation and CI config
- config/config.exs – standard Mix configuration
- lib/deco.ex – the public entry point defining `use Deco` and the `deco` macro
- lib/deco/core.ex – core logic that sequences decorators and transforms AST
- lib/deco/trace.ex – example decorator module (tracing)
- test/ – unit tests for the library

##### README for additional insights
- Describes the single macro `deco DECORATORS in FORM`
- Emphasizes minimalist core, zero runtime overhead (all at compile-time)
- Shows built-in convenience functions (trace, pipe_result, update_guard, around)
- Illustrates how decorators are just functions `Macro.t() -> Macro.t()`

a. Main components
- User Module (client code) that does `use Deco` and writes decorated functions
- Deco.Public API (lib/deco.ex) containing the `deco` macro
- Deco.Core (lib/deco/core.ex) which implements the decorator pipeline
- Built-in Decorators (lib/deco/trace.ex, and core helper functions: update_guard, pipe_result, around)
- Elixir Compiler / Mix – processes the AST emitted by the macros and produces BEAM bytecode
- External Service: Logger (used by the Trace decorator)

b. Relationships & interactions
1. At compile time, the user’s `deco … in def … end` invokes the macro in Deco.Public API.
2. The macro delegates to Deco.Core, passing in the AST of the function and the list of decorator functions.
3. Deco.Core iterates the decorators tuple, invoking each decorator function (e.g., Trace.trace/0, pipe_result/1, around/1) to transform the AST.
4. Deco.Core returns the final transformed AST to the compiler.
5. The Elixir compiler (via Mix) compiles the AST into BEAM code. If the Trace decorator is present, the resulting function will emit logs via the Logger module at runtime.
6. The test suite (in test/) runs the compiled functions to validate behavior.

c. Architectural patterns & design principles
- Metaprogramming via macros: all decoration happens at compile time to avoid run-time overhead
- Functional composition: decorators are plain functions that transform AST, composed in a pipeline
- Single responsibility: one macro in deco.ex, core pipeline in core.ex, each decorator in its own module/function

d. Key technologies & libraries
- Elixir Macros (Macro.t AST transformations)
- Mix build tool (mix.exs)
- Logger (for Trace decorator)
- ExUnit (test framework in test/)

##### Diagramming suggestions
- Use clear labels for each component: “User Module,” “Deco Macro (lib/deco.ex),” “Decorator Pipeline (lib/deco/core.ex),” “Decorator Functions,” “Elixir Compiler,” “Logger.”
- from User Module → Deco Macro → Decorator Pipeline → Elixir Compiler
- from Decorator Pipeline → each Decorator Function → back to Decorator Pipeline
- from Decorated Functions at runtime → Logger (if Trace is used)
- Core engine (Decorator Pipeline) in another
- Decorators (trace, pipe_result, around) in a third

##### Detail without over-segmentation
- Compile-time AST transformation flow end-to-end
- How user code maps into the library and out to compiled BEAM code
- Test suite as a verification component, but keep the focus on the decorator mechanism

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Code Layer
    UserModule["User Module"]:::user
    Docs["README.md & LICENSE"]:::dev

    %% Public API
    DecoMacro["Deco Macro (lib/deco.ex)"]:::public
    DecoratorPipeline["Decorator Pipeline (lib/deco/core.ex)"]:::core

    %% Decorators
    subgraph "Built-in Decorators"
        Trace["Trace Decorator"]:::decor
        Pipe["pipe_result"]:::decor
        Around["around"]:::decor
        Update["update_guard"]:::decor
    end

    %% Compilation & Runtime
    Compiler["Elixir Compiler (Mix)"]:::external
    Runtime["Runtime (BEAM)"]:::external
    Logger["Logger (External Module)"]:::external

    %% Development Tools
    MixDef["mix.exs & mix.lock"]:::dev
    Config["config/config.exs"]:::dev
    CI[".travis.yml"]:::dev
    TestSuite["Test Suite (test/)"]:::dev

    %% Flow
    UserModule -->|"use Deco & write decorated functions"| DecoMacro
    Docs -->|docs for users| UserModule
    DecoMacro -->|"delegates AST & decorators"| DecoratorPipeline
    DecoratorPipeline -->|"applies decorator"| Trace
    DecoratorPipeline -->|"applies decorator"| Pipe
    DecoratorPipeline -->|"applies decorator"| Around
    DecoratorPipeline -->|"applies decorator"| Update
    Trace --> DecoratorPipeline
    Pipe --> DecoratorPipeline
    Around --> DecoratorPipeline
    Update --> DecoratorPipeline
    DecoratorPipeline -->|"returns transformed AST"| Compiler
    MixDef --> Compiler
    Config --> Compiler
    Compiler -->|"generates BEAM bytecode"| Runtime
    Runtime -->|"if Trace present"| Logger
    Compiler -->|runs tests| TestSuite
    CI --> TestSuite

    %% Click Events
    click DecoMacro "https://github.com/vic/deco/blob/master/lib/deco.ex"
    click DecoratorPipeline "https://github.com/vic/deco/blob/master/lib/deco/core.ex"
    click Trace "https://github.com/vic/deco/blob/master/lib/deco/trace.ex"
    click MixDef "https://github.com/vic/deco/blob/master/mix.exs"
    click Config "https://github.com/vic/deco/blob/master/config/config.exs"
    click TestSuite "https://github.com/vic/deco/blob/master/test/deco_test.exs"
    click CI "https://github.com/vic/deco/blob/master/.travis.yml"
    click Docs "https://github.com/vic/deco/blob/master/README.md"

    %% Styles
    classDef user fill:#E8F0FE,stroke:#3182CE,color:#1A202C;
    classDef public fill:#E6FFFA,stroke:#2C7A7B,color:#1A202C;
    classDef core fill:#FFF5F5,stroke:#C53030,color:#1A202C;
    classDef decor fill:#FFF7EC,stroke:#DD6B20,color:#1A202C;
    classDef external fill:#EDF2F7,stroke:#4A5568,color:#1A202C,stroke-dasharray: 5 5;
    classDef dev fill:#F0FFF4,stroke:#005F73,color:#1A202C;
```
