rudof_mcp 0.2.19

RDF data shapes implementation in 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
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
use crate::service::errors::internal_error;
use rmcp::{ErrorData as McpError, model::CallToolResult, model::Content};
use serde::Serialize;
use serde_json::{Value, json};

/// Result type for parsing operations that may produce tool execution errors.
///
/// Tool execution errors (with `isError: true`) are different from protocol errors:
/// - **Tool Execution Errors**: Actionable feedback that LLMs can use to self-correct
///   and retry with adjusted parameters (e.g., invalid format, missing data).
/// - **Protocol Errors**: Issues with the request structure that models are less
///   likely to fix (e.g., unknown tool, malformed request).
pub type ToolResult<T> = Result<T, ToolExecutionError>;

/// Represents a tool execution error that should be returned with `isError: true`.
///
/// This allows LLMs to understand what went wrong and potentially retry
/// with corrected parameters.
#[derive(Debug)]
pub struct ToolExecutionError {
    /// Human-readable error message for the LLM
    pub message: String,
    /// Optional hint for how to fix the error
    pub hint: Option<String>,
}

impl ToolExecutionError {
    /// Create a new tool execution error with a message.
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            hint: None,
        }
    }

    /// Create a tool execution error with a hint for correction.
    pub fn with_hint(message: impl Into<String>, hint: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            hint: Some(hint.into()),
        }
    }

    /// Convert this error into a `CallToolResult` with `isError: true`.
    ///
    /// This is the MCP-recommended way to report errors that LLMs can
    /// use for self-correction.
    pub fn into_call_tool_result(self) -> CallToolResult {
        let text = if let Some(hint) = self.hint {
            format!("{}\n\nHint: {}", self.message, hint)
        } else {
            self.message
        };
        CallToolResult::error(vec![Content::text(text)])
    }
}

impl std::fmt::Display for ToolExecutionError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

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

/// Parse a required input value and return a recoverable tool error when invalid.
pub fn parse_value_with_hint<T, E, F>(value: &str, value_name: &str, hint: &str, parser: F) -> ToolResult<T>
where
    F: FnOnce(&str) -> Result<T, E>,
    E: std::fmt::Display,
{
    parser(value).map_err(|e| ToolExecutionError::with_hint(format!("Invalid {}: {}", value_name, e), hint.to_string()))
}

/// Parse an optional input value and return a recoverable tool error when invalid.
pub fn parse_optional_value_with_hint<T, E, F>(
    value: Option<&str>,
    value_name: &str,
    hint: &str,
    parser: F,
) -> ToolResult<Option<T>>
where
    F: Fn(&str) -> Result<T, E>,
    E: std::fmt::Display,
{
    value
        .map(|raw| parse_value_with_hint(raw, value_name, hint, &parser))
        .transpose()
}

/// Serialize tool response objects into MCP structured content.
pub fn serialize_structured<T: Serialize>(value: &T, operation: &str) -> Result<Value, McpError> {
    serde_json::to_value(value).map_err(|e| {
        internal_error(
            "Serialization error",
            e.to_string(),
            Some(json!({
                "operation": operation,
                "phase": "serialize_response"
            })),
        )
    })
}

/// Shared metadata for URI format resources.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct FormatEntry {
    pub value: &'static str,
    pub name: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub mime_type: Option<&'static str>,
    #[serde(skip_serializing_if = "<[_]>::is_empty")]
    pub extensions: &'static [&'static str],
    pub description: &'static str,
}

/// Shared metadata for mode/sort option resources.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct OptionEntry {
    pub value: &'static str,
    pub name: &'static str,
    pub description: &'static str,
}

/// Shared metadata for query type resources.
#[derive(Debug, Clone, Copy, Serialize)]
pub struct QueryTypeEntry {
    pub name: &'static str,
    pub description: &'static str,
    pub example: &'static str,
}

pub fn format_entries_json(entries: &[FormatEntry], default: &str) -> Value {
    json!({
        "formats": entries,
        "default": default,
    })
}

