forjar 1.4.2

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! FJ-110: Language Server Protocol for forjar YAML.
//!
//! Lightweight LSP server over stdio using JSON-RPC 2.0.
//! Provides: autocompletion, hover docs, diagnostics (validation),
//! go-to-definition for includes/data sources.
//! No external LSP library — raw JSON-RPC protocol.

use std::collections::HashMap;
use std::io::{self, BufRead, Write};

/// LSP server state.
#[derive(Debug)]
pub struct LspServer {
    /// Whether the client has sent `initialize`.
    pub initialized: bool,
    /// Open document URIs mapped to their content.
    pub documents: HashMap<String, String>,
    /// Workspace root URI.
    pub root_uri: Option<String>,
    /// Whether a shutdown was requested.
    pub shutdown_requested: bool,
}

/// LSP diagnostic severity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum DiagnosticSeverity {
    /// Error severity.
    Error = 1,
    /// Warning severity.
    Warning = 2,
    /// Informational severity.
    Information = 3,
    /// Hint severity.
    Hint = 4,
}

/// LSP diagnostic.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Diagnostic {
    /// Start line (0-based).
    pub line: u32,
    /// Start character offset.
    pub character: u32,
    /// End line (0-based).
    pub end_line: u32,
    /// End character offset.
    pub end_character: u32,
    /// Diagnostic severity level.
    pub severity: DiagnosticSeverity,
    /// Diagnostic message.
    pub message: String,
    /// Source identifier (e.g., "forjar-lsp").
    pub source: String,
}

/// LSP completion item.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CompletionItem {
    /// Display label.
    pub label: String,
    /// Detail description.
    pub detail: String,
    /// Text to insert on accept.
    pub insert_text: String,
    /// Completion kind (1=Text, 6=Variable, 14=Keyword, 15=Snippet).
    pub kind: u32,
}

/// Known forjar YAML top-level keys.
const TOP_LEVEL_KEYS: &[(&str, &str)] = &[
    ("machines", "Machine definitions (name, host, transport)"),
    ("resources", "Resource definitions (package, file, service)"),
    ("data", "Data sources (file, forjar-state, environment)"),
    ("tags", "Tag assignments for resources"),
    ("policy", "Policy configuration (convergence, security)"),
    ("includes", "Include other forjar YAML files"),
    ("template", "Template parameters for reusable configs"),
];

/// Known resource types.
const RESOURCE_TYPES: &[(&str, &str)] = &[
    ("package", "System package (apt, yum, brew, cargo)"),
    ("file", "File resource (content, template, permissions)"),
    ("service", "System service (systemd, launchd)"),
    ("mount", "Filesystem mount point"),
    ("directory", "Directory with ownership/permissions"),
    ("command", "Arbitrary command execution"),
    ("git_repo", "Git repository clone/checkout"),
    ("cron", "Cron job definition"),
    ("user", "System user account"),
    ("group", "System group"),
    ("gpu_driver", "GPU driver installation (NVIDIA/ROCm)"),
];

/// Known resource fields.
const RESOURCE_FIELDS: &[(&str, &str)] = &[
    ("name", "Resource identifier (unique within config)"),
    ("type", "Resource type (package, file, service, etc.)"),
    (
        "ensure",
        "Desired state: present|absent|latest|running|stopped",
    ),
    ("provider", "Package provider: apt, yum, brew, cargo, pip"),
    ("content", "File content (inline string)"),
    ("source", "File source path or URL"),
    ("mode", "File permissions (octal, e.g., '0644')"),
    ("owner", "File/directory owner"),
    ("group", "File/directory group"),
    ("depends_on", "List of resource dependencies"),
    ("machine", "Target machine name"),
    ("tags", "Resource tags for filtering"),
    ("on_success", "Notification on successful convergence"),
    ("on_failure", "Notification on failure"),
    ("on_drift", "Notification when drift detected"),
];

impl Default for LspServer {
    fn default() -> Self {
        Self::new()
    }
}

impl LspServer {
    /// Create a new LSP server instance.
    pub fn new() -> Self {
        LspServer {
            initialized: false,
            documents: HashMap::new(),
            root_uri: None,
            shutdown_requested: false,
        }
    }

    /// Handle a JSON-RPC request and return optional response.
    pub fn handle_message(&mut self, msg: &serde_json::Value) -> Option<serde_json::Value> {
        let method = msg.get("method")?.as_str()?;
        let id = msg.get("id");

        match method {
            "initialize" => {
                self.initialized = true;
                if let Some(root) = msg.pointer("/params/rootUri") {
                    self.root_uri = root.as_str().map(String::from);
                }
                Some(make_response(id, initialize_result()))
            }
            "initialized" => None, // notification, no response
            "shutdown" => {
                self.shutdown_requested = true;
                Some(make_response(id, serde_json::Value::Null))
            }
            "exit" => std::process::exit(if self.shutdown_requested { 0 } else { 1 }),
            "textDocument/didOpen" => {
                self.handle_did_open(msg);
                None
            }
            "textDocument/didChange" => {
                self.handle_did_change(msg);
                None
            }
            "textDocument/completion" => {
                let items = self.handle_completion(msg);
                Some(make_response(
                    id,
                    serde_json::to_value(items).unwrap_or_default(),
                ))
            }
            "textDocument/hover" => {
                let hover = self.handle_hover(msg);
                Some(make_response(id, hover))
            }
            _ => {
                if id.is_some() {
                    Some(make_error_response(id, -32601, "Method not found"))
                } else {
                    None // unknown notification
                }
            }
        }
    }

