influxdb3-plugin-sdk 0.4.0

Author-side packaging library for InfluxDB 3 plugins.
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
//! Error types for the SDK.
//!
//! Error layers:
//! - [`SdkError`] — crate-level error returned by every public function.
//! - [`ValidationFailure`] — the focused, validation-only error returned by
//!   the `validate::plugin_dir` wrapper. Either [`ValidationFailure::Invalid`]
//!   (a batch of [`ValidationError`]s), [`ValidationFailure::Io`], or
//!   [`ValidationFailure::InvalidExcludePattern`] (a `[plugin].exclude` entry
//!   that is not a valid gitignore pattern).
//! - [`ValidationReport`] — accumulator that collects [`ValidationError`]s
//!   during a pass and converts to a [`ValidationFailure`].
//!
//! The diagnostic type [`ValidationError`] lives in `influxdb3-plugin-schemas`
//! (the validation contract); it is re-imported here.

use influxdb3_plugin_schemas::{SchemaError, SchemaErrors, ValidationError};
use std::path::PathBuf;

/// Crate-level error type.
///
/// `#[non_exhaustive]`: variant additions are not breaking; field additions
/// to existing variants are — introduce a new variant and deprecate the old.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum SdkError {
    #[error("I/O error{}", path_suffix(.path.as_ref()))]
    Io {
        #[source]
        source: std::io::Error,
        path: Option<PathBuf>,
    },

    #[error(transparent)]
    Schema(#[from] SchemaError),

    #[error("{} validation error(s) found", .0.len())]
    ValidationErrors(Vec<ValidationError>),

    #[error("archive construction failed: {message}")]
    Archive { message: String },

    #[error(
        "archive path {archive_path:?} exceeds ustar split-path limit ({limit} bytes); shorten file paths or the plugin name/version"
    )]
    PathTooLong { archive_path: String, limit: usize },

    #[error("hash computation failed")]
    Hash {
        #[source]
        source: std::io::Error,
    },

    /// Surfaces from [`crate::mutate_index::add_entry`] when
    /// `(name, version)` already exists in the input index. `existing_versions`
    /// enumerates every version of `name` already in the index (in index order)
    /// so the author can pick a higher version or `yank` the conflicting one.
    #[error(
        "plugin ({name:?}, {version:?}) already exists in the target index; \
         existing versions: {existing_versions:?}"
    )]
    AlreadyPublished {
        name: String,
        version: String,
        existing_versions: Vec<String>,
    },

    /// Surfaces from [`crate::mutate_index::add_entry`] when the incoming
    /// plugin name canonicalizes to an existing entry's canonical form but
    /// spellings differ (e.g., `my-plugin` vs `my_plugin`, or `MyPlugin`
    /// vs `myplugin`). `existing` enumerates every `(spelling, version)`
    /// already in the index under that canonical form, preserving index
    /// order.
    #[error(
        "canonical collision: plugin name {name:?} conflicts with existing \
         entries sharing canonical form {canonical:?}: {existing:?}"
    )]
    CanonicalCollision {
        name: String,
        canonical: String,
        existing: Vec<(String, semver::Version)>,
    },

    #[error("plugin ({name:?}, {version:?}) is not present in the target index")]
    EntryNotFound { name: String, version: String },

    /// A manifest `[plugin].exclude` entry is not a valid gitignore pattern.
    /// Surfaces from any command that performs source-file selection.
    #[error("invalid exclude pattern {pattern:?}: {message}")]
    InvalidExcludePattern { pattern: String, message: String },
}

impl SdkError {
    /// Stable tag per variant. The exhaustive match forces new variants to
    /// be registered with test fixtures (drift guard).
    pub fn variant_name(&self) -> &'static str {
        match self {
            Self::Io { .. } => "Io",
            Self::Schema(_) => "Schema",
            Self::ValidationErrors(_) => "ValidationErrors",
            Self::Archive { .. } => "Archive",
            Self::PathTooLong { .. } => "PathTooLong",
            Self::Hash { .. } => "Hash",
            Self::AlreadyPublished { .. } => "AlreadyPublished",
            Self::CanonicalCollision { .. } => "CanonicalCollision",
            Self::EntryNotFound { .. } => "EntryNotFound",
            Self::InvalidExcludePattern { .. } => "InvalidExcludePattern",
        }
    }
}

fn path_suffix(path: Option<&PathBuf>) -> String {
    match path {
        Some(p) => format!(" at {}", p.display()),
        None => String::new(),
    }
}