pub fn option_entries_json(key: &str, entries: &[OptionEntry], default: &str) -> Value {
    json!({
        key: entries,
        "default": default,
    })
}

pub fn query_type_entries_json(entries: &[QueryTypeEntry]) -> Value {
    json!({
        "query_types": entries,
    })
}

/// Default maximum characters included in text previews sent in `content`.
pub const DEFAULT_CONTENT_PREVIEW_CHARS: usize = 1200;

/// Build a Markdown code block preview and append truncation notice when needed.
pub fn code_block_preview(language: &str, text: &str, max_chars: usize) -> String {
    let truncated = text.chars().count() > max_chars;
    let preview: String = text.chars().take(max_chars).collect();
    let display = if truncated { preview.as_str() } else { text };
    let mut block = format!("```{}\n{}\n```", language, display);
    if truncated {
        block.push_str("\n\nPreview truncated. Full output is available in structuredContent.");
    }
    block
}

/// Supported RDF formats as a slice for completions.
pub const RDF_FORMAT_LIST: &[&str] = &["turtle", "ntriples", "rdfxml", "jsonld", "trig", "nquads", "n3"];

pub const RDF_FORMAT_ENTRIES: &[FormatEntry] = &[
    FormatEntry {
        name: "Turtle",
        value: "turtle",
        mime_type: Some("text/turtle"),
        extensions: &[".ttl"],
        description: "Terse RDF Triple Language - human-readable format",
    },
    FormatEntry {
        name: "N-Triples",
        value: "ntriples",
        mime_type: Some("application/n-triples"),
        extensions: &[".nt"],
        description: "Line-based plain text format for RDF",
    },
    FormatEntry {
        name: "RDF/XML",
        value: "rdfxml",
        mime_type: Some("application/rdf+xml"),
        extensions: &[".rdf", ".xml"],
        description: "XML-based RDF serialization",
    },
    FormatEntry {
        name: "JSON-LD",
        value: "jsonld",
        mime_type: Some("application/ld+json"),
        extensions: &[".jsonld"],
        description: "JSON format with linked data support",
    },
    FormatEntry {
        name: "TriG",
        value: "trig",
        mime_type: Some("application/trig"),
        extensions: &[".trig"],
        description: "Extension of Turtle for named graphs",
    },
    FormatEntry {
        name: "N-Quads",
        value: "nquads",
        mime_type: Some("application/n-quads"),
        extensions: &[".nq"],
        description: "Extension of N-Triples for named graphs",
    },
    FormatEntry {
        name: "N3",
        value: "n3",
        mime_type: Some("text/n3"),
        extensions: &[".n3"],
        description: "Notation3 - superset of Turtle",
    },
];

/// Supported RDF formats as a constant for documentation and hints.
pub const RDF_FORMATS: &str = "turtle, ntriples, rdfxml, jsonld, trig, nquads, n3";

/// Supported ShEx formats as a slice for completions.
pub const SHEX_FORMAT_LIST: &[&str] = &[
    "shexc", "shexj", "turtle", "ntriples", "rdfxml", "jsonld", "trig", "n3", "nquads", "internal", "simple", "json",
];