    fn handle_did_open(&mut self, msg: &serde_json::Value) {
        if let (Some(uri), Some(text)) = (
            msg.pointer("/params/textDocument/uri")
                .and_then(|v| v.as_str()),
            msg.pointer("/params/textDocument/text")
                .and_then(|v| v.as_str()),
        ) {
            self.documents.insert(uri.to_string(), text.to_string());
        }
    }

    fn handle_did_change(&mut self, msg: &serde_json::Value) {
        if let (Some(uri), Some(changes)) = (
            msg.pointer("/params/textDocument/uri")
                .and_then(|v| v.as_str()),
            msg.pointer("/params/contentChanges")
                .and_then(|v| v.as_array()),
        ) {
            // Full document sync — take last change
            if let Some(text) = changes
                .last()
                .and_then(|c| c.get("text"))
                .and_then(|t| t.as_str())
            {
                self.documents.insert(uri.to_string(), text.to_string());
            }
        }
    }

    fn handle_completion(&self, msg: &serde_json::Value) -> Vec<CompletionItem> {
        let uri = msg
            .pointer("/params/textDocument/uri")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        let line = msg
            .pointer("/params/position/line")
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as usize;

        let doc = match self.documents.get(uri) {
            Some(d) => d,
            None => return Vec::new(),
        };
        let current_line = doc.lines().nth(line).unwrap_or("");
        let indent = current_line.len() - current_line.trim_start().len();
        completion_items(indent, current_line.trim())
    }

    fn handle_hover(&self, msg: &serde_json::Value) -> serde_json::Value {
        let uri = msg
            .pointer("/params/textDocument/uri")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        let line = msg
            .pointer("/params/position/line")
            .and_then(|v| v.as_u64())
            .unwrap_or(0) as usize;

        let doc = match self.documents.get(uri) {
            Some(d) => d,
            None => return serde_json::Value::Null,
        };
        let current_line = doc.lines().nth(line).unwrap_or("");
        hover_info(current_line.trim())
    }

    /// Validate a document and return diagnostics.
    pub fn validate_document(&self, uri: &str) -> Vec<Diagnostic> {
        let doc = match self.documents.get(uri) {
            Some(d) => d,
            None => return Vec::new(),
        };
        validate_yaml(doc)
    }
}

/// Generate completion items based on context.
pub(crate) fn completion_items(indent: usize, line_prefix: &str) -> Vec<CompletionItem> {
    let mut items = Vec::new();
    if indent == 0 && (line_prefix.is_empty() || !line_prefix.contains(':')) {
        // Top-level keys
        for (key, desc) in TOP_LEVEL_KEYS {
            items.push(CompletionItem {
                label: key.to_string(),
                detail: desc.to_string(),
                insert_text: format!("{key}:"),
                kind: 14, // Keyword
            });
        }
    } else if line_prefix.starts_with("type:") || line_prefix.starts_with("- type:") {
        // Resource types
        for (rtype, desc) in RESOURCE_TYPES {
            items.push(CompletionItem {
                label: rtype.to_string(),
                detail: desc.to_string(),
                insert_text: rtype.to_string(),
                kind: 6, // Variable
            });
        }
    } else if indent >= 2 {
        // Resource fields
        for (field, desc) in RESOURCE_FIELDS {
            items.push(CompletionItem {
                label: field.to_string(),
                detail: desc.to_string(),
                insert_text: format!("{field}: "),
                kind: 6, // Variable
            });
        }
    }
    items
}

/// Generate hover info for a line.
pub(super) fn hover_info(line: &str) -> serde_json::Value {
    let key = line
        .split(':')
        .next()
        .unwrap_or("")
        .trim()
        .trim_start_matches("- ");
    let desc = TOP_LEVEL_KEYS
        .iter()
        .chain(RESOURCE_FIELDS.iter())
        .chain(RESOURCE_TYPES.iter())
        .find(|(k, _)| *k == key)
        .map(|(_, d)| *d);

    match desc {
        Some(d) => serde_json::json!({
            "contents": { "kind": "markdown", "value": format!("**{key}**: {d}") }
        }),
        None => serde_json::Value::Null,
    }
}

