prttl 0.4.0

Pretty-printer for RDF Turtle
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
// SPDX-FileCopyrightText: 2021-2025 Robin Vobruba <hoijui.quaero@gmail.com>
//
// SPDX-License-Identifier: Apache-2.0

use std::{path::PathBuf, sync::LazyLock};

use clap::{Arg, ArgAction, Command, ValueHint, command, crate_name, value_parser};
use cli_utils::logging;
use const_format::formatcp;
use prttl::options::{FormatOptions, SpecialPredicateOrder, SpecialSubjectTypeOrder};
use thiserror::Error;
use tracing_subscriber::filter::LevelFilter;

pub const A_L_CANONICALIZE: &str = "canonicalize";
// pub const A_S_CANONICALIZE: char = 'C';
pub const A_L_CHECK: &str = "check";
pub const A_S_CHECK: char = 'c';
pub const A_L_FORCE: &str = "force";
pub const A_S_FORCE: char = 'f';
pub const A_L_GENERATE_PRTR_SORTING_IDS: &str = "generate-sorting-ids";
pub const A_S_GENERATE_PRTR_SORTING_IDS: char = 'g';
pub const A_L_INDENTATION: &str = "indentation";
pub const A_S_INDENTATION: char = 'i';
// pub const A_L_INPUT: &str = "input";
// pub const A_S_INPUT: char = 'I';
pub const A_L_LABEL_ALL_BLANK_NODES: &str = "label-all-blank-nodes";
pub const A_S_LABEL_ALL_BLANK_NODES: char = 'l';
pub const A_L_NO_PRTR_SORTING: &str = "no-prtr-sorting";
// pub const A_S_NO_PRTR_SORTING: char = 'p';
pub const A_L_NO_SPARQL_SYNTAX: &str = "no-sparql-syntax";
// pub const A_S_NO_SPARQL_SYNTAX: char = 's';
pub const A_L_PREDICATE_ORDER: &str = "pred-order";
pub const A_L_PREDICATE_ORDER_PRESET: &str = "pred-order-preset";
pub const A_L_PRIORITIZE_INPUT_ORDER: &str = "prioritize-input-order";
pub const A_L_SINGLE_LEAFED_NEW_LINES: &str = "single-leafed-new-lines";
pub const A_S_SINGLE_LEAFED_NEW_LINES: char = 'n';
pub const A_L_SUBJECT_TYPE_ORDER: &str = "subj-type-order";
pub const A_L_SUBJECT_TYPE_ORDER_PRESET: &str = "subj-type-order-preset";
pub const A_L_QUIET: &str = "quiet";
pub const A_S_QUIET: char = 'q';
pub const A_L_VERBOSE: &str = "verbose";
pub const A_S_VERBOSE: char = 'v';
pub const A_L_VERSION: &str = "version";
pub const A_S_VERSION: char = 'V';
pub const A_L_SRC: &str = "src";

pub const DEFAULT_INDENTATION: u8 = 2;
static DEFAULT_INDENTATION_STR: LazyLock<String> =
    LazyLock::new(|| DEFAULT_INDENTATION.to_string());

// /// File(s) or directory to format.
// #[arg()]
// src: Vec<PathBuf>,

fn arg_canonicalize() -> Arg {
    Arg::new(A_L_CANONICALIZE)
        .help("Whether to canonicalize the input before formatting")
        .long_help(
            "Whether to canonicalize the input before formatting. \
This refers to <https://www.w3.org/TR/rdf-canon/>, \
and effectively just label the blank nodes in a uniform way.",
        )
        .action(ArgAction::SetTrue)
        // .short(A_S_CANONICALIZE)
        .long(A_L_CANONICALIZE)
}

fn arg_check() -> Arg {
    Arg::new(A_L_CHECK)
        .help(
            "Do not edit the file but only check \
if it already applies this tools format",
        )
        .action(ArgAction::SetTrue)
        .short(A_S_CHECK)
        .long(A_L_CHECK)
}

fn arg_force() -> Arg {
    Arg::new(A_L_FORCE)
        .help(
            "Forces overwriting of the output file, \
if it already exists, which includes the case of the input and output file \
being equal",
        )
        // Whether to force-write the output,
        // even if potential issues with the formatting have been detected.
        .action(ArgAction::SetTrue)
        .short(A_S_FORCE)
        .long(A_L_FORCE)
}

fn arg_generate_sorting_ids() -> Arg {
    Arg::new(A_L_GENERATE_PRTR_SORTING_IDS)
        .help(
            "Whether to extend the parsed data with `prtr:sortingId`s for blank nodes \
that do not yet have one assigned",
        )
        .action(ArgAction::SetTrue)
        .short(A_S_GENERATE_PRTR_SORTING_IDS)
        .long(A_L_GENERATE_PRTR_SORTING_IDS)
}

