quillmark-cli 0.48.0

Command-line interface for the Quillmark Markdown rendering system
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
use crate::errors::{CliError, Result};
use clap::Parser;
use quillmark::Quill;
use quillmark_core::quill::{CardSchema, FieldSchema, FieldType, QuillConfig};
use quillmark_core::QuillValue;
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;

#[derive(Parser)]
pub struct ValidateArgs {
    /// Path to quill directory
    #[arg(value_name = "QUILL_PATH")]
    quill_path: PathBuf,

    /// Show verbose output with all validation details
    #[arg(short, long)]
    verbose: bool,
}

/// Validation issue severity
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Severity {
    Error,
    Warning,
}

/// A single validation issue
#[derive(Debug)]
struct ValidationIssue {
    severity: Severity,
    message: String,
}

impl ValidationIssue {
    fn error(message: impl Into<String>) -> Self {
        Self {
            severity: Severity::Error,
            message: message.into(),
        }
    }

    fn warning(message: impl Into<String>) -> Self {
        Self {
            severity: Severity::Warning,
            message: message.into(),
        }
    }
}

/// Result of validating a Quill configuration
#[derive(Debug, Default)]
struct ValidationResult {
    issues: Vec<ValidationIssue>,
}

impl ValidationResult {
    fn new() -> Self {
        Self { issues: Vec::new() }
    }

    fn add_error(&mut self, message: impl Into<String>) {
        self.issues.push(ValidationIssue::error(message));
    }

    fn add_warning(&mut self, message: impl Into<String>) {
        self.issues.push(ValidationIssue::warning(message));
    }

    fn has_errors(&self) -> bool {
        self.issues.iter().any(|i| i.severity == Severity::Error)
    }

    fn error_count(&self) -> usize {
        self.issues
            .iter()
            .filter(|i| i.severity == Severity::Error)
            .count()
    }

    fn warning_count(&self) -> usize {
        self.issues
            .iter()
            .filter(|i| i.severity == Severity::Warning)
            .count()
    }
}

pub fn execute(args: ValidateArgs) -> Result<()> {
    // Validate quill path exists
    if !args.quill_path.exists() {
        return Err(CliError::InvalidArgument(format!(
            "Quill directory not found: {}",
            args.quill_path.display()
        )));
    }

    let quill_yaml_path = args.quill_path.join("Quill.yaml");
    if !quill_yaml_path.exists() {
        return Err(CliError::InvalidArgument(format!(
            "Quill.yaml not found in: {}",
            args.quill_path.display()
        )));
    }

    if args.verbose {
        println!("Validating quill at: {}", args.quill_path.display());
    }

    let mut result = ValidationResult::new();

    // Step 1: Parse the YAML config first (before full Quill load)
    let yaml_content = fs::read_to_string(&quill_yaml_path).map_err(CliError::Io)?;

    let config = match QuillConfig::from_yaml(&yaml_content) {
        Ok(c) => c,
        Err(e) => {
            result.add_error(format!("Failed to parse Quill.yaml: {}", e));
            print_validation_result(&result, args.verbose);
            return Err(CliError::InvalidArgument(
                "Quill configuration is invalid".to_string(),
            ));
        }
    };

    if args.verbose {
        println!("  Quill name: {}", config.name);
        println!("  Backend: {}", config.backend);
        println!("  Fields: {}", config.main().fields.len());
        println!("  Cards: {}", config.card_definitions().len());
    }

    // Step 2: Validate file references
    validate_file_references(&args.quill_path, &config, &mut result);

    // Step 3: Validate field schemas including defaults
    validate_field_schemas(&config.main().fields, &mut result, "field");

    // Step 4: Validate card schemas
    for card_schema in config.card_definitions() {
        validate_card_schema(&card_schema.name, card_schema, &mut result);
    }

    // Step 5: Try to load the full Quill (this validates schema generation)
    match Quill::from_path(&args.quill_path) {
        Ok(quill) => {
            if args.verbose {
                println!("  Schema generated successfully");
                println!("  Defaults extracted: {}", quill.extract_defaults().len());
            }

            // Step 6: Validate extracted defaults match schema types
            validate_defaults_against_schema(&quill, &config, &mut result);
        }
        Err(e) => {
            result.add_error(format!("Failed to load Quill: {}", e));
        }
    }

    // Print results
    print_validation_result(&result, args.verbose);

    if result.has_errors() {
        Err(CliError::InvalidArgument(format!(
            "Validation failed with {} error(s)",
            result.error_count()
        )))
    } else {
        Ok(())
    }
}

fn validate_file_references(
    quill_path: &PathBuf,
    config: &QuillConfig,
    result: &mut ValidationResult,
) {
    // Check plate_file reference
    if let Some(ref plate_file) = config.plate_file {
        let plate_path = quill_path.join(plate_file);
        if !plate_path.exists() {
            result.add_error(format!(
                "Referenced plate_file '{}' does not exist",
                plate_file
            ));
        }
    }

    // Check example_file reference
    if let Some(ref example_file) = config.example_file {
        let example_path = quill_path.join(example_file);
        if !example_path.exists() {
            result.add_warning(format!(
                "Referenced example_file '{}' does not exist",
                example_file
            ));
        }
    }
}