/// Adapts schemas-layer `SchemaErrors` into `SdkError::ValidationErrors`.
/// Each `ReportedError` becomes one [`ValidationError::SchemaReported`],
/// preserving field paths and inner `SchemaError` variants without lossy
/// string-squashing. This is the canonical `?`-conversion path for callers
/// of `Manifest::parse_toml` / `Index::parse_json`.
impl From<SchemaErrors> for SdkError {
    fn from(errors: SchemaErrors) -> Self {
        let diagnostics = errors
            .into_iter()
            .map(ValidationError::SchemaReported)
            .collect();
        SdkError::ValidationErrors(diagnostics)
    }
}

impl From<influxdb3_plugin_schemas::IndexInsertError> for SdkError {
    fn from(err: influxdb3_plugin_schemas::IndexInsertError) -> Self {
        use influxdb3_plugin_schemas::IndexInsertError;
        match err {
            IndexInsertError::Duplicate {
                name,
                version,
                existing_versions,
            } => SdkError::AlreadyPublished {
                name,
                version: version.to_string(),
                existing_versions: existing_versions.iter().map(|v| v.to_string()).collect(),
            },
            IndexInsertError::CanonicalCollision {
                name,
                canonical,
                existing,
            } => SdkError::CanonicalCollision {
                name,
                canonical,
                existing,
            },
            _ => unreachable!(
                "IndexInsertError has no other variants in the current schemas version"
            ),
        }
    }
}

impl From<crate::plugin_source_files::SelectError> for SdkError {
    fn from(err: crate::plugin_source_files::SelectError) -> Self {
        use crate::plugin_source_files::SelectError;
        match err {
            SelectError::InvalidExcludePattern { pattern, message } => {
                SdkError::InvalidExcludePattern { pattern, message }
            }
            SelectError::Io { source, path } => SdkError::Io { source, path },
            // Preserve historical archive behavior: walk errors → Archive.
            SelectError::Walk { message } => SdkError::Archive {
                message: format!("walkdir error: {message}"),
            },
        }
    }
}

/// The error returned by the `validate::plugin_dir` wrapper.
///
/// A focused, validation-only error type that separates validation failures
/// from the kitchen-sink [`SdkError`]. External consumers (including the
/// future runtime) interact with the pure `schemas::plugin_format` surface and
/// define their own I/O error types; they do not depend on this type.
///
/// `package.rs` stays ergonomic via [`From<ValidationFailure> for SdkError`],
/// so `?` propagation through `SdkError`-returning code paths works unchanged.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ValidationFailure {
    /// One or more validation rules failed; all diagnostics for a single pass.
    #[error("{} validation error(s) found", .0.len())]
    Invalid(Vec<ValidationError>),

    /// Non-`NotFound` I/O error reading the plugin directory or a file.
    #[error("I/O error{}", path_suffix(.path.as_ref()))]
    Io {
        #[source]
        source: std::io::Error,
        path: Option<PathBuf>,
    },

    /// A manifest `[plugin].exclude` entry is not a valid gitignore pattern.
    #[error("invalid exclude pattern {pattern:?}: {message}")]
    InvalidExcludePattern { pattern: String, message: String },
}

/// `Invalid → SdkError::ValidationErrors`, `Io → SdkError::Io`. Keeps
/// `SdkError`-returning callers (e.g. `package.rs`) ergonomic under `?`.
impl From<ValidationFailure> for SdkError {
    fn from(failure: ValidationFailure) -> Self {
        match failure {
            ValidationFailure::Invalid(errs) => SdkError::ValidationErrors(errs),
            ValidationFailure::Io { source, path } => SdkError::Io { source, path },
            ValidationFailure::InvalidExcludePattern { pattern, message } => {
                SdkError::InvalidExcludePattern { pattern, message }
            }
        }
    }
}

impl From<crate::plugin_source_files::SelectError> for ValidationFailure {
    fn from(err: crate::plugin_source_files::SelectError) -> Self {
        use crate::plugin_source_files::SelectError;
        match err {
            SelectError::InvalidExcludePattern { pattern, message } => {
                ValidationFailure::InvalidExcludePattern { pattern, message }
            }
            SelectError::Io { source, path } => ValidationFailure::Io { source, path },
            // validate has no Archive surface; fold walk errors into Io.
            SelectError::Walk { message } => ValidationFailure::Io {
                source: std::io::Error::other(message),
                path: None,
            },
        }
    }
}