fn arg_label_all_blank_nodes() -> Arg {
    Arg::new(A_L_LABEL_ALL_BLANK_NODES)
        .help(
            "Whether to disable sorting of blank nodes \
using their `prtr:sortingId` value, if any",
        )
        .long_help(
            "Whether to use labels for all blank nodes, \
or rather maximize nesting of them. \
 \
NOTE That blank nodes referenced in more then one place can never be nested.",
        )
        .action(ArgAction::SetTrue)
        .short(A_S_LABEL_ALL_BLANK_NODES)
        .long(A_L_LABEL_ALL_BLANK_NODES)
}

fn arg_indentation() -> Arg {
    Arg::new(A_L_INDENTATION)
        .help("Number of spaces per level of indentation")
        .num_args(1)
        .short(A_S_INDENTATION)
        .long(A_L_INDENTATION)
        .action(ArgAction::Set)
        // .value_hint(ValueHint::Other)
        .value_name("NUM")
        .value_parser(value_parser!(u8).range(1..))
        .default_value(DEFAULT_INDENTATION_STR.as_str())
}

// fn arg_input() -> Arg {
//     Arg::new(A_L_INPUT)
//         .help("an input RDF file to pretty print to Turtle; '-' for stdin")
//         .num_args(1)
//         .short(A_S_INPUT)
//         .long(A_L_INPUT)
//         .action(ArgAction::Set)
//         .value_hint(ValueHint::FilePath)
//         .value_name("FILE")
//         .default_value("-")
// }

fn arg_no_prtr_sorting() -> Arg {
    Arg::new(A_L_NO_PRTR_SORTING)
        .help(
            "Whether to disable sorting of blank nodes \
using their `prtr:sortingId` value, if any",
        )
        .long_help(
            "Whether to disable sorting of blank nodes \
using their `prtr:sortingId` value, if any. \
\
[`prtr`](https://codeberg.org/elevont/prtr) \
is an ontology concerned with \
[RDF Pretty Printing](https://www.w3.org/DesignIssues/Pretty.html).",
        )
        .action(ArgAction::SetTrue)
        // .short(A_S_NO_PRTR_SORTING)
        .long(A_L_NO_PRTR_SORTING)
}

fn arg_no_sparql_syntax() -> Arg {
    Arg::new(A_L_NO_SPARQL_SYNTAX)
        .help(
            "Whether to use SPARQL-ish syntax for base and prefix, \
or the traditional Turtle syntax",
        )
        .long_help(
            "Whether to use SPARQL-ish syntax for base and prefix, \
or the traditional Turtle syntax. \
 \
- SPARQL-ish: \
 \
```turtle \
BASE <http://example.com/> \
PREFIX foaf: <http://xmlns.com/foaf/0.1/> \
``` \
 \
- Traditional Turtle: \
 \
```turtle \
@base <http://example.com/> . \
@prefix foaf: <http://xmlns.com/foaf/0.1/> . \
``` \
",
        )
        .action(ArgAction::SetTrue)
        // .short(A_S_NO_SPARQL_SYNTAX)
        .long(A_L_NO_SPARQL_SYNTAX)
}

fn arg_predicate_order() -> Arg {
    Arg::new(A_L_PREDICATE_ORDER)
        .help(
            "Sets a custom order of predicates to be used for sorting.
Predicates that match come first; in the provided order.
Predicates that do not match come afterwards; in alphabetical order.

You may specify predicate names as absolute IRIs or as prefixed names.
Only direct matches are considered; meaning: No type inference is conducted.",
        )
        .long(A_L_PREDICATE_ORDER)
        .num_args(0..)
        .value_name("PREDICATE")
        .value_hint(ValueHint::Other)
        .action(ArgAction::Append)
}

fn arg_predicate_order_preset() -> Arg {
    Arg::new(A_L_PREDICATE_ORDER_PRESET)
        .help(
            "Sets a predefined order of predicates to be used for sorting.
Predicates that match come first; in the provided order.
Predicates that do not match come afterwards; in alphabetical order.

You may specify predicate names as absolute IRIs or as prefixed names.
Only direct matches are considered; meaning: No type inference is conducted.",
        )
        .long(A_L_PREDICATE_ORDER_PRESET)
        .value_name("PREDICATE_ORDER_PRESET")
        .value_parser(value_parser!(SpecialPredicateOrder))
        .action(ArgAction::Set)
}

