rez-lsp-server 0.1.3

A Language Server Protocol implementation for Rez package manager with intelligent code completion, validation, and navigation
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
491
492
493
//! Rez-specific validation for package.py files.

use super::{Severity, ValidationIssue, Validator};
use crate::core::{types::Version, Result};
use regex::Regex;
use std::collections::{HashMap, HashSet};

/// Validates Rez-specific syntax and semantics in package.py files.
pub struct RezValidator {
    /// Required fields for a valid Rez package
    required_fields: HashSet<String>,
    /// Optional but recommended fields
    recommended_fields: HashSet<String>,
    /// Deprecated fields that should be avoided
    deprecated_fields: HashMap<String, String>,
    /// Regex patterns for validation
    patterns: RezPatterns,
}

struct RezPatterns {
    /// Pattern for version strings
    version_pattern: Regex,
    /// Pattern for package names
    name_pattern: Regex,
    /// Pattern for requirement strings
    requirement_pattern: Regex,
    /// Pattern for tool definitions
    #[allow(dead_code)]
    tool_pattern: Regex,
}

impl RezValidator {
    /// Create a new Rez validator.
    pub fn new() -> Result<Self> {
        let mut required_fields = HashSet::new();
        required_fields.insert("name".to_string());
        required_fields.insert("version".to_string());

        let mut recommended_fields = HashSet::new();
        recommended_fields.insert("description".to_string());
        recommended_fields.insert("authors".to_string());
        recommended_fields.insert("requires".to_string());

        let mut deprecated_fields = HashMap::new();
        deprecated_fields.insert(
            "uuid".to_string(),
            "UUIDs are no longer used in Rez packages".to_string(),
        );
        deprecated_fields.insert(
            "config".to_string(),
            "Use 'private_build_requires' instead".to_string(),
        );

        let patterns = RezPatterns {
            version_pattern: Regex::new(r"^[0-9]+(\.[0-9]+)*([a-zA-Z][a-zA-Z0-9]*)?$")?,
            name_pattern: Regex::new(r"^[a-zA-Z][a-zA-Z0-9_]*$")?,
            requirement_pattern: Regex::new(r"^[a-zA-Z][a-zA-Z0-9_]*([<>=!]+[0-9]+(\.[0-9]+)*)?$")?,
            tool_pattern: Regex::new(r"^[a-zA-Z][a-zA-Z0-9_]*$")?,
        };

        Ok(Self {
            required_fields,
            recommended_fields,
            deprecated_fields,
            patterns,
        })
    }

    /// Extract field assignments from Python code.
    fn extract_fields(&self, content: &str) -> HashMap<String, (u32, String)> {
        let mut fields = HashMap::new();
        let assignment_regex = Regex::new(r"^(\w+)\s*=\s*(.+)$").unwrap();

        for (line_num, line) in content.lines().enumerate() {
            let line_num = line_num as u32 + 1;
            let trimmed = line.trim();

            // Skip comments and empty lines
            if trimmed.is_empty() || trimmed.starts_with('#') {
                continue;
            }

            if let Some(captures) = assignment_regex.captures(trimmed) {
                let field_name = captures.get(1).unwrap().as_str().to_string();
                let field_value = captures.get(2).unwrap().as_str().to_string();
                fields.insert(field_name, (line_num, field_value));
            }
        }

        fields
    }

    /// Validate required fields.
    fn check_required_fields(
        &self,
        fields: &HashMap<String, (u32, String)>,
    ) -> Vec<ValidationIssue> {
        let mut issues = Vec::new();

        for required_field in &self.required_fields {
            if !fields.contains_key(required_field) {
                issues.push(
                    ValidationIssue::new(
                        Severity::Error,
                        1,
                        1,
                        1,
                        format!("Missing required field '{}'", required_field),
                        "R001",
                    )
                    .with_suggestion(format!(
                        "Add '{}' field to the package definition",
                        required_field
                    )),
                );
            }
        }

        issues
    }

    /// Validate recommended fields.
    fn check_recommended_fields(
        &self,
        fields: &HashMap<String, (u32, String)>,
    ) -> Vec<ValidationIssue> {
        let mut issues = Vec::new();

        for recommended_field in &self.recommended_fields {
            if !fields.contains_key(recommended_field) {
                issues.push(
                    ValidationIssue::new(
                        Severity::Warning,
                        1,
                        1,
                        1,
                        format!("Missing recommended field '{}'", recommended_field),
                        "R101",
                    )
                    .with_suggestion(format!(
                        "Consider adding '{}' field for better package documentation",
                        recommended_field
                    )),
                );
            }
        }

        issues
    }