/// Adapts schemas-layer `SchemaErrors` into `ValidationFailure::Invalid`.
/// Each `ReportedError` becomes one [`ValidationError::SchemaReported`].
/// Additive companion to [`From<SchemaErrors> for SdkError`]; covers the
/// `plugin_dir` wrapper boundary.
impl From<SchemaErrors> for ValidationFailure {
    fn from(errors: SchemaErrors) -> Self {
        let diagnostics = errors
            .into_iter()
            .map(ValidationError::SchemaReported)
            .collect();
        ValidationFailure::Invalid(diagnostics)
    }
}

/// Multi-error accumulator for validation passes.
///
/// Callers [`push`](Self::push) failures as they're encountered, then
/// [`into_result`](Self::into_result) to get `Ok(())` (empty) or
/// `Err(ValidationFailure::Invalid(errors))`. The type forces collect-all
/// semantics: the builder is the only way to return validation results.
#[derive(Debug, Default)]
pub struct ValidationReport {
    errors: Vec<ValidationError>,
}

impl ValidationReport {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn push(&mut self, err: ValidationError) {
        self.errors.push(err);
    }

    pub fn extend<I>(&mut self, errors: I)
    where
        I: IntoIterator<Item = ValidationError>,
    {
        self.errors.extend(errors);
    }

    pub fn is_empty(&self) -> bool {
        self.errors.is_empty()
    }

    pub fn len(&self) -> usize {
        self.errors.len()
    }