fn validate_field_schemas(
    fields: &HashMap<String, FieldSchema>,
    result: &mut ValidationResult,
    context: &str,
) {
    for (field_name, field_schema) in fields {
        // Validate default value type matches declared type
        if let Some(ref default) = field_schema.default {
            if let Some(type_mismatch) = check_type_mismatch(&field_schema.r#type, default) {
                result.add_error(format!(
                    "{} '{}': default value {} but field type is '{}'",
                    context,
                    field_name,
                    type_mismatch,
                    field_schema.r#type.as_str()
                ));
            }
        }

        // Validate enum values are strings if specified
        if let Some(ref enum_values) = field_schema.enum_values {
            if enum_values.is_empty() {
                result.add_warning(format!(
                    "{} '{}': enum constraint is empty",
                    context, field_name
                ));
            }

            // If there's a default, check it's in the enum
            if let Some(ref default) = field_schema.default {
                if let Some(default_str) = default.as_str() {
                    if !enum_values.contains(&default_str.to_string()) {
                        result.add_error(format!(
                            "{} '{}': default value '{}' is not in enum values {:?}",
                            context, field_name, default_str, enum_values
                        ));
                    }
                }
            }
        }

        // Warn about missing description
        if field_schema
            .description
            .as_deref()
            .unwrap_or("")
            .trim()
            .is_empty()
        {
            result.add_warning(format!(
                "{} '{}': missing or empty description",
                context, field_name
            ));
        }
    }
}

fn validate_card_schema(card_name: &str, card_schema: &CardSchema, result: &mut ValidationResult) {
    // Warn about missing description
    if card_schema
        .description
        .as_deref()
        .unwrap_or("")
        .trim()
        .is_empty()
    {
        result.add_warning(format!(
            "card '{}': missing or empty description",
            card_name
        ));
    }

    // Validate card fields
    let context = format!("card '{}' field", card_name);
    validate_field_schemas(&card_schema.fields, result, &context);
}

fn check_type_mismatch(field_type: &FieldType, value: &QuillValue) -> Option<String> {
    let json_value = value.as_json();

    match field_type {
        FieldType::String => {
            if !json_value.is_string() {
                Some(format!(
                    "is {} (not a string)",
                    describe_json_type(json_value)
                ))
            } else {
                None
            }
        }
        FieldType::Number => {
            if !json_value.is_number() {
                Some(format!(
                    "is {} (not a number)",
                    describe_json_type(json_value)
                ))
            } else {
                None
            }
        }
        FieldType::Boolean => {
            if !json_value.is_boolean() {
                Some(format!(
                    "is {} (not a boolean)",
                    describe_json_type(json_value)
                ))
            } else {
                None
            }
        }
        FieldType::Array => {
            if !json_value.is_array() {
                Some(format!(
                    "is {} (not an array)",
                    describe_json_type(json_value)
                ))
            } else {
                None
            }
        }
        FieldType::Object => {
            if !json_value.is_object() {
                Some(format!(
                    "is {} (not an object)",
                    describe_json_type(json_value)
                ))
            } else {
                None
            }
        }
        FieldType::Date | FieldType::DateTime | FieldType::Markdown => {
            // Date/DateTime/Markdown are stored as strings
            if !json_value.is_string() {
                Some(format!(
                    "is {} (not a string)",
                    describe_json_type(json_value)
                ))
            } else {
                None
            }
        }
    }
}

fn describe_json_type(value: &serde_json::Value) -> &'static str {
    match value {
        serde_json::Value::Null => "null",
        serde_json::Value::Bool(_) => "a boolean",
        serde_json::Value::Number(_) => "a number",
        serde_json::Value::String(_) => "a string",
        serde_json::Value::Array(_) => "an array",
        serde_json::Value::Object(_) => "an object",
    }
}

fn validate_defaults_against_schema(
    quill: &Quill,
    config: &QuillConfig,
    result: &mut ValidationResult,
) {
    let defaults = quill.extract_defaults();

    for (field_name, default_value) in defaults {
        // Look up field type in config
        if let Some(field_schema) = config.main().fields.get(field_name) {
            if let Some(type_mismatch) = check_type_mismatch(&field_schema.r#type, default_value) {
                result.add_error(format!(
                    "extracted default for '{}' {}, expected '{}'",
                    field_name,
                    type_mismatch,
                    field_schema.r#type.as_str()
                ));
            }
        }
    }
}

fn print_validation_result(result: &ValidationResult, verbose: bool) {
    let error_count = result.error_count();
    let warning_count = result.warning_count();

    // Print issues
    for issue in &result.issues {
        match issue.severity {
            Severity::Error => eprintln!("[ERROR] {}", issue.message),
            Severity::Warning => {
                if verbose {
                    eprintln!("[WARNING] {}", issue.message)
                }
            }
        }
    }

    // Print summary
    if error_count == 0 && warning_count == 0 {
        println!("Validation passed: quill configuration is valid");
    } else if error_count == 0 {
        println!("Validation passed with {} warning(s)", warning_count);
    } else {
        eprintln!(
            "Validation failed: {} error(s), {} warning(s)",
            error_count, warning_count
        );
    }
}