domainforge-core 0.21.2

Rust core library implementing the SEA DSL primitives and validation engine.
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
//! Buf.build CLI Integration
//!
//! This module provides optional integration with the buf.build CLI for
//! linting, breaking change detection, and code generation.
//!
//! All buf operations gracefully degrade if the buf CLI is not installed.

use std::path::{Path, PathBuf};
use std::process::Command;

/// Result type for buf operations.
pub type BufResult<T> = Result<T, BufError>;

/// Errors from buf CLI operations.
#[derive(Debug, Clone)]
pub enum BufError {
    /// Buf CLI not found in PATH
    NotInstalled,
    /// Buf command failed with exit code
    CommandFailed { exit_code: i32, stderr: String },
    /// IO error
    IoError(String),
    /// Configuration error
    ConfigError(String),
}

impl std::fmt::Display for BufError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            BufError::NotInstalled => write!(
                f,
                "buf CLI not found. Install from: https://buf.build/docs/installation"
            ),
            BufError::CommandFailed { exit_code, stderr } => {
                write!(f, "buf command failed (exit {}): {}", exit_code, stderr)
            }
            BufError::IoError(msg) => write!(f, "IO error: {}", msg),
            BufError::ConfigError(msg) => write!(f, "Configuration error: {}", msg),
        }
    }
}

impl std::error::Error for BufError {}

/// Buf.build CLI integration.
///
/// Provides optional integration with the buf CLI for enhanced proto workflows.
/// All operations gracefully degrade if buf is not installed.
pub struct BufIntegration {
    /// Path to buf binary (or None to search PATH)
    buf_path: Option<PathBuf>,
}

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

impl BufIntegration {
    /// Create a new BufIntegration with default settings.
    pub fn new() -> Self {
        Self { buf_path: None }
    }

    /// Create with a specific buf binary path.
    pub fn with_path(path: impl Into<PathBuf>) -> Self {
        Self {
            buf_path: Some(path.into()),
        }
    }

    /// Check if buf CLI is available.
    pub fn is_available(&self) -> bool {
        self.get_version().is_ok()
    }

    /// Get buf CLI version.
    pub fn get_version(&self) -> BufResult<String> {
        let output = self
            .run_buf(&["--version"])
            .map_err(|_| BufError::NotInstalled)?;

        Ok(output.trim().to_string())
    }

    /// Lint proto files in a directory.
    ///
    /// Returns Ok with lint output, or gracefully degrades if buf not installed.
    pub fn lint(&self, proto_dir: &Path) -> BufResult<BufLintResult> {
        // Check if buf.yaml exists, create temporary one if not
        let buf_yaml = proto_dir.join("buf.yaml");
        let temp_config = if !buf_yaml.exists() {
            let config = Self::generate_buf_yaml();
            std::fs::write(&buf_yaml, &config).map_err(|e| BufError::IoError(e.to_string()))?;
            Some(buf_yaml.clone())
        } else {
            None
        };

        let result = self.run_buf(&["lint", proto_dir.to_str().unwrap_or(".")]);

        // Clean up temporary config
        if let Some(path) = temp_config {
            let _ = std::fs::remove_file(path);
        }

        match result {
            Ok(output) => Ok(BufLintResult {
                success: true,
                issues: vec![],
                raw_output: output,
            }),
            Err(BufError::CommandFailed { stderr, .. }) => Ok(BufLintResult {
                success: false,
                issues: Self::parse_lint_issues(&stderr),
                raw_output: stderr,
            }),
            Err(e) => Err(e),
        }
    }

