shacl-cli 0.1.3

Command-line interface for SHACL validation using shacl-rust
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
use clap::{Parser, Subcommand};
use log::{debug, info};
use rayon::prelude::*;
use shacl_rust::{
    core::{shape::Shape, ShapesInfo},
    err::{path_to_str, ShaclError},
    parser, rdf, validate,
    validation::dataset::ValidationDataset,
};
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};

/// SHACL (Shapes Constraint Language) validator and toolkit
#[derive(Parser)]
#[command(name = "shacl-validator")]
#[command(author, version, about, long_about = None)]
struct Cli {
    /// Set the verbosity level (can be used multiple times: -v, -vv, -vvv)
    #[arg(short, long, action = clap::ArgAction::Count)]
    verbose: u8,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Parse and display SHACL shapes from a shapes graph
    Parse {
        /// Path to the SHACL shapes file
        #[arg(value_name = "SHAPES_FILE")]
        shapes_file: PathBuf,

        /// RDF format of the shapes file (ttl, nt, nq, rdf, jsonld, trig)
        /// If not specified, will be auto-detected from file extension
        #[arg(short, long)]
        format: Option<String>,

        /// Output format for displaying shapes (pretty, json, compact)
        #[arg(short, long, default_value = "pretty")]
        output: String,
    },

    /// Validate RDF data against SHACL shapes
    Validate {
        /// Path to the SHACL shapes file
        #[arg(value_name = "SHAPES_FILE")]
        shapes_file: PathBuf,

        /// Data files to validate (one or more)
        #[arg(value_name = "DATA_FILE", required = true)]
        data_files: Vec<PathBuf>,

        /// RDF format of the data file (auto-detected from extension if not specified)
        /// Supported: ttl, nt, nq, rdf, jsonld, trig
        #[arg(short = 'd', long)]
        data_format: Option<String>,

        /// RDF format of the shapes file (auto-detected from extension if not specified)
        /// Supported: ttl, nt, nq, rdf, jsonld, trig
        #[arg(short = 's', long)]
        shapes_format: Option<String>,

        /// Output file for validation report (if not specified, prints to stdout)
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Output format as file extension (ttl, nt, nq, rdf, jsonld, trig, json, yaml)
        /// If omitted or 'text', prints human-readable format. Otherwise exports as RDF graph.
        #[arg(long, default_value = "text")]
        output_format: String,

        /// Disable progress output
        #[arg(long, visible_alias = "quite")]
        quiet: bool,
    },

    /// Show information about SHACL shapes
    Info {
        /// Path to the SHACL shapes file
        #[arg(value_name = "SHAPES_FILE")]
        shapes_file: PathBuf,

        /// RDF format of the shapes file (auto-detected from extension if not specified)
        /// Supported: ttl, nt, nq, rdf, jsonld, trig
        #[arg(short, long)]
        format: Option<String>,

        /// Show detailed statistics
        #[arg(short, long)]
        detailed: bool,
    },
}

fn main() -> Result<(), ShaclError> {
    let cli = Cli::parse();

    // Initialize logger based on verbosity
    let log_level = match cli.verbose {
        0 => "warn",
        1 => "info",
        2 => "debug",
        _ => "trace",
    };
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(log_level)).init();

    debug!("Starting SHACL validator");

    match cli.command {
        Commands::Parse {
            shapes_file,
            format,
            output,
        } => {
            info!("Parsing shapes from: {}", shapes_file.display());
            parse_shapes_command(shapes_file, format, &output)
        }
        Commands::Validate {
            shapes_file,
            data_files,
            data_format,
            shapes_format,
            output,
            output_format,
            quiet,
        } => {
            info!("Validating {} data file(s)", data_files.len());
            info!("Using shapes: {}", shapes_file.display());
            validate_command(
                shapes_file,
                data_files,
                data_format,
                shapes_format,
                output,
                &output_format,
                quiet,
            )
        }
        Commands::Info {
            shapes_file,
            format,
            detailed,
        } => {
            info!("Showing info for shapes: {}", shapes_file.display());
            info_command(shapes_file, format, detailed)
        }
    }
}