    /// Validate deprecated fields.
    fn check_deprecated_fields(
        &self,
        fields: &HashMap<String, (u32, String)>,
    ) -> Vec<ValidationIssue> {
        let mut issues = Vec::new();

        for (field_name, (line_num, _)) in fields {
            if let Some(reason) = self.deprecated_fields.get(field_name) {
                issues.push(
                    ValidationIssue::new(
                        Severity::Warning,
                        *line_num,
                        1,
                        field_name.len() as u32,
                        format!("Deprecated field '{}': {}", field_name, reason),
                        "R201",
                    )
                    .with_suggestion("Remove this deprecated field"),
                );
            }
        }

        issues
    }

    /// Validate package name.
    fn validate_name(&self, fields: &HashMap<String, (u32, String)>) -> Vec<ValidationIssue> {
        let mut issues = Vec::new();

        if let Some((line_num, value)) = fields.get("name") {
            let clean_value = self.clean_string_value(value);

            if !self.patterns.name_pattern.is_match(&clean_value) {
                issues.push(ValidationIssue::new(
                    Severity::Error,
                    *line_num,
                    1,
                    value.len() as u32,
                    "Invalid package name format",
                    "R002",
                ).with_suggestion("Package names must start with a letter and contain only letters, numbers, and underscores"));
            }

            // Check for reserved names
            let reserved_names = ["test", "build", "install", "package"];
            if reserved_names.contains(&clean_value.as_str()) {
                issues.push(
                    ValidationIssue::new(
                        Severity::Warning,
                        *line_num,
                        1,
                        value.len() as u32,
                        format!("Package name '{}' is a reserved word", clean_value),
                        "R102",
                    )
                    .with_suggestion("Consider using a different package name"),
                );
            }
        }

        issues
    }

    /// Validate version field.
    fn validate_version(&self, fields: &HashMap<String, (u32, String)>) -> Vec<ValidationIssue> {
        let mut issues = Vec::new();

        if let Some((line_num, value)) = fields.get("version") {
            let clean_value = self.clean_string_value(value);

            // Try to parse as Rez version
            match Version::new(&clean_value) {
                version if version.tokens.is_empty() => {
                    issues.push(
                        ValidationIssue::new(
                            Severity::Error,
                            *line_num,
                            1,
                            value.len() as u32,
                            "Invalid version format",
                            "R003",
                        )
                        .with_suggestion("Use semantic versioning (e.g., '1.0.0')"),
                    );
                }
                _ => {
                    // Version is valid, check for best practices
                    if !self.patterns.version_pattern.is_match(&clean_value) {
                        issues.push(
                            ValidationIssue::new(
                                Severity::Warning,
                                *line_num,
                                1,
                                value.len() as u32,
                                "Version format doesn't follow semantic versioning",
                                "R103",
                            )
                            .with_suggestion(
                                "Consider using semantic versioning (major.minor.patch)",
                            ),
                        );
                    }
                }
            }
        }

        issues
    }

    /// Validate requires field.
    fn validate_requires(&self, fields: &HashMap<String, (u32, String)>) -> Vec<ValidationIssue> {
        let mut issues = Vec::new();

        if let Some((line_num, value)) = fields.get("requires") {
            // Parse the requires list
            if let Some(requirements) = self.parse_list_value(value) {
                for requirement in requirements.iter() {
                    let clean_req = self.clean_string_value(requirement);

                    // Validate requirement format
                    if !self.patterns.requirement_pattern.is_match(&clean_req) {
                        issues.push(
                            ValidationIssue::new(
                                Severity::Error,
                                *line_num,
                                1,
                                requirement.len() as u32,
                                format!("Invalid requirement format: '{}'", clean_req),
                                "R004",
                            )
                            .with_suggestion(
                                "Requirements should be in format 'package' or 'package>=1.0.0'",
                            ),
                        );
                    }

                    // Check for common typos
                    let common_packages = ["python", "maya", "houdini", "nuke", "blender"];
                    if !common_packages
                        .iter()
                        .any(|&pkg| clean_req.starts_with(pkg))
                    {
                        // This is a custom package, check for naming conventions
                        if clean_req.contains('-') {
                            issues.push(
                                ValidationIssue::new(
                                    Severity::Warning,
                                    *line_num,
                                    1,
                                    requirement.len() as u32,
                                    "Package names with hyphens may cause issues",
                                    "R104",
                                )
                                .with_suggestion("Consider using underscores instead of hyphens"),
                            );
                        }
                    }
                }

                // Check for duplicate requirements
                let mut seen = HashSet::new();
                for requirement in &requirements {
                    let clean_req = self.clean_string_value(requirement);
                    let package_name = clean_req
                        .split(&['<', '>', '=', '!'][..])
                        .next()
                        .unwrap_or(&clean_req)
                        .to_string();

                    if !seen.insert(package_name.clone()) {
                        issues.push(
                            ValidationIssue::new(
                                Severity::Warning,
                                *line_num,
                                1,
                                value.len() as u32,
                                format!("Duplicate requirement: '{}'", package_name),
                                "R105",
                            )
                            .with_suggestion("Remove duplicate requirements"),
                        );
                    }
                }
            }
        }

        issues
    }