pub const SHEX_FORMAT_ENTRIES: &[FormatEntry] = &[
    FormatEntry {
        name: "ShExC",
        value: "shexc",
        mime_type: Some("text/shex"),
        extensions: &[".shex"],
        description: "ShEx Compact Syntax - human-readable format (default)",
    },
    FormatEntry {
        name: "ShExJ",
        value: "shexj",
        mime_type: Some("application/shex+json"),
        extensions: &[".json"],
        description: "ShEx JSON format",
    },
    FormatEntry {
        name: "Internal",
        value: "internal",
        mime_type: None,
        extensions: &[],
        description: "Internal format for ShEx schemas",
    },
    FormatEntry {
        name: "Simple",
        value: "simple",
        mime_type: None,
        extensions: &[],
        description: "Simplified ShEx format",
    },
    FormatEntry {
        name: "JSON",
        value: "json",
        mime_type: Some("application/json"),
        extensions: &[".json"],
        description: "Plain JSON format",
    },
    FormatEntry {
        name: "JSON-LD",
        value: "jsonld",
        mime_type: Some("application/ld+json"),
        extensions: &[".jsonld"],
        description: "JSON-LD format with linked data support",
    },
    FormatEntry {
        name: "Turtle",
        value: "turtle",
        mime_type: Some("text/turtle"),
        extensions: &[".ttl"],
        description: "Terse RDF Triple Language",
    },
    FormatEntry {
        name: "N-Triples",
        value: "ntriples",
        mime_type: Some("application/n-triples"),
        extensions: &[".nt"],
        description: "Line-based plain text format for RDF",
    },
    FormatEntry {
        name: "RDF/XML",
        value: "rdfxml",
        mime_type: Some("application/rdf+xml"),
        extensions: &[".rdf", ".xml"],
        description: "XML-based RDF serialization",
    },
    FormatEntry {
        name: "TriG",
        value: "trig",
        mime_type: Some("application/trig"),
        extensions: &[".trig"],
        description: "Extension of Turtle for named graphs",
    },
    FormatEntry {
        name: "N3",
        value: "n3",
        mime_type: Some("text/n3"),
        extensions: &[".n3"],
        description: "Notation3 - superset of Turtle with additional features",
    },
    FormatEntry {
        name: "N-Quads",
        value: "nquads",
        mime_type: Some("application/n-quads"),
        extensions: &[".nq"],
        description: "Extension of N-Triples for named graphs",
    },
];

/// Supported ShEx formats as a constant for documentation and hints.
pub const SHEX_FORMATS: &str =
    "shexc, shexj, turtle, ntriples, rdfxml, trig, n3, nquads, json, jsonld, internal, simple";

/// Supported SHACL formats as a slice for completions.
pub const SHACL_FORMAT_LIST: &[&str] = &["turtle", "jsonld", "rdfxml", "trig", "nquads", "json"];

pub const SHACL_FORMAT_ENTRIES: &[FormatEntry] = &[
    FormatEntry {
        name: "Turtle",
        value: "turtle",
        mime_type: Some("text/turtle"),
        extensions: &[".ttl"],
        description: "Turtle serialization for SHACL shapes (default)",
    },
    FormatEntry {
        name: "JSON-LD",
        value: "jsonld",
        mime_type: Some("application/ld+json"),
        extensions: &[".jsonld"],
        description: "JSON-LD representation of SHACL shapes",
    },
    FormatEntry {
        name: "RDF/XML",
        value: "rdfxml",
        mime_type: Some("application/rdf+xml"),
        extensions: &[".rdf", ".xml"],
        description: "RDF/XML serialization",
    },
    FormatEntry {
        name: "TriG",
        value: "trig",
        mime_type: Some("application/trig"),
        extensions: &[".trig"],
        description: "TriG format for named graphs",
    },
    FormatEntry {
        name: "N-Quads",
        value: "nquads",
        mime_type: Some("application/n-quads"),
        extensions: &[".nq"],
        description: "N-Quads format",
    },
    FormatEntry {
        name: "JSON",
        value: "json",
        mime_type: Some("application/json"),
        extensions: &[".json"],
        description: "Plain JSON (when applicable)",
    },
];

/// Supported SHACL formats as a constant for documentation and hints.
pub const SHACL_FORMATS: &str = "turtle, rdfxml, jsonld, trig, nquads, json";

/// Supported ShapeMap formats as a constant for documentation and hints.
pub const SHAPEMAP_FORMATS: &str = "compact";

/// Supported image formats as a constant for documentation and hints.
pub const IMAGE_FORMATS: &str = "svg, png";

/// Supported SPARQL result formats as a constant for documentation and hints.
pub const SPARQL_RESULT_FORMATS: &str = "internal, turtle, ntriples, json-ld, rdf-xml, csv, trig, n3, nquads";

