# アーキテクチャ構成図

## システム全体構成図

```mermaid
graph TB
    subgraph "External Systems"
        User[User/Terminal]
        IDE[IDE/Editor]
        Plugins[External Plugins]
        ExtCmd[External Commands]
        FS[File System]
        Network[Network/HTTP]
        SQLite[(SQLite)]
    end

    subgraph "Nushell Core"
        subgraph "Presentation Layer"
            Main[main.rs<br/>Entry Point]
            CLI[nu-cli<br/>REPL/Completions]
            Explore[nu-explore<br/>Interactive Viewer]
            LSP[nu-lsp<br/>Language Server]
        end

        subgraph "Application Layer"
            Engine[nu-engine<br/>Evaluator]
            CmdLang[nu-cmd-lang<br/>Language Commands]
            CmdBase[nu-cmd-base<br/>Command Base]
        end

        subgraph "Domain Layer"
            Protocol[nu-protocol<br/>AST/Types/Values]
            Parser[nu-parser<br/>Lexer/Parser]
        end

        subgraph "Infrastructure Layer"
            Command[nu-command<br/>Built-in Commands]
            CmdExtra[nu-cmd-extra<br/>Extra Commands]
            Std[nu-std<br/>Standard Library]
        end

        subgraph "Plugin Layer"
            Plugin[nu-plugin<br/>Plugin SDK]
            PluginEngine[nu-plugin-engine<br/>Plugin Runtime]
            PluginProto[nu-plugin-protocol<br/>Protocol Definitions]
        end

        subgraph "Utility Layer"
            Utils[nu-utils<br/>Utilities]
            Path[nu-path<br/>Path Handling]
            Table[nu-table<br/>Table Rendering]
            JSON[nu-json<br/>JSON Support]
        end
    end

    User --> Main
    IDE --> LSP
    Main --> CLI
    Main --> Engine
    CLI --> Engine
    Explore --> Protocol
    LSP --> Parser
    LSP --> Protocol

    Engine --> Protocol
    Engine --> Parser
    CmdLang --> Protocol
    CmdBase --> Protocol

    Parser --> Protocol

    Command --> Protocol
    Command --> Engine
    CmdExtra --> Protocol
    Std --> Protocol

    Plugin --> Protocol
    PluginEngine --> PluginProto
    PluginEngine --> Plugin
    PluginProto --> Protocol

    Utils --> Protocol
    Path --> Protocol
    Table --> Protocol
    JSON --> Protocol

    Command --> FS
    Command --> Network
    Command --> SQLite
    Engine --> Plugins
    PluginEngine --> Plugins
    Engine --> ExtCmd
```

## レイヤー依存関係図

```mermaid
graph TD
    subgraph "Layer Dependencies"
        A[Presentation Layer<br/>nu-cli, src/main.rs, nu-lsp, nu-explore]
        B[Application Layer<br/>nu-engine, nu-cmd-lang, nu-cmd-base]
        C[Domain Layer<br/>nu-protocol, nu-parser]
        D[Infrastructure Layer<br/>nu-command, nu-cmd-extra, nu-std]
        E[Plugin Layer<br/>nu-plugin, nu-plugin-engine]
        F[Utility Layer<br/>nu-utils, nu-path, nu-table, nu-json]
    end

    A --> B
    A --> C
    B --> C
    D --> C
    D --> B
    E --> C
    F --> C
```

## データフロー図

```mermaid
flowchart LR
    subgraph Input
        UserInput[User Input]
        Script[Script File]
        Stdin[stdin]
    end

    subgraph "Processing Pipeline"
        Lex[Lexer<br/>Token Stream]
        Parse[Parser<br/>AST/Block]
        Compile[IR Compiler<br/>IrBlock]
        Eval[Evaluator<br/>PipelineData]
        Cmd1[Command 1]
        Cmd2[Command 2]
        CmdN[Command N]
    end

    subgraph Output
        Table[Table Formatter]
        JSON[JSON Output]
        Text[Text Output]
        File[File Output]
    end

    UserInput --> Lex
    Script --> Lex
    Stdin --> Lex
    Lex --> Parse
    Parse --> Compile
    Compile --> Eval
    Eval --> Cmd1
    Cmd1 -->|PipelineData| Cmd2
    Cmd2 -->|PipelineData| CmdN
    CmdN --> Table
    CmdN --> JSON
    CmdN --> Text
    CmdN --> File
```

## コンポーネント構成図