fn parse_shapes_command(
    shapes_file: PathBuf,
    format: Option<String>,
    output: &str,
) -> Result<(), ShaclError> {
    debug!(
        "Reading shapes graph from {} with format {}",
        shapes_file.display(),
        format.as_deref().unwrap_or("auto")
    );

    let graph = read_graph_from_file(&shapes_file, format.as_deref())?;

    info!("Graph loaded with {} triples", graph.len());

    let shapes = parser::parse_shapes(&graph)?;
    info!("Parsed {} shapes", shapes.len());

    match output {
        "pretty" => println!("{}", ShapesPretty(&shapes)),
        "json" => print_shapes_json(&shapes)?,
        "compact" => println!("{}", ShapesCompact(&shapes)),
        _ => {
            return Err(ShaclError::Parse(format!(
                "Unknown output format: {}. Use 'pretty', 'json', or 'compact'",
                output
            )))
        }
    }

    Ok(())
}

struct ShapesPretty<'a>(&'a [Shape<'a>]);

impl Display for ShapesPretty<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "\n{}", "=".repeat(80))?;
        writeln!(f, "Parsed {} SHACL Shape(s)", self.0.len())?;
        writeln!(f, "{}\n", "=".repeat(80))?;

        for (idx, shape) in self.0.iter().enumerate() {
            writeln!(f, "Shape #{}:", idx + 1)?;
            writeln!(f, "{}", shape)?;
            writeln!(f)?;
        }

        Ok(())
    }
}

struct ShapesCompact<'a>(&'a [Shape<'a>]);

impl Display for ShapesCompact<'_> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Parsed {} shape(s):", self.0.len())?;
        for (idx, shape) in self.0.iter().enumerate() {
            writeln!(
                f,
                "  {}. {} - {} target(s), {} constraint(s)",
                idx + 1,
                shape.node,
                shape.targets.len(),
                shape.constraints.len()
            )?;
        }
        Ok(())
    }
}

