codeint-config 0.10.3

Configuration and CLI argument parsing for codeint
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
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
use std::path::PathBuf;

use crate::OutputFormat;
use crate::PluginKind;
use crate::PluginLimits;
use crate::calendar_day::CalendarDay;
use crate::churn::{ChurnFormat, ChurnWindow};
use crate::coupling::CouplingSettings;
use crate::resolved::ConfigOverrides;

pub(crate) const DEFAULT_PLUGIN_DOWNLOAD_TIMEOUT_SECONDS: u64 = 60;
pub const DEFAULT_PLUGIN_MAX_DOWNLOAD_BYTES: u64 = 50 * 1024 * 1024;
pub(crate) const MAX_PLUGIN_DOWNLOAD_MIB: u64 = (i64::MAX as u64) / (1024 * 1024);
pub(crate) const WORST_FINDINGS_PER_METRIC: usize = 20;
pub const DEFAULT_MAX_TRACE_PATHS: usize = 10;
pub const DEFAULT_RENAME_THRESHOLD: u8 = 50;
pub const MAX_RENAME_THRESHOLD: u8 = 100;
pub const DEFAULT_UPDATE_CONNECT_TIMEOUT_SECONDS: u64 = 15;
pub const DEFAULT_UPDATE_METADATA_MAX_TIME_SECONDS: u64 = 30;
pub const DEFAULT_UPDATE_DOWNLOAD_MAX_TIME_SECONDS: u64 = 300;
pub const DEFAULT_CHECK_RUN_NAME: &str = "codeint";
pub const CHECK_RUN_ANNOTATIONS_PER_REQUEST: usize = 50;
pub const DEFAULT_CHECK_RUN_MAX_ANNOTATIONS: usize = 250;
pub const CHECK_RUN_SUMMARY_MAX_BYTES: usize = 65_535;
pub const CHECK_RUN_ANNOTATION_MESSAGE_MAX_BYTES: usize = 65_535;
pub const CHECK_RUN_API_ERROR_MAX_CHARS: usize = 200;
pub const DEFAULT_CHECK_RUN_CONNECT_TIMEOUT_SECONDS: u64 = 15;
pub const DEFAULT_CHECK_RUN_MAX_TIME_SECONDS: u64 = 30;
pub const DEFAULT_GITHUB_API_URL: &str = "https://api.github.com";
const LATEST_COMMITS_OF_A_TREND: usize = 20;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PluginHostConfig {
    pub cache_dir: Option<PathBuf>,
    pub ca_cert: Option<PathBuf>,
    pub download_timeout_seconds: u64,
    pub max_download_bytes: u64,
    pub compiled_plugin_cache: Option<PathBuf>,
}

impl Default for PluginHostConfig {
    fn default() -> PluginHostConfig {
        PluginHostConfig {
            cache_dir: None,
            ca_cert: None,
            download_timeout_seconds: DEFAULT_PLUGIN_DOWNLOAD_TIMEOUT_SECONDS,
            max_download_bytes: DEFAULT_PLUGIN_MAX_DOWNLOAD_BYTES,
            compiled_plugin_cache: None,
        }
    }
}

#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    #[error("cannot read the current folder")]
    CurrentFolder(#[source] std::io::Error),
    #[error("{} does not exist", path.display())]
    MissingPath { path: PathBuf },
    #[error("{} is not a folder", path.display())]
    NotAFolder { path: PathBuf },
    #[error("cannot open {}", path.display())]
    UnreadablePath {
        path: PathBuf,
        #[source]
        source: std::io::Error,
    },
    #[error(
        "cannot write the report to {}: {} is not a folder that exists",
        path.display(),
        folder.display()
    )]
    MissingOutputFolder { path: PathBuf, folder: PathBuf },
    #[error("cannot determine the home folder for agent configuration")]
    MissingAgentHome,
    #[error(
        "CODEINT_PLUGIN_MAX_DOWNLOAD_MB must be a positive integer in MiB up to {MAX_PLUGIN_DOWNLOAD_MIB}, got {value:?}"
    )]
    InvalidPluginMaxDownloadMb { value: String },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReportDestination {
    StandardOutput,
    File(PathBuf),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReportInput {
    StandardInput,
    File(PathBuf),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FindingsShown {
    WorstPerMetric(usize),
    All,
}

impl Default for FindingsShown {
    fn default() -> FindingsShown {
        FindingsShown::WorstPerMetric(WORST_FINDINGS_PER_METRIC)
    }
}

macro_rules! impl_full_output_format {
    ($target:ident) => {
        impl From<OutputFormat> for $target {
            fn from(format: OutputFormat) -> $target {
                match format {
                    OutputFormat::Toon => $target::Toon,
                    OutputFormat::Text => $target::Text,
                    OutputFormat::Json => $target::Json,
                    OutputFormat::Sarif => $target::Sarif,
                    OutputFormat::Markdown => $target::Markdown,
                }
            }
        }
    };
}

macro_rules! impl_subset_output_format {
    ($target:ident, $supported:expr, $($from:ident => $to:ident),+ $(,)?) => {
        impl $target {
            pub const SUPPORTED: &'static [&'static str] = $supported;

            pub fn try_from_output_format(format: OutputFormat) -> Option<$target> {
                match format {
                    $(OutputFormat::$from => Some($target::$to),)+
                    _ => None,
                }
            }
        }
    };
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum ReportFormat {
    Toon,
    Text,
    Json,
    Sarif,
    Markdown,
}

impl_full_output_format!(ReportFormat);

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, clap::ValueEnum)]
pub enum CheckRunConclusionMode {
    #[default]
    Auto,
    Neutral,
    Failure,
}