```mermaid
graph TB
    subgraph "nu-protocol"
        AST[ast/<br/>Abstract Syntax Tree]
        Values[value/<br/>Value Types]
        Engine_State[engine/<br/>EngineState, Stack]
        Pipeline[pipeline/<br/>PipelineData]
        Config[config/<br/>Configuration]
        Errors[errors/<br/>ShellError]
        IR[ir/<br/>Intermediate Representation]
    end

    subgraph "nu-parser"
        Lexer[lex.rs<br/>Tokenizer]
        ParserCore[parser.rs<br/>Parser Core]
        Keywords[parse_keywords.rs<br/>Keyword Parsing]
        Patterns[parse_patterns.rs<br/>Pattern Matching]
    end

    subgraph "nu-engine"
        Eval[eval.rs<br/>Expression Evaluator]
        EvalIR[eval_ir.rs<br/>IR Evaluator]
        Compile_Engine[compile.rs<br/>IR Compiler]
        Env[env.rs<br/>Environment]
        Closure[closure_eval.rs<br/>Closure Evaluation]
    end

    subgraph "nu-command"
        Filters[filters/<br/>where, select, each...]
        FileSystem[filesystem/<br/>ls, cd, cp, mv...]
        Strings[strings/<br/>split, str, lines...]
        Network_Cmd[network/<br/>http, fetch...]
        System[system/<br/>ps, exec, run...]
        Math[math/<br/>sum, avg, min, max...]
    end

    AST --> Values
    Engine_State --> Values
    Pipeline --> Values
    IR --> AST

    Lexer --> ParserCore
    ParserCore --> AST
    Keywords --> ParserCore
    Patterns --> ParserCore

    Eval --> Engine_State
    Eval --> AST
    EvalIR --> IR
    EvalIR --> Engine_State
    Compile_Engine --> IR
    Compile_Engine --> AST

    Filters --> Pipeline
    FileSystem --> Pipeline
    Strings --> Pipeline
    Network_Cmd --> Pipeline
    System --> Pipeline
    Math --> Pipeline
```

## プラグインアーキテクチャ図

```mermaid
sequenceDiagram
    participant Shell as Nushell Engine
    participant Engine as Plugin Engine
    participant Proto as Plugin Protocol
    participant Plugin as External Plugin

    Shell->>Engine: Load Plugin
    Engine->>Plugin: Spawn Process
    Engine->>Proto: Hello (Protocol Version)
    Plugin->>Proto: Hello Response
    Engine->>Proto: Signature Request
    Plugin->>Proto: Signature (Commands)
    Engine->>Shell: Register Commands

    Note over Shell,Plugin: Plugin Ready

    Shell->>Engine: Execute Command
    Engine->>Proto: Call (Run)
    Proto->>Plugin: PluginInput
    Plugin->>Plugin: Process Data
    Plugin->>Proto: PluginOutput (Stream)
    Proto->>Engine: PipelineData
    Engine->>Shell: Result

    Note over Shell,Plugin: Streaming Data

    loop Stream Processing
        Plugin->>Proto: Data (Value)
        Proto->>Engine: StreamData
        Engine->>Shell: Value
    end

    Plugin->>Proto: End (Stream)
    Proto->>Engine: StreamEnd
```

## エンジン状態管理図

```mermaid
stateDiagram-v2
    [*] --> Initialization
    Initialization --> ConfigLoading
    ConfigLoading --> REPLReady

    REPLReady --> Parsing: User Input
    Parsing --> Compiling: AST Generated
    Compiling --> Evaluating: IR Generated
    Evaluating --> CommandExecution: PipelineData
    CommandExecution --> REPLReady: Result

    CommandExecution --> PluginCall: Plugin Command
    PluginCall --> CommandExecution: Plugin Result

    REPLReady --> [*]: Exit

    state Parsing {
        [*] --> Lexing
        Lexing --> TokenStream
        TokenStream --> ASTGeneration
        ASTGeneration --> [*]
    }

    state Compiling {
        [*] --> IRGeneration
        IRGeneration --> BlockCompile
        BlockCompile --> [*]
    }

    state Evaluating {
        [*] --> StackSetup
        StackSetup --> ExpressionEval
        ExpressionEval --> PipelineProcess
        PipelineProcess --> [*]
    }
```

## Value型階層図

```mermaid
classDiagram
    class Value {
        <<enumeration>>
        Bool
        Int
        Float
        String
        Glob
        Filesize
        Duration
        Date
        Range
        Record
        List
        Closure
        Error
        Binary
        CellPath
        Custom
        Nothing
    }

    class PipelineData {
        <<enumeration>>
        Empty
        Value
        ListStream
        ByteStream
    }

    class Record {
        cols: Vec~String~
        vals: Vec~Value~
    }

    class CustomValue {
        <<trait>>
        +type_name() String
        +to_base_value() Value
        +clone_value() Value
    }

    class PluginCustomValue {
        name: String
        data: Vec~u8~
        source: PluginSource
    }

    Value --> PipelineData : contains
    Value --> Record : Record variant
    Value --> CustomValue : Custom variant
    CustomValue <|-- PluginCustomValue
```

## コマンド実行フロー図

```mermaid
flowchart TB
    subgraph "Command Execution"
        Input[Pipeline Input]
        Signature[Check Signature]
        Args[Parse Arguments]
        Run[Execute Run Method]
        Output[Pipeline Output]
    end

    subgraph "Command Trait"
        Name[name]
        SigMethod[signature]
        Usage[usage]
        RunMethod[run]
        Examples[examples]
    end

    Input --> Signature
    Signature --> Args
    Args --> Run
    Run --> Output

    SigMethod --> Signature
    RunMethod --> Run
```