pub const SPARQL_QUERY_RESULT_FORMAT_ENTRIES: &[FormatEntry] = &[
    FormatEntry {
        name: "Internal",
        value: "internal",
        mime_type: None,
        extensions: &[],
        description: "Internal format for query results",
    },
    FormatEntry {
        name: "JSON",
        value: "json",
        mime_type: Some("application/sparql-results+json"),
        extensions: &[".srj", ".json"],
        description: "SPARQL JSON Results format",
    },
    FormatEntry {
        name: "XML",
        value: "xml",
        mime_type: Some("application/sparql-results+xml"),
        extensions: &[".srx", ".xml"],
        description: "SPARQL XML Results format",
    },
    FormatEntry {
        name: "CSV",
        value: "csv",
        mime_type: Some("text/csv"),
        extensions: &[".csv"],
        description: "Comma-separated values format",
    },
    FormatEntry {
        name: "TSV",
        value: "tsv",
        mime_type: Some("text/tab-separated-values"),
        extensions: &[".tsv"],
        description: "Tab-separated values format",
    },
    FormatEntry {
        name: "Turtle",
        value: "turtle",
        mime_type: Some("text/turtle"),
        extensions: &[".ttl"],
        description: "Terse RDF Triple Language (for CONSTRUCT/DESCRIBE)",
    },
    FormatEntry {
        name: "N-Triples",
        value: "ntriples",
        mime_type: Some("application/n-triples"),
        extensions: &[".nt"],
        description: "N-Triples format (for CONSTRUCT/DESCRIBE)",
    },
    FormatEntry {
        name: "RDF/XML",
        value: "rdfxml",
        mime_type: Some("application/rdf+xml"),
        extensions: &[".rdf"],
        description: "RDF/XML format (for CONSTRUCT/DESCRIBE)",
    },
    FormatEntry {
        name: "TriG",
        value: "trig",
        mime_type: Some("application/trig"),
        extensions: &[".trig"],
        description: "Extension of Turtle for named graphs",
    },
];

/// Supported SPARQL query result formats as a slice for completions.
pub const SPARQL_RESULT_FORMAT_LIST: &[&str] = &[
    "internal", "json", "xml", "csv", "tsv", "turtle", "ntriples", "rdfxml", "trig",
];

/// Supported validation result formats as a slice for completions (shared by ShEx and SHACL).
/// Includes all values from both validators: `csv` is valid for ShEx, `minimal` for SHACL.
pub const RESULT_FORMAT_LIST: &[&str] = &[
    "details", "compact", "json", "csv", "minimal", "turtle", "ntriples", "rdfxml", "trig", "n3", "nquads",
];

pub const SHEX_VALIDATION_RESULT_FORMAT_ENTRIES: &[FormatEntry] = &[
    FormatEntry {
        name: "Details",
        value: "details",
        mime_type: None,
        extensions: &[],
        description: "Detailed validation results with full error information (default)",
    },
    FormatEntry {
        name: "Compact",
        value: "compact",
        mime_type: None,
        extensions: &[],
        description: "Compact human-readable validation results",
    },
    FormatEntry {
        name: "JSON",
        value: "json",
        mime_type: Some("application/json"),
        extensions: &[],
        description: "Structured JSON validation results",
    },
    FormatEntry {
        name: "CSV",
        value: "csv",
        mime_type: Some("text/csv"),
        extensions: &[".csv"],
        description: "Validation results in CSV format",
    },
    FormatEntry {
        name: "Turtle",
        value: "turtle",
        mime_type: Some("text/turtle"),
        extensions: &[".ttl"],
        description: "Validation results in Turtle format",
    },
    FormatEntry {
        name: "N-Triples",
        value: "ntriples",
        mime_type: Some("application/n-triples"),
        extensions: &[".nt"],
        description: "Validation results in N-Triples format",
    },
    FormatEntry {
        name: "RDF/XML",
        value: "rdfxml",
        mime_type: Some("application/rdf+xml"),
        extensions: &[".rdf", ".xml"],
        description: "Validation results in RDF/XML format",
    },
    FormatEntry {
        name: "TriG",
        value: "trig",
        mime_type: Some("application/trig"),
        extensions: &[".trig"],
        description: "Validation results in TriG format",
    },
    FormatEntry {
        name: "N3",
        value: "n3",
        mime_type: Some("text/n3"),
        extensions: &[".n3"],
        description: "Validation results in Notation3 format",
    },
    FormatEntry {
        name: "N-Quads",
        value: "nquads",
        mime_type: Some("application/n-quads"),
        extensions: &[".nq"],
        description: "Validation results in N-Quads format",
    },
];