#[derive(Clone, PartialEq, Eq)]
pub struct CheckRunPublishConfig {
    pub repository: Option<String>,
    pub api_url: String,
    pub token: Option<String>,
    pub connect_timeout_seconds: u64,
    pub max_time_seconds: u64,
}

impl CheckRunPublishConfig {
    pub fn new(
        repository: Option<String>,
        api_url: String,
        token: Option<String>,
    ) -> CheckRunPublishConfig {
        CheckRunPublishConfig {
            repository,
            api_url,
            token,
            connect_timeout_seconds: DEFAULT_CHECK_RUN_CONNECT_TIMEOUT_SECONDS,
            max_time_seconds: DEFAULT_CHECK_RUN_MAX_TIME_SECONDS,
        }
    }
}

impl std::fmt::Debug for CheckRunPublishConfig {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("CheckRunPublishConfig")
            .field("repository", &self.repository)
            .field("api_url", &self.api_url)
            .field("token", &self.token.as_ref().map(|_| "[REDACTED]"))
            .field("connect_timeout_seconds", &self.connect_timeout_seconds)
            .field("max_time_seconds", &self.max_time_seconds)
            .finish()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CheckRunConfig {
    pub name: String,
    pub base_ref: Option<String>,
    pub env_base_ref: Option<String>,
    pub head_sha: Option<String>,
    pub conclusion: CheckRunConclusionMode,
    pub max_annotations: usize,
    pub publish: Option<CheckRunPublishConfig>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ReportMode {
    Render {
        all: bool,
        format: Option<ReportRenderFormat>,
    },
    CheckRun(CheckRunConfig),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum ReportRenderFormat {
    Toon,
    Text,
    Json,
    Sarif,
    Markdown,
}

impl_full_output_format!(ReportRenderFormat);

impl ReportRenderFormat {
    pub fn as_report_format(self) -> ReportFormat {
        match self {
            ReportRenderFormat::Toon => ReportFormat::Toon,
            ReportRenderFormat::Text => ReportFormat::Text,
            ReportRenderFormat::Json => ReportFormat::Json,
            ReportRenderFormat::Sarif => ReportFormat::Sarif,
            ReportRenderFormat::Markdown => ReportFormat::Markdown,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum TrendFormat {
    Toon,
    Text,
    Json,
    Markdown,
}

impl_subset_output_format!(
    TrendFormat,
    &["toon", "text", "json", "markdown"],
    Toon => Toon,
    Text => Text,
    Json => Json,
    Markdown => Markdown,
);

impl_subset_output_format!(
    ChurnFormat,
    &["toon", "text", "json", "markdown"],
    Toon => Toon,
    Text => Text,
    Json => Json,
    Markdown => Markdown,
);

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum GuardFormat {
    Text,
    Toon,
    Json,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum SchemaFormat {
    Json,
    Toon,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, clap::ValueEnum)]
pub enum InitFormat {
    #[default]
    Jsonc,
    Json,
    Toml,
    Yaml,
}

impl InitFormat {
    pub fn file_name(self) -> &'static str {
        match self {
            InitFormat::Jsonc => "codeint.jsonc",
            InitFormat::Json => "codeint.json",
            InitFormat::Toml => "codeint.toml",
            InitFormat::Yaml => "codeint.yaml",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum ConfigFormat {
    Text,
    Toon,
    Json,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum AgentFormat {
    Text,
    Toon,
    Json,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum RatchetFormat {
    Text,
    Toon,
    Json,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum BaselineFormat {
    Text,
    Toon,
    Json,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum StateCheckFormat {
    Toon,
    Text,
    Json,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum DoctorFormat {
    Text,
    Json,
    Toon,
}

pub type PluginTestFormat = DoctorFormat;

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum ImpactFormat {
    Toon,
    Json,
    Text,
}

impl_subset_output_format!(
    ImpactFormat,
    &["toon", "text", "json"],
    Toon => Toon,
    Text => Text,
    Json => Json,
);

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum TraceFormat {
    Toon,
    Json,
    Text,
}

impl_subset_output_format!(
    TraceFormat,
    &["toon", "text", "json"],
    Toon => Toon,
    Text => Text,
    Json => Json,
);

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum AgentClient {
    ClaudeCode,
    Cursor,
    Codex,
}

impl AgentClient {
    pub const ALL: [AgentClient; 3] = [
        AgentClient::ClaudeCode,
        AgentClient::Cursor,
        AgentClient::Codex,
    ];

    pub fn as_str(self) -> &'static str {
        match self {
            AgentClient::ClaudeCode => "claude-code",
            AgentClient::Cursor => "cursor",
            AgentClient::Codex => "codex",
        }
    }

    pub fn display_name(self) -> &'static str {
        match self {
            AgentClient::ClaudeCode => "Claude Code",
            AgentClient::Cursor => "Cursor",
            AgentClient::Codex => "Codex",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AgentCommand {
    Status {
        config_root: PathBuf,
        client: Option<AgentClient>,
        format: AgentFormat,
    },
    Install {
        config_root: PathBuf,
        client: Option<AgentClient>,
        dry_run: bool,
        format: AgentFormat,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub enum PluginCommand {
    Test {
        plugin_host: PluginHostConfig,
        path: PathBuf,
        kind: PluginKind,
        sources: Vec<PathBuf>,
        limits: PluginLimits,
        format: PluginTestFormat,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StateUpdateCommand<F> {
    pub folder: PathBuf,
    pub reset: bool,
    pub coverage_reports: Vec<PathBuf>,
    pub format: F,
    pub plugin_host: PluginHostConfig,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RatchetCommand {
    Update(StateUpdateCommand<RatchetFormat>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BaselineCommand {
    Update(StateUpdateCommand<BaselineFormat>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StateCommand {
    Check {
        folder: PathBuf,
        base_ref: String,
        head_ref: Option<String>,
        rename_threshold: u8,
        destination: ReportDestination,
        format: StateCheckFormat,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrendRange {
    Latest(usize),
    Since(CalendarDay),
}

impl Default for TrendRange {
    fn default() -> TrendRange {
        TrendRange::Latest(LATEST_COMMITS_OF_A_TREND)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum Command {
    Check {
        folder: PathBuf,
        destination: ReportDestination,
        findings: FindingsShown,
        format: Option<ReportFormat>,
        package: Option<String>,
        owner: Option<String>,
        coverage_reports: Vec<PathBuf>,
        ignore_baseline: bool,
        plugin_host: PluginHostConfig,
    },
    Diff {
        folder: PathBuf,
        base_ref: String,
        head_ref: Option<String>,
        rename_threshold: u8,
        destination: ReportDestination,
        findings: FindingsShown,
        format: Option<ReportFormat>,
        package: Option<String>,
        owner: Option<String>,
        result_cache: Option<PathBuf>,
        plugin_host: PluginHostConfig,
    },
    Trend {
        folder: PathBuf,
        range: TrendRange,
        destination: ReportDestination,
        format: Option<TrendFormat>,
        result_cache: Option<PathBuf>,
        plugin_host: PluginHostConfig,
    },
    Impact {
        folder: PathBuf,
        paths: Vec<String>,
        changed: Option<String>,
        depth: Option<usize>,
        package: Option<String>,
        destination: ReportDestination,
        format: Option<ImpactFormat>,
        plugin_host: PluginHostConfig,
    },
    Trace {
        folder: PathBuf,
        from: String,
        to: String,
        all: bool,
        max_paths: usize,
        package: Option<String>,
        destination: ReportDestination,
        format: Option<TraceFormat>,
        plugin_host: PluginHostConfig,
    },
    Init {
        folder: PathBuf,
        adopt: bool,
        format: InitFormat,
        plugin_host: PluginHostConfig,
    },
    Update {
        check_only: bool,
        version: Option<String>,
        release_base_url: Option<String>,
    },
    Mcp {
        folder: PathBuf,
        result_cache: Option<PathBuf>,
        plugin_host: PluginHostConfig,
    },
    Guard {
        folder: PathBuf,
        target: PathBuf,
        format: GuardFormat,
    },
    Schema {
        destination: ReportDestination,
        format: SchemaFormat,
    },
    Config {
        folder: PathBuf,
        format: ConfigFormat,
        overrides: ConfigOverrides,
    },
    Report {
        folder: PathBuf,
        input: ReportInput,
        destination: ReportDestination,
        mode: ReportMode,
    },
    Doctor {
        folder: PathBuf,
        format: DoctorFormat,
        plugin_host: PluginHostConfig,
    },
    Agent(AgentCommand),
    Ratchet(RatchetCommand),
    Plugin(PluginCommand),
    Baseline(BaselineCommand),
    State(StateCommand),
    Churn {
        folder: PathBuf,
        window: ChurnWindow,
        destination: ReportDestination,
        findings: FindingsShown,
        format: Option<ChurnFormat>,
        plugin_host: PluginHostConfig,
    },
    Coupling {
        folder: PathBuf,
        settings: CouplingSettings,
        destination: ReportDestination,
        findings: FindingsShown,
        format: Option<ChurnFormat>,
        plugin_host: PluginHostConfig,
    },
}

#[derive(Debug)]
pub struct Invocation {
    pub command: Command,
}