/// Basic YAML validation for forjar configs.
pub fn validate_yaml(content: &str) -> Vec<Diagnostic> {
    let mut diags = Vec::new();
    // Check YAML parse
    if let Err(e) = serde_yaml_ng::from_str::<serde_json::Value>(content) {
        diags.push(Diagnostic {
            line: 0,
            character: 0,
            end_line: 0,
            end_character: 80,
            severity: DiagnosticSeverity::Error,
            message: format!("YAML parse error: {e}"),
            source: "forjar-lsp".to_string(),
        });
        return diags;
    }

    // Check for common line-level issues
    for (i, line) in content.lines().enumerate() {
        let trimmed = line.trim();
        if trimmed.contains('\t') {
            diags.push(make_diag(
                i,
                line,
                DiagnosticSeverity::Warning,
                "Tabs should not be used in YAML; use spaces",
            ));
        }
        if trimmed.starts_with("ensure:") {
            let val = trimmed.trim_start_matches("ensure:").trim();
            if !["present", "absent", "latest", "running", "stopped", ""].contains(&val) {
                diags.push(make_diag(i, line, DiagnosticSeverity::Warning,
                    &format!("Unknown ensure value '{val}'; expected present|absent|latest|running|stopped")));
            }
        }
    }

    // FJ-2504: Unknown field detection + structural validation
    for w in &crate::core::parser::check_unknown_fields(content) {
        diags.push(make_diag(0, "", DiagnosticSeverity::Warning, &w.message));
    }
    if let Ok(config) = crate::core::parser::parse_config(content) {
        for e in &crate::core::parser::validate_config(&config) {
            diags.push(make_diag(0, "", DiagnosticSeverity::Error, &e.message));
        }
    }

    diags
}

pub(super) fn make_diag(
    line_idx: usize,
    line: &str,
    severity: DiagnosticSeverity,
    msg: &str,
) -> Diagnostic {
    Diagnostic {
        line: line_idx as u32,
        character: 0,
        end_line: line_idx as u32,
        end_character: line.len() as u32,
        severity,
        message: msg.to_string(),
        source: "forjar-lsp".to_string(),
    }
}

/// Build an LSP initialize result.
pub(super) fn initialize_result() -> serde_json::Value {
    serde_json::json!({
        "capabilities": {
            "textDocumentSync": 1,
            "completionProvider": { "triggerCharacters": [":", " ", "-"] },
            "hoverProvider": true,
            "diagnosticProvider": { "interFileDependencies": false, "workspaceDiagnostics": false }
        },
        "serverInfo": { "name": "forjar-lsp", "version": env!("CARGO_PKG_VERSION") }
    })
}

pub(super) fn make_response(
    id: Option<&serde_json::Value>,
    result: serde_json::Value,
) -> serde_json::Value {
    serde_json::json!({
        "jsonrpc": "2.0",
        "id": id.cloned().unwrap_or(serde_json::Value::Null),
        "result": result
    })
}

pub(super) fn make_error_response(
    id: Option<&serde_json::Value>,
    code: i32,
    msg: &str,
) -> serde_json::Value {
    serde_json::json!({
        "jsonrpc": "2.0",
        "id": id.cloned().unwrap_or(serde_json::Value::Null),
        "error": { "code": code, "message": msg }
    })
}

/// Write an LSP message (Content-Length header + JSON body).
pub fn write_message<W: Write>(writer: &mut W, msg: &serde_json::Value) -> io::Result<()> {
    let body = serde_json::to_string(msg).map_err(io::Error::other)?;
    write!(writer, "Content-Length: {}\r\n\r\n{}", body.len(), body)?;
    writer.flush()
}

/// Read one LSP message from stdin.
pub fn read_message<R: BufRead>(reader: &mut R) -> io::Result<serde_json::Value> {
    let mut header = String::new();
    let mut content_length: usize = 0;

    loop {
        header.clear();
        reader.read_line(&mut header)?;
        let trimmed = header.trim();
        if trimmed.is_empty() {
            break;
        }
        if let Some(len_str) = trimmed.strip_prefix("Content-Length: ") {
            content_length = len_str
                .parse()
                .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
        }
    }

    if content_length == 0 {
        return Err(io::Error::new(
            io::ErrorKind::UnexpectedEof,
            "no content length",
        ));
    }

    let mut body = vec![0u8; content_length];
    reader.read_exact(&mut body)?;
    serde_json::from_slice(&body).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}

/// Entry point: run the LSP server on stdio.
pub fn cmd_lsp() -> Result<(), String> {
    let stdin = io::stdin();
    let stdout = io::stdout();
    let mut reader = stdin.lock();
    let mut writer = stdout.lock();
    let mut server = LspServer::new();

    loop {
        let msg = read_message(&mut reader).map_err(|e| e.to_string())?;
        if let Some(resp) = server.handle_message(&msg) {
            write_message(&mut writer, &resp).map_err(|e| e.to_string())?;
        }
        // Publish diagnostics after document changes
        if let Some(method) = msg.get("method").and_then(|m| m.as_str()) {
            if method == "textDocument/didOpen" || method == "textDocument/didChange" {
                if let Some(uri) = msg
                    .pointer("/params/textDocument/uri")
                    .and_then(|v| v.as_str())
                {
                    let diags = server.validate_document(uri);
                    let notification = super::lsp_publish::publish_diagnostics(uri, &diags);
                    write_message(&mut writer, &notification).map_err(|e| e.to_string())?;
                }
            }
        }
    }
}

// Re-export for test modules that use `super::lsp::*`
#[cfg(test)]
pub(super) use super::lsp_publish::publish_diagnostics;