pub const SHACL_VALIDATION_RESULT_FORMAT_ENTRIES: &[FormatEntry] = &[
    FormatEntry {
        name: "Details",
        value: "details",
        mime_type: None,
        extensions: &[],
        description: "Detailed validation results with full error information (default)",
    },
    FormatEntry {
        name: "Compact",
        value: "compact",
        mime_type: None,
        extensions: &[],
        description: "Compact human-readable validation results",
    },
    FormatEntry {
        name: "Minimal",
        value: "minimal",
        mime_type: None,
        extensions: &[],
        description: "Minimal validation output",
    },
    FormatEntry {
        name: "JSON",
        value: "json",
        mime_type: Some("application/json"),
        extensions: &[],
        description: "Structured JSON validation results",
    },
    FormatEntry {
        name: "CSV",
        value: "csv",
        mime_type: Some("text/csv"),
        extensions: &[".csv"],
        description: "Validation results in CSV format",
    },
    FormatEntry {
        name: "Turtle",
        value: "turtle",
        mime_type: Some("text/turtle"),
        extensions: &[".ttl"],
        description: "Validation results in Turtle format",
    },
    FormatEntry {
        name: "N-Triples",
        value: "ntriples",
        mime_type: Some("application/n-triples"),
        extensions: &[".nt"],
        description: "Validation results in N-Triples format",
    },
    FormatEntry {
        name: "RDF/XML",
        value: "rdfxml",
        mime_type: Some("application/rdf+xml"),
        extensions: &[".rdf", ".xml"],
        description: "Validation results in RDF/XML format",
    },
    FormatEntry {
        name: "TriG",
        value: "trig",
        mime_type: Some("application/trig"),
        extensions: &[".trig"],
        description: "Validation results in TriG format",
    },
    FormatEntry {
        name: "N3",
        value: "n3",
        mime_type: Some("text/n3"),
        extensions: &[".n3"],
        description: "Validation results in Notation3 format",
    },
    FormatEntry {
        name: "N-Quads",
        value: "nquads",
        mime_type: Some("application/n-quads"),
        extensions: &[".nq"],
        description: "Validation results in N-Quads format",
    },
];

/// Supported ShEx schema input formats (loading).
pub const SHEX_INPUT_FORMATS_SUPPORTED: &str = "shexc, shexj";

/// Supported ShEx schema output formats (serialization).
pub const SHEX_OUTPUT_FORMATS_SUPPORTED: &str = "shexc, shexj, json, jsonld, internal";

/// Supported ShEx validation result formats as a constant.
pub const SHEX_RESULT_FORMATS: &str = "compact, details, json, csv";

/// Supported SHACL validation result formats as a constant.
pub const SHACL_RESULT_FORMATS: &str = "compact, details, minimal, csv, turtle, ntriples, rdfxml, trig, n3, nquads";

/// Supported reader modes as a constant.
pub const READER_MODES_LIST: &[&str] = &["strict", "lax"];

/// Supported node info modes as a slice for completions.
pub const NODE_INFO_MODE_LIST: &[&str] = &["both", "outgoing", "incoming"];

pub const NODE_MODE_ENTRIES: &[OptionEntry] = &[
    OptionEntry {
        name: "Both",
        value: "both",
        description: "Show both incoming and outgoing relationships",
    },
    OptionEntry {
        name: "Incoming",
        value: "incoming",
        description: "Show only relationships pointing to this node",
    },
    OptionEntry {
        name: "Outgoing",
        value: "outgoing",
        description: "Show only relationships originating from this node",
    },
];

/// Supported node info modes as a constant.
pub const NODE_INFO_MODES: &str = "outgoing, incoming, both";