    /// Check for breaking changes between two proto directories.
    pub fn breaking_check(&self, new_dir: &Path, old_dir: &Path) -> BufResult<BufBreakingResult> {
        let result = self.run_buf(&[
            "breaking",
            new_dir.to_str().unwrap_or("."),
            "--against",
            old_dir.to_str().unwrap_or("."),
        ]);

        match result {
            Ok(output) => Ok(BufBreakingResult {
                is_compatible: true,
                violations: vec![],
                raw_output: output,
            }),
            Err(BufError::CommandFailed { stderr, .. }) => Ok(BufBreakingResult {
                is_compatible: false,
                violations: Self::parse_breaking_violations(&stderr),
                raw_output: stderr,
            }),
            Err(e) => Err(e),
        }
    }

    /// Generate a buf.yaml configuration file content.
    pub fn generate_buf_yaml() -> String {
        r#"version: v1
breaking:
  use:
    - FILE
lint:
  use:
    - DEFAULT
  except:
    - PACKAGE_VERSION_SUFFIX
    - PACKAGE_DIRECTORY_MATCH
"#
        .to_string()
    }

    /// Generate a buf.gen.yaml for code generation.
    pub fn generate_buf_gen_yaml(languages: &[BufLanguage]) -> String {
        let mut plugins = String::new();

        for lang in languages {
            plugins.push_str(&lang.to_plugin_config());
        }

        format!(
            r#"version: v1
plugins:
{}"#,
            plugins
        )
    }

    fn run_buf(&self, args: &[&str]) -> BufResult<String> {
        let buf_cmd = self
            .buf_path
            .as_ref()
            .map(|p| p.to_string_lossy().to_string())
            .unwrap_or_else(|| "buf".to_string());

        let output = Command::new(&buf_cmd).args(args).output().map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                BufError::NotInstalled
            } else {
                BufError::IoError(e.to_string())
            }
        })?;

        if output.status.success() {
            Ok(String::from_utf8_lossy(&output.stdout).to_string())
        } else {
            Err(BufError::CommandFailed {
                exit_code: output.status.code().unwrap_or(-1),
                stderr: String::from_utf8_lossy(&output.stderr).to_string(),
            })
        }
    }

    fn parse_lint_issues(output: &str) -> Vec<BufLintIssue> {
        output
            .lines()
            .filter(|l| !l.is_empty())
            .map(|line| BufLintIssue {
                message: line.to_string(),
                file: None,
                line: None,
                rule: None,
            })
            .collect()
    }

    fn parse_breaking_violations(output: &str) -> Vec<String> {
        output
            .lines()
            .filter(|l| !l.is_empty())
            .map(|s| s.to_string())
            .collect()
    }
}

/// Result of buf lint operation.
#[derive(Debug, Clone)]
pub struct BufLintResult {
    /// Whether lint passed with no issues
    pub success: bool,
    /// List of lint issues found
    pub issues: Vec<BufLintIssue>,
    /// Raw output from buf
    pub raw_output: String,
}

impl BufLintResult {
    /// Create a skipped result (when buf not available).
    pub fn skipped(reason: &str) -> Self {
        Self {
            success: true,
            issues: vec![],
            raw_output: format!("Skipped: {}", reason),
        }
    }
}

/// A single lint issue.
#[derive(Debug, Clone)]
pub struct BufLintIssue {
    /// Issue message
    pub message: String,
    /// File path (if available)
    pub file: Option<String>,
    /// Line number (if available)
    pub line: Option<u32>,
    /// Lint rule ID (if available)
    pub rule: Option<String>,
}

/// Result of buf breaking check.
#[derive(Debug, Clone)]
pub struct BufBreakingResult {
    /// Whether the changes are compatible
    pub is_compatible: bool,
    /// List of breaking violations
    pub violations: Vec<String>,
    /// Raw output from buf
    pub raw_output: String,
}

/// Supported languages for buf code generation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BufLanguage {
    Go,
    Rust,
    Python,
    TypeScript,
    Java,
    CSharp,
    Cpp,
    Swift,
}