    /// `Ok(())` if empty, else `Err(ValidationFailure::Invalid(errors))`.
    pub fn into_result(self) -> Result<(), ValidationFailure> {
        if self.errors.is_empty() {
            Ok(())
        } else {
            Err(ValidationFailure::Invalid(self.errors))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;

    fn every_sdk_variant() -> Vec<SdkError> {
        vec![
            SdkError::Io {
                source: std::io::Error::other("boom"),
                path: Some(PathBuf::from("/tmp/x")),
            },
            SdkError::Schema(SchemaError::DescriptionEmpty),
            SdkError::ValidationErrors(vec![ValidationError::MissingRequiredFile {
                file: "__init__.py".into(),
            }]),
            SdkError::Archive {
                message: "path too long".into(),
            },
            SdkError::PathTooLong {
                archive_path: "plugin-name-0.1.0/a/b/c/extremely/deep/path/leaf".into(),
                limit: 255,
            },
            SdkError::Hash {
                source: std::io::Error::other("read failed"),
            },
            SdkError::AlreadyPublished {
                name: "downsampler".into(),
                version: "1.2.0".into(),
                existing_versions: vec!["1.0.0".into(), "1.1.0".into(), "1.2.0".into()],
            },
            SdkError::CanonicalCollision {
                name: "my-plugin".into(),
                canonical: "my_plugin".into(),
                existing: vec![
                    ("my_plugin".into(), semver::Version::new(1, 0, 0)),
                    ("my_plugin".into(), semver::Version::new(1, 1, 0)),
                ],
            },
            SdkError::EntryNotFound {
                name: "downsampler".into(),
                version: "1.2.0".into(),
            },
            SdkError::InvalidExcludePattern {
                pattern: "tests/**[".into(),
                message: "unclosed character class".into(),
            },
        ]
    }

    #[test]
    fn sdk_error_display_stable() {
        let rendered: Vec<String> = every_sdk_variant().iter().map(|e| e.to_string()).collect();
        insta::assert_yaml_snapshot!("sdk_error_display", rendered);
    }

    #[test]
    fn sdk_error_variant_tags_stable() {
        let tags: Vec<&'static str> = every_sdk_variant()
            .iter()
            .map(SdkError::variant_name)
            .collect();
        insta::assert_yaml_snapshot!("sdk_error_variant_tags", tags);
    }

    #[test]
    fn validation_report_empty_is_ok() {
        let report = ValidationReport::new();
        assert!(report.is_empty());
        assert_eq!(report.len(), 0);
        assert!(report.into_result().is_ok());
    }

    #[test]
    fn validation_report_non_empty_becomes_err() {
        let mut report = ValidationReport::new();
        report.push(ValidationError::MissingRequiredFile {
            file: "__init__.py".into(),
        });
        assert!(!report.is_empty());
        assert_eq!(report.len(), 1);
        match report.into_result() {
            Err(ValidationFailure::Invalid(errs)) => {
                assert_eq!(errs.len(), 1);
            }
            other => panic!("expected ValidationFailure::Invalid, got {other:?}"),
        }
    }

    #[test]
    fn validation_report_extend_collects() {
        let mut report = ValidationReport::new();
        report.extend([
            ValidationError::NoEntryPoint,
            ValidationError::MissingRequiredFile {
                file: "manifest.toml".into(),
            },
        ]);
        assert_eq!(report.len(), 2);
    }

    #[test]
    fn validation_failure_converts_to_sdk_error() {
        let invalid = ValidationFailure::Invalid(vec![ValidationError::NoEntryPoint]);
        match SdkError::from(invalid) {
            SdkError::ValidationErrors(errs) => assert_eq!(errs.len(), 1),
            other => panic!("expected ValidationErrors, got {other:?}"),
        }

        let io = ValidationFailure::Io {
            source: std::io::Error::other("boom"),
            path: Some(PathBuf::from("/tmp/x")),
        };
        match SdkError::from(io) {
            SdkError::Io { path, .. } => {
                assert_eq!(path.as_deref(), Some(std::path::Path::new("/tmp/x")));
            }
            other => panic!("expected Io, got {other:?}"),
        }
    }

    #[test]
    fn schema_errors_convert_to_validation_failure() {
        use influxdb3_plugin_schemas::{FieldPath, ReportedError, SchemaErrors};
        let errors = SchemaErrors::new(vec![ReportedError::new(
            FieldPath::root().field("plugin").field("description"),
            SchemaError::DescriptionEmpty,
        )]);
        match ValidationFailure::from(errors) {
            ValidationFailure::Invalid(errs) => {
                assert_eq!(errs.len(), 1);
                assert!(matches!(errs[0], ValidationError::SchemaReported(_)));
            }
            other => panic!("expected Invalid, got {other:?}"),
        }
    }

    #[test]
    fn schema_error_auto_converts() {
        fn try_it() -> Result<(), SdkError> {
            Err(SchemaError::DescriptionEmpty)?;
            Ok(())
        }
        let err = try_it().unwrap_err();
        assert!(matches!(
            err,
            SdkError::Schema(SchemaError::DescriptionEmpty)
        ));
    }

    #[test]
    fn select_error_maps_to_validation_failure_invalid_exclude_pattern() {
        use crate::plugin_source_files::SelectError;
        let se = SelectError::InvalidExcludePattern {
            pattern: "[z-a]".into(),
            message: "bad".into(),
        };
        match ValidationFailure::from(se) {
            ValidationFailure::InvalidExcludePattern { pattern, .. } => {
                assert_eq!(pattern, "[z-a]")
            }
            other => panic!("expected InvalidExcludePattern, got {other:?}"),
        }
    }

    #[test]
    fn validation_invalid_exclude_pattern_converts_to_sdk_invalid_exclude_pattern() {
        let vf = ValidationFailure::InvalidExcludePattern {
            pattern: "[z-a]".into(),
            message: "bad".into(),
        };
        match SdkError::from(vf) {
            SdkError::InvalidExcludePattern { pattern, .. } => assert_eq!(pattern, "[z-a]"),
            other => panic!("expected InvalidExcludePattern, got {other:?}"),
        }
    }

    #[test]
    fn select_error_invalid_pattern_maps_to_sdk_invalid_exclude_pattern() {
        use crate::plugin_source_files::SelectError;
        let se = SelectError::InvalidExcludePattern {
            pattern: "[z-a]".into(),
            message: "bad glob".into(),
        };
        match SdkError::from(se) {
            SdkError::InvalidExcludePattern { pattern, .. } => assert_eq!(pattern, "[z-a]"),
            other => panic!("expected InvalidExcludePattern, got {other:?}"),
        }
    }

    #[test]
    fn schemas_error_structured_payload_preserved_via_sdk_schema() {
        use std::error::Error as _;

        let wrapped = SdkError::from(SchemaError::InvalidPluginName {
            name: "Bad Name".into(),
        });
        match &wrapped {
            SdkError::Schema(SchemaError::InvalidPluginName { name }) => {
                assert_eq!(name, "Bad Name");
            }
            other => panic!("expected SdkError::Schema(InvalidPluginName), got {other:?}"),
        }
        assert!(wrapped.to_string().contains("Bad Name"));

        let sem_err = semver::Version::parse("1.2").unwrap_err();
        let wrapped = SdkError::from(SchemaError::InvalidVersion {
            version: "1.2".into(),
            source: sem_err,
        });
        let bottom = wrapped
            .source()
            .expect("nested source reaches semver::Error");
        assert!(bottom.downcast_ref::<semver::Error>().is_some());
    }
}