fn print_shapes_json(shapes: &[Shape<'_>]) -> Result<(), ShaclError> {
    use serde_json::json;

    let shapes_json: Vec<_> = shapes
        .iter()
        .map(|shape| {
            json!({
                "node": shape.node.to_string(),
                "name": shape.name,
                "targets": shape.targets.iter().map(|t| t.to_string()).collect::<Vec<_>>(),
                "deactivated": shape.deactivated,
                "severity": shape.severity.to_string(),
                "messages": shape.message.iter().collect::<Vec<_>>(),
                "constraints": shape.constraints.iter().map(|c| c.to_string()).collect::<Vec<_>>(),
                "closed": shape.closed.as_ref().map(|c| c.to_string()),
            })
        })
        .collect();

    let output = json!({
        "shapes": shapes_json,
        "count": shapes.len(),
    });

    println!(
        "{}",
        serde_json::to_string_pretty(&output)
            .map_err(|e| { ShaclError::Parse(format!("Failed to serialize to JSON: {}", e)) })?
    );

    Ok(())
}

fn info_command(
    shapes_file: PathBuf,
    format: Option<String>,
    detailed: bool,
) -> Result<(), ShaclError> {
    debug!(
        "Reading shapes graph from {} with format {}",
        shapes_file.display(),
        format.as_deref().unwrap_or("auto")
    );

    let graph = read_graph_from_file(&shapes_file, format.as_deref())?;
    info!("Graph loaded with {} triples", graph.len());

    let shapes = parser::parse_shapes(&graph)?;
    println!("{}", ShapesInfo::new(&shapes, graph.len(), detailed));

    Ok(())
}

fn validate_command(
    shapes_file: PathBuf,
    data_files: Vec<PathBuf>,
    data_format: Option<String>,
    shapes_format: Option<String>,
    output: Option<PathBuf>,
    output_format: &str,
    quiet: bool,
) -> Result<(), ShaclError> {
    // If quiet is set, override log level to error
    if quiet {
        env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("error")).init();
    }
    let data_graphs_results: Vec<Result<(PathBuf, oxigraph::model::Graph), ShaclError>> =
        data_files
            .into_par_iter()
            .map(|data_file| {
                debug!(
                    "Reading data graph from {} with format {}",
                    data_file.display(),
                    data_format.as_deref().unwrap_or("auto")
                );
                let graph = read_graph_from_file(&data_file, data_format.as_deref())?;
                info!(
                    "Data graph {} loaded with {} triples",
                    data_file.display(),
                    graph.len()
                );
                Ok((data_file, graph))
            })
            .collect();

    let mut data_graph = oxigraph::model::Graph::new();
    for data_graph_result in data_graphs_results {
        let (data_file, graph) = data_graph_result?;
        let before_len = data_graph.len();
        data_graph.extend(graph.iter().map(oxigraph::model::Triple::from));
        info!(
            "Merged data graph {} ({} triples, total now {})",
            data_file.display(),
            graph.len(),
            data_graph.len()
        );
        debug!(
            "Data merge added {} unique triples",
            data_graph.len().saturating_sub(before_len)
        );
    }

    debug!(
        "Reading shapes graph from {} with format {}",
        shapes_file.display(),
        shapes_format.as_deref().unwrap_or("auto")
    );

    // Load shapes graph
    let shapes_graph = read_graph_from_file(&shapes_file, shapes_format.as_deref())?;
    info!("Shapes graph loaded with {} triples", shapes_graph.len());

    let validation_dataset = ValidationDataset::from_graphs(data_graph, shapes_graph)?;

    // Parse shapes
    let shapes = parser::parse_shapes(validation_dataset.shapes_graph())?;
    info!("Parsed {} shapes", shapes.len());

    let report = validate(&validation_dataset, &shapes);

    // Determine output format and generate report
    let output_text = match output_format {
        "text" => {
            // Human-readable text format
            report.to_string()
        }
        "json" => {
            // JSON format
            report.as_json().to_string()
        }
        _ => {
            // Try to parse as RDF format (ttl, nt, nq, rdf, jsonld, trig)
            use oxigraph::io::RdfFormat;
            let rdf_format = RdfFormat::from_extension(output_format).ok_or_else(|| {
                ShaclError::Parse(format!(
                    "Unsupported output format: '{}'. Supported: text, json, yaml, ttl, nt, nq, rdf, jsonld, trig",
                    output_format
                ))
            })?;

            // Convert validation report to RDF graph
            let report_graph = report.to_graph();

            // Serialize to string
            rdf::serialize_graph_to_string(&report_graph, rdf_format)?
        }
    };

    // Write output
    if let Some(output_path) = output {
        debug!("Writing report to {}", output_path.display());
        std::fs::write(&output_path, &output_text)
            .map_err(|e| ShaclError::Io(format!("Failed to write output file: {}", e)))?;
        info!("Report written to {}", output_path.display());
    } else {
        // Print to stdout
        println!("{}", output_text);
    }

    // Exit with error code if validation failed
    if !report.conforms {
        std::process::exit(1);
    }

    Ok(())
}

fn read_graph_from_file(
    path: &Path,
    format: Option<&str>,
) -> Result<oxigraph::model::Graph, ShaclError> {
    let content = std::fs::read_to_string(path_to_str(path)?).map_err(|e| {
        ShaclError::Io(format!(
            "Failed to read graph file '{}': {}",
            path.display(),
            e
        ))
    })?;

    let effective_format = format.or_else(|| path.extension().and_then(|ext| ext.to_str()));
    let effective_format = effective_format.ok_or_else(|| {
        ShaclError::Parse(format!(
            "Could not infer RDF format for '{}'. Please provide --format.",
            path.display()
        ))
    })?;
    rdf::read_graph_from_string(&content, effective_format)
}