    /// Validate tools field.
    fn validate_tools(&self, fields: &HashMap<String, (u32, String)>) -> Vec<ValidationIssue> {
        let mut issues = Vec::new();

        if let Some((line_num, value)) = fields.get("tools") {
            // Basic validation for tools dictionary format
            if !value.trim().starts_with('{') || !value.trim().ends_with('}') {
                issues.push(
                    ValidationIssue::new(
                        Severity::Error,
                        *line_num,
                        1,
                        value.len() as u32,
                        "Tools field must be a dictionary",
                        "R005",
                    )
                    .with_suggestion("Use dictionary format: tools = {'tool_name': 'tool_path'}"),
                );
            }
        }

        issues
    }

    /// Clean string values by removing quotes.
    fn clean_string_value(&self, value: &str) -> String {
        value
            .trim()
            .trim_start_matches('"')
            .trim_end_matches('"')
            .trim_start_matches('\'')
            .trim_end_matches('\'')
            .to_string()
    }

    /// Parse a list value from Python syntax.
    fn parse_list_value(&self, value: &str) -> Option<Vec<String>> {
        let trimmed = value.trim();
        if !trimmed.starts_with('[') || !trimmed.ends_with(']') {
            return None;
        }

        let content = &trimmed[1..trimmed.len() - 1];
        let items: Vec<String> = content
            .split(',')
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty())
            .collect();

        Some(items)
    }
}

impl Default for RezValidator {
    fn default() -> Self {
        Self::new().expect("Failed to create RezValidator")
    }
}

impl Validator for RezValidator {
    fn validate(&self, content: &str, _file_path: &str) -> Result<Vec<ValidationIssue>> {
        let mut issues = Vec::new();

        // Extract field assignments
        let fields = self.extract_fields(content);

        // Run all Rez-specific validations
        issues.extend(self.check_required_fields(&fields));
        issues.extend(self.check_recommended_fields(&fields));
        issues.extend(self.check_deprecated_fields(&fields));
        issues.extend(self.validate_name(&fields));
        issues.extend(self.validate_version(&fields));
        issues.extend(self.validate_requires(&fields));
        issues.extend(self.validate_tools(&fields));

        // Sort issues by line number
        issues.sort_by_key(|issue| issue.line);

        Ok(issues)
    }

    fn name(&self) -> &str {
        "RezValidator"
    }
}

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

    #[test]
    fn test_rez_validator_creation() {
        let validator = RezValidator::new();
        assert!(validator.is_ok());
    }

    #[test]
    fn test_valid_rez_package() {
        let validator = RezValidator::new().unwrap();
        let content = r#"
name = "test_package"
version = "1.0.0"
description = "A test package"
authors = ["Test Author"]
requires = ["python>=3.7"]
"#;

        let issues = validator.validate(content, "package.py").unwrap();
        // Should have no errors, maybe some warnings
        assert!(issues.iter().all(|i| i.severity != Severity::Error));
    }

    #[test]
    fn test_missing_required_fields() {
        let validator = RezValidator::new().unwrap();
        let content = r#"
description = "A test package"
"#;

        let issues = validator.validate(content, "package.py").unwrap();
        assert!(issues
            .iter()
            .any(|i| i.code == "R001" && i.message.contains("name")));
        assert!(issues
            .iter()
            .any(|i| i.code == "R001" && i.message.contains("version")));
    }

    #[test]
    fn test_invalid_package_name() {
        let validator = RezValidator::new().unwrap();
        let content = r#"
name = "123invalid"
version = "1.0.0"
"#;

        let issues = validator.validate(content, "package.py").unwrap();
        assert!(issues.iter().any(|i| i.code == "R002"));
    }

    #[test]
    fn test_deprecated_fields() {
        let validator = RezValidator::new().unwrap();
        let content = r#"
name = "test"
version = "1.0.0"
uuid = "some-uuid"
"#;

        let issues = validator.validate(content, "package.py").unwrap();
        assert!(issues.iter().any(|i| i.code == "R201"));
    }
}