/// Supported ShEx validation result sort modes as a constant.
pub const SHEX_SORT_BY_MODES: &str = "node, shape, status, details";

pub const READER_MODE_ENTRIES: &[OptionEntry] = &[
    OptionEntry {
        name: "Strict",
        value: "strict",
        description: "Stops validation when there is an error (default)",
    },
    OptionEntry {
        name: "Lax",
        value: "lax",
        description: "Emits a warning and continues processing when errors occur",
    },
];

pub const SHEX_VALIDATION_SORT_OPTION_ENTRIES: &[OptionEntry] = &[
    OptionEntry {
        name: "Node",
        value: "node",
        description: "Sort validation results by RDF node (default)",
    },
    OptionEntry {
        name: "Shape",
        value: "shape",
        description: "Sort validation results by ShEx shape",
    },
    OptionEntry {
        name: "Status",
        value: "status",
        description: "Sort validation results by validation status (conformant/non-conformant)",
    },
    OptionEntry {
        name: "Details",
        value: "details",
        description: "Sort validation results by detail level or error information",
    },
];

/// Supported SHACL validation result sort modes as a constant.
pub const SHACL_SORT_BY_MODES: &str = "severity, node, component, value, path, sourceshape, details";

pub const SHACL_VALIDATION_SORT_OPTION_ENTRIES: &[OptionEntry] = &[
    OptionEntry {
        name: "Severity",
        value: "severity",
        description: "Sort by violation severity: Violation > Warning > Info (default)",
    },
    OptionEntry {
        name: "Node",
        value: "node",
        description: "Sort by the focus node that was validated",
    },
    OptionEntry {
        name: "Component",
        value: "component",
        description: "Sort by SHACL constraint component type",
    },
    OptionEntry {
        name: "Value",
        value: "value",
        description: "Sort by the value that caused the violation",
    },
    OptionEntry {
        name: "Path",
        value: "path",
        description: "Sort by the property path involved in the violation",
    },
    OptionEntry {
        name: "Source Shape",
        value: "sourceshape",
        description: "Sort by the shape that produced the result",
    },
    OptionEntry {
        name: "Details",
        value: "details",
        description: "Sort by result message/details",
    },
];

pub const SPARQL_QUERY_TYPE_ENTRIES: &[QueryTypeEntry] = &[
    QueryTypeEntry {
        name: "SELECT",
        description: "Returns a table of variable bindings",
        example: "SELECT ?subject ?predicate ?object WHERE { ?subject ?predicate ?object }",
    },
    QueryTypeEntry {
        name: "CONSTRUCT",
        description: "Returns an RDF graph constructed by substituting variables",
        example: "CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }",
    },
    QueryTypeEntry {
        name: "ASK",
        description: "Returns a boolean indicating whether a query pattern matches",
        example: "ASK WHERE { ?s ?p ?o }",
    },
    QueryTypeEntry {
        name: "DESCRIBE",
        description: "Returns an RDF graph describing resources",
        example: "DESCRIBE <http://example.org/resource>",
    },
];

/// Build a `ToolExecutionError` for a format that is recognised but not yet implemented.
pub fn unsupported_format_error(format_name: &str, requested: &str, supported: &str) -> ToolExecutionError {
    ToolExecutionError::with_hint(
        format!("Format '{}' is not yet implemented for {}", requested, format_name),
        format!("Currently supported: {}", supported),
    )
}

/// Detect the SPARQL query type by scanning for the first keyword token.
///
/// Returns `Some("SELECT")`, `Some("CONSTRUCT")`, `Some("ASK")`, `Some("DESCRIBE")`,
/// or `None` when no type keyword is found.
pub fn sparql_query_type(query: &str) -> Option<&'static str> {
    for token in query.split_whitespace() {
        match token.to_uppercase().as_str() {
            "SELECT" => return Some("SELECT"),
            "CONSTRUCT" => return Some("CONSTRUCT"),
            "ASK" => return Some("ASK"),
            "DESCRIBE" => return Some("DESCRIBE"),
            _ => {},
        }
    }
    None
}