fn arg_prioritize_input_order() -> Arg {
    Arg::new(A_L_PRIORITIZE_INPUT_ORDER)
        .help("Prioritize maintaining the input order vs keeping already assigned sorting IDs")
        .long_help(
            "Prioritize maintaining the input order vs keeping already assigned sorting IDs.

If we are to generate `prtr:sortingId` values for blank nodes
that do not yet have one but need one (see [`Self::generate_sorting_ids`],
how should we do it?

1. If `prioritize_input_order = true`,
    we will ensure `prtr:sortingId` values are assigned in the order
    the blank nodes appear in the input.
    This means, we will also potentially assign different values
    to blank nodes that already have a `prtr:sortingId` value.
    This on the other hand, may make the result less compatible with
    other versions/branches of the same file,
    that do not have this (same) reassignment.

    We suggest to only choose this if the order your nodes are in
    within the input is more important to you
    then to keep the already assigned sorting IDs.
    or if a scenario of merging conflicts
    with other versions of the file is very unlikely.
2. If `prioritize_input_order = false`,
   already assigned sorting IDs will be preserved to 100%,
   and the blank nodes without ID will only remain
   within the same order as in the input,
   if there are enough free IDs between the already assigned ones.",
        )
        .action(ArgAction::SetTrue)
        .long(A_L_PRIORITIZE_INPUT_ORDER)
}

fn arg_single_entry_on_new_line() -> Arg {
    Arg::new(A_L_SINGLE_LEAFED_NEW_LINES)
        .help("Whether to move a single/lone predicate-object pair or object alone onto a new line")
        .action(ArgAction::SetTrue)
        .short(A_S_SINGLE_LEAFED_NEW_LINES)
        .long(A_L_SINGLE_LEAFED_NEW_LINES)
}

fn arg_subject_type_order() -> Arg {
    Arg::new(A_L_SUBJECT_TYPE_ORDER)
        .help(
            "Sets a custom order of subject types to be used for sorting.
Subjects with a matching type come first; in the provided order.
Subjects without any matching type come afterwards; in alphabetical order.

You may specify subject type names as absolute IRIs or as prefixed names.
Only direct matches are considered; meaning: No type inference is conducted.",
        )
        .long(A_L_SUBJECT_TYPE_ORDER)
        .num_args(0..)
        .value_name("SUBJECT_TYPE")
        .value_hint(ValueHint::Other)
        .action(ArgAction::Append)
}

fn arg_subject_type_order_preset() -> Arg {
    Arg::new(A_L_SUBJECT_TYPE_ORDER_PRESET)
        .help(
            "Sets a predefined order of subject types to be used for sorting.
Subjects with a matching type come first; in the provided order.
Subjects without any matching type come afterwards; in alphabetical order.

Only direct matches are considered; meaning: No type inference is conducted.",
        )
        .long(A_L_SUBJECT_TYPE_ORDER_PRESET)
        .value_name("SUBJECT_TYPE_ORDER_PRESET")
        .value_parser(value_parser!(SpecialSubjectTypeOrder))
        .action(ArgAction::Set)
}

fn arg_quiet() -> Arg {
    Arg::new(A_L_QUIET)
        .help("Minimize or suppress output to stdout")
        .long_help("Minimize or suppress output to stdout, and only shows log output on stderr.")
        .action(ArgAction::SetTrue)
        .short(A_S_QUIET)
        .long(A_L_QUIET)
        .conflicts_with(A_L_VERBOSE)
}

fn arg_verbose() -> Arg {
    Arg::new(A_L_VERBOSE)
        .help("more verbose output (useful for debugging)")
        .short(A_S_VERBOSE)
        .long(A_L_VERBOSE)
        .action(ArgAction::SetTrue)
}

fn arg_version() -> Arg {
    Arg::new(A_L_VERSION)
        .help(formatcp!(
            "Print version information and exit. \
May be combined with -{A_S_QUIET},--{A_L_QUIET}, \
to really only output the version string."
        ))
        .short(A_S_VERSION)
        .long(A_L_VERSION)
        .action(ArgAction::SetTrue)
}

fn arg_src() -> Arg {
    Arg::new(A_L_SRC)
        .help("Source RDF file(s) or director(y|ies) containing Turtle files to format")
        .num_args(1..)
        .value_name("FILE_OR_DIR")
        .value_hint(ValueHint::Other)
        .value_parser(value_parser!(PathBuf))
        .required(true)
        .action(ArgAction::Set)
}

fn args_matcher() -> Command {
    command!()
        .about("Pretty prints RDF/Turtle files")
        .long_about(
            "Takes RDF data as input (commonly a Turtle file), \
and generates diff optimized RDF/Turtle, \
using a lot of new-lines. \
 \
One peculiarity of this tool is, \
that it removes (Turtle-syntax) comments. \
We do this, because we believe that all comments \
should rather be encoded into triples, \
and we celebrate this in our own data, \
specifically our ontologies. \
More about this: \
<https://codeberg.org/elevont/cmt-ont>",
        )
        .bin_name(clap::crate_name!())
        .help_expected(true)
        .disable_version_flag(true)
        .arg(arg_canonicalize())
        .arg(arg_check())
        .arg(arg_force())
        .arg(arg_generate_sorting_ids())
        .arg(arg_label_all_blank_nodes())
        .arg(arg_indentation())
        // .arg(arg_input())
        // .arg(arg_output())
        .arg(arg_no_prtr_sorting())
        .arg(arg_no_sparql_syntax())
        .arg(arg_predicate_order())
        .arg(arg_predicate_order_preset())
        .arg(arg_prioritize_input_order())
        .arg(arg_single_entry_on_new_line())
        .arg(arg_subject_type_order())
        .arg(arg_subject_type_order_preset())
        .arg(arg_quiet())
        .arg(arg_verbose())
        .arg(arg_version())
        .arg(arg_src())
}

#[allow(clippy::print_stdout)]
fn print_version_and_exit(quiet: bool) {
    if !quiet {
        print!("{} ", clap::crate_name!());
    }
    println!("{}", prttl::VERSION);
    std::process::exit(0);
}

#[derive(Error, Debug)]
pub enum InitError {
    #[error("Failed to init logging system: {0}")]
    LogInit(#[from] tracing_subscriber::util::TryInitError),

    #[error("Failed to change the logging level: {0}")]
    LogChangeLevel(#[from] tracing_subscriber::reload::Error),
}

pub fn init() -> Result<(FormatOptions, Vec<PathBuf>), InitError> {
    let log_reload_handle = logging::setup(crate_name!())?;
    let args = args_matcher().get_matches();

    let quiet = args.get_flag(A_L_QUIET);
    let version = args.get_flag(A_L_VERSION);
    if version {
        print_version_and_exit(quiet);
    }

    let verbose = args.get_flag(A_L_VERBOSE);
    let log_level = if verbose {
        LevelFilter::TRACE
    } else if quiet {
        LevelFilter::WARN
    } else {
        LevelFilter::INFO
    };
    logging::set_log_level_tracing(&log_reload_handle, log_level)?;

    let canonicalize = args.get_flag(A_L_CANONICALIZE);
    let check = args.get_flag(A_L_CHECK);
    let force = args.get_flag(A_L_FORCE);
    let generate_sorting_ids = args.get_flag(A_L_GENERATE_PRTR_SORTING_IDS);
    let prioritize_input_order = args.get_flag(A_L_PRIORITIZE_INPUT_ORDER);
    let indentation_spaces = args
        .get_one::<u8>(A_L_INDENTATION)
        .copied()
        .unwrap_or(DEFAULT_INDENTATION)
        .into();
    let max_nesting = !args.get_flag(A_L_LABEL_ALL_BLANK_NODES);
    let prtr_sorting = !args.get_flag(A_L_NO_PRTR_SORTING);
    let sparql_syntax = !args.get_flag(A_L_NO_SPARQL_SYNTAX);
    let single_leafed_new_lines = args.get_flag(A_L_SINGLE_LEAFED_NEW_LINES);
    let warn_unsupported_numbers = true;

    let predicate_order: Option<Vec<String>> = args
        .get_many::<String>(A_L_PREDICATE_ORDER)
        .map(|vals| vals.cloned().collect());
    let predicate_order_preset: Option<SpecialPredicateOrder> = args
        .get_one::<SpecialPredicateOrder>(A_L_PREDICATE_ORDER_PRESET)
        .copied();
    let subject_type_order: Option<Vec<String>> = args
        .get_many::<String>(A_L_SUBJECT_TYPE_ORDER)
        .map(|vals| vals.cloned().collect());
    let subject_type_order_preset: Option<SpecialSubjectTypeOrder> = args
        .get_one::<SpecialSubjectTypeOrder>(A_L_SUBJECT_TYPE_ORDER_PRESET)
        .copied();

    let indentation = " ".repeat(indentation_spaces);
    let src: Vec<PathBuf> = args
        .get_many::<PathBuf>(A_L_SRC)
        .unwrap()
        .cloned()
        .collect();
    Ok((
        FormatOptions {
            check,
            indentation,
            single_leafed_new_lines,
            force,
            generate_sorting_ids,
            prioritize_input_order,
            prtr_sorting,
            sparql_syntax,
            max_nesting,
            canonicalize,
            warn_unsupported_numbers,
            subject_type_order_preset,
            subject_type_order,
            predicate_order_preset,
            predicate_order,
        },
        src,
    ))
}