impl BufLanguage {
    /// Get plugin configuration for buf.gen.yaml.
    pub fn to_plugin_config(&self) -> String {
        match self {
            BufLanguage::Go => r#"  - plugin: buf.build/protocolbuffers/go
    out: gen/go
"#
            .to_string(),
            BufLanguage::Rust => r#"  - plugin: buf.build/community/neoeinstein-prost
    out: gen/rust
"#
            .to_string(),
            BufLanguage::Python => r#"  - plugin: buf.build/protocolbuffers/python
    out: gen/python
"#
            .to_string(),
            BufLanguage::TypeScript => r#"  - plugin: buf.build/community/stephenh-ts-proto
    out: gen/typescript
"#
            .to_string(),
            BufLanguage::Java => r#"  - plugin: buf.build/protocolbuffers/java
    out: gen/java
"#
            .to_string(),
            BufLanguage::CSharp => r#"  - plugin: buf.build/protocolbuffers/csharp
    out: gen/csharp
"#
            .to_string(),
            BufLanguage::Cpp => r#"  - plugin: buf.build/protocolbuffers/cpp
    out: gen/cpp
"#
            .to_string(),
            BufLanguage::Swift => r#"  - plugin: buf.build/apple/swift
    out: gen/swift
"#
            .to_string(),
        }
    }

    /// Parse from string.
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "go" | "golang" => Some(BufLanguage::Go),
            "rust" | "rs" => Some(BufLanguage::Rust),
            "python" | "py" => Some(BufLanguage::Python),
            "typescript" | "ts" => Some(BufLanguage::TypeScript),
            "java" => Some(BufLanguage::Java),
            "csharp" | "cs" | "c#" => Some(BufLanguage::CSharp),
            "cpp" | "c++" => Some(BufLanguage::Cpp),
            "swift" => Some(BufLanguage::Swift),
            _ => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_buf_yaml() {
        let yaml = BufIntegration::generate_buf_yaml();
        assert!(yaml.contains("version: v1"));
        assert!(yaml.contains("breaking:"));
        assert!(yaml.contains("lint:"));
        assert!(yaml.contains("DEFAULT"));
    }

    #[test]
    fn test_generate_buf_gen_yaml() {
        let yaml = BufIntegration::generate_buf_gen_yaml(&[BufLanguage::Go, BufLanguage::Rust]);
        assert!(yaml.contains("version: v1"));
        assert!(yaml.contains("plugins:"));
        assert!(yaml.contains("protocolbuffers/go"));
        assert!(yaml.contains("neoeinstein-prost"));
    }

    #[test]
    fn test_buf_language_from_str() {
        assert_eq!(BufLanguage::parse("go"), Some(BufLanguage::Go));
        assert_eq!(BufLanguage::parse("rust"), Some(BufLanguage::Rust));
        assert_eq!(BufLanguage::parse("py"), Some(BufLanguage::Python));
        assert_eq!(BufLanguage::parse("ts"), Some(BufLanguage::TypeScript));
        assert_eq!(BufLanguage::parse("invalid"), None);
    }

    #[test]
    fn test_buf_lint_result_skipped() {
        let result = BufLintResult::skipped("buf not installed");
        assert!(result.success);
        assert!(result.issues.is_empty());
        assert!(result.raw_output.contains("Skipped"));
    }

    #[test]
    fn test_buf_error_display() {
        let err = BufError::NotInstalled;
        assert!(err.to_string().contains("buf CLI not found"));

        let err2 = BufError::CommandFailed {
            exit_code: 1,
            stderr: "some error".to_string(),
        };
        assert!(err2.to_string().contains("exit 1"));
    }

    #[test]
    fn test_run_buf_not_found() {
        // Test that it handles missing binary
        let buf = BufIntegration::with_path("non_existent_binary");
        assert!(!buf.is_available());

        // Should return NotInstalled error
        let result = buf.get_version();
        match result {
            Err(BufError::NotInstalled) => (),
            _ => panic!("Expected NotInstalled error, got {:?}", result),
        }
    }
}