cm_fidl_validator/
error.rs

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
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use cm_types::ParseError;
use fidl_fuchsia_component_decl as fdecl;
use std::fmt;
use std::fmt::Display;
use thiserror::Error;

/// Enum type that can represent any error encountered during validation.
#[derive(Debug, Error, PartialEq, Clone)]
pub enum Error {
    #[error("Field `{}` is missing for {}.", .0.field, .0.decl)]
    MissingField(DeclField),

    #[error("Field `{}` is empty for {}.", .0.field, .0.decl)]
    EmptyField(DeclField),

    #[error("{} has unnecessary field `{}`.", .0.decl, .0.field)]
    ExtraneousField(DeclField),

    #[error("\"{1}\" is duplicated for field `{}` in {}.", .0.field, .0.decl)]
    DuplicateField(DeclField, String),

    #[error("Field `{}` for {} is invalid.",  .0.field, .0.decl)]
    InvalidField(DeclField),

    #[error("Field {} for {} is invalid. {}.", .0.field, .0.decl, .1)]
    InvalidUrl(DeclField, String),

    #[error("Field `{}` for {} is too long.", .0.field, .0.decl)]
    FieldTooLong(DeclField),

    #[error("Field `{}` for {} has an invalid path segment.", .0.field, .0.decl)]
    FieldInvalidSegment(DeclField),

    #[error("\"{0}\" capabilities must be offered as a built-in capability.")]
    CapabilityMustBeBuiltin(DeclType),

    #[error("\"{0}\" capabilities are not currently allowed as built-ins.")]
    CapabilityCannotBeBuiltin(DeclType),

    #[error("Encountered an unknown capability declaration. This may happen due to ABI skew between the FIDL component declaration and the system.")]
    UnknownCapability,

    #[error("\"{1}\" is referenced in {0} but it does not appear in children.")]
    InvalidChild(DeclField, String),

    #[error("\"{1}\" is referenced in {0} but it does not appear in collections.")]
    InvalidCollection(DeclField, String),

    #[error("\"{1}\" is referenced in {0} but it does not appear in storage.")]
    InvalidStorage(DeclField, String),

    #[error("\"{1}\" is referenced in {0} but it does not appear in environments.")]
    InvalidEnvironment(DeclField, String),

    #[error("\"{1}\" is referenced in {0} but it does not appear in capabilities.")]
    InvalidCapability(DeclField, String),

    #[error("\"{1}\" is referenced in {0} but it does not appear in runners.")]
    InvalidRunner(DeclField, String),

    #[error("There are dependency cycle(s): {0}.")]
    DependencyCycle(String),

    #[error("Path \"{path}\" from {decl} overlaps with \"{other_path}\" path from {other_decl}. Paths across decls must be unique in order to avoid namespace collisions.")]
    InvalidPathOverlap { decl: DeclField, path: String, other_decl: DeclField, other_path: String },

    #[error("{} \"{}\" path overlaps with \"/pkg\", which is a protected path", decl, path)]
    PkgPathOverlap { decl: DeclField, path: String },

    #[error("Source path \"{1}\" provided to {0} decl is unnecessary. Built-in capabilities don't need this field as they originate from the framework.")]
    ExtraneousSourcePath(DeclField, String),

    #[error("Configuration schema defines a vector nested inside another vector. Vector can only contain numbers, booleans, and strings.")]
    NestedVector,

    #[error("The `availability` field in {0} for {1} must be set to \"optional\" because the source is \"void\".")]
    AvailabilityMustBeOptional(DeclField, String),

    #[error("Invalid aggregate offer: {0}")]
    InvalidAggregateOffer(String),

    #[error("All sources that feed into an aggregation operation should have the same availability. Got {0}.")]
    DifferentAvailabilityInAggregation(AvailabilityList),

    #[error("Multiple runners used.")]
    MultipleRunnersUsed,

    #[error("Used runner conflicts with program runner.")]
    ConflictingRunners,

    #[error(
        "Runner is missing for executable component. A runner must be specified in the \
            `program` section or in the `use` section."
    )]
    MissingRunner,

    #[error("Dynamic children cannot specify an environment.")]
    DynamicChildWithEnvironment,
}

/// [AvailabilityList] is a newtype to provide a human friendly [Display] impl for a vector
/// of availabilities.
#[derive(Debug, PartialEq, Clone)]
pub struct AvailabilityList(pub Vec<fdecl::Availability>);

impl Display for AvailabilityList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let comma_separated =
            self.0.iter().map(|s| format!("{:?}", s)).collect::<Vec<_>>().join(", ");
        write!(f, "[ {comma_separated} ]")
    }
}

impl Error {
    pub fn missing_field(decl_type: DeclType, keyword: impl Into<String>) -> Self {
        Error::MissingField(DeclField { decl: decl_type, field: keyword.into() })
    }

    pub fn empty_field(decl_type: DeclType, keyword: impl Into<String>) -> Self {
        Error::EmptyField(DeclField { decl: decl_type, field: keyword.into() })
    }

    pub fn extraneous_field(decl_type: DeclType, keyword: impl Into<String>) -> Self {
        Error::ExtraneousField(DeclField { decl: decl_type, field: keyword.into() })
    }

    pub fn duplicate_field(
        decl_type: DeclType,
        keyword: impl Into<String>,
        value: impl Into<String>,
    ) -> Self {
        Error::DuplicateField(DeclField { decl: decl_type, field: keyword.into() }, value.into())
    }

    pub fn invalid_field(decl_type: DeclType, keyword: impl Into<String>) -> Self {
        Error::InvalidField(DeclField { decl: decl_type, field: keyword.into() })
    }

    pub fn invalid_url(
        decl_type: DeclType,
        keyword: impl Into<String>,
        message: impl Into<String>,
    ) -> Self {
        Error::InvalidUrl(DeclField { decl: decl_type, field: keyword.into() }, message.into())
    }

    pub fn field_too_long(decl_type: DeclType, keyword: impl Into<String>) -> Self {
        Error::FieldTooLong(DeclField { decl: decl_type, field: keyword.into() })
    }

    pub fn field_invalid_segment(decl_type: DeclType, keyword: impl Into<String>) -> Self {
        Error::FieldInvalidSegment(DeclField { decl: decl_type, field: keyword.into() })
    }

    pub fn invalid_child(
        decl_type: DeclType,
        keyword: impl Into<String>,
        child: impl Into<String>,
    ) -> Self {
        Error::InvalidChild(DeclField { decl: decl_type, field: keyword.into() }, child.into())
    }

    pub fn invalid_collection(
        decl_type: DeclType,
        keyword: impl Into<String>,
        collection: impl Into<String>,
    ) -> Self {
        Error::InvalidCollection(
            DeclField { decl: decl_type, field: keyword.into() },
            collection.into(),
        )
    }

    pub fn invalid_environment(
        decl_type: DeclType,
        keyword: impl Into<String>,
        environment: impl Into<String>,
    ) -> Self {
        Error::InvalidEnvironment(
            DeclField { decl: decl_type, field: keyword.into() },
            environment.into(),
        )
    }

    // TODO: Replace with `invalid_capability`?
    pub fn invalid_runner(
        decl_type: DeclType,
        keyword: impl Into<String>,
        runner: impl Into<String>,
    ) -> Self {
        Error::InvalidRunner(DeclField { decl: decl_type, field: keyword.into() }, runner.into())
    }

    pub fn invalid_capability(
        decl_type: DeclType,
        keyword: impl Into<String>,
        capability: impl Into<String>,
    ) -> Self {
        Error::InvalidCapability(
            DeclField { decl: decl_type, field: keyword.into() },
            capability.into(),
        )
    }

    pub fn dependency_cycle(error: String) -> Self {
        Error::DependencyCycle(error)
    }

    pub fn invalid_path_overlap(
        decl: DeclType,
        path: impl Into<String>,
        other_decl: DeclType,
        other_path: impl Into<String>,
    ) -> Self {
        Error::InvalidPathOverlap {
            decl: DeclField { decl, field: "target_path".to_string() },
            path: path.into(),
            other_decl: DeclField { decl: other_decl, field: "target_path".to_string() },
            other_path: other_path.into(),
        }
    }

    pub fn pkg_path_overlap(decl: DeclType, path: impl Into<String>) -> Self {
        Error::PkgPathOverlap {
            decl: DeclField { decl, field: "target_path".to_string() },
            path: path.into(),
        }
    }

    pub fn extraneous_source_path(decl_type: DeclType, path: impl Into<String>) -> Self {
        Error::ExtraneousSourcePath(
            DeclField { decl: decl_type, field: "source_path".to_string() },
            path.into(),
        )
    }

    pub fn nested_vector() -> Self {
        Error::NestedVector
    }

    pub fn availability_must_be_optional(
        decl_type: DeclType,
        keyword: impl Into<String>,
        source_name: Option<&String>,
    ) -> Self {
        Error::AvailabilityMustBeOptional(
            DeclField { decl: decl_type, field: keyword.into() },
            source_name.cloned().unwrap_or_else(|| "<unnamed>".to_string()),
        )
    }

    pub fn invalid_aggregate_offer(info: impl Into<String>) -> Self {
        Error::InvalidAggregateOffer(info.into())
    }

    pub fn different_availability_in_aggregation(availability: Vec<fdecl::Availability>) -> Self {
        Error::DifferentAvailabilityInAggregation(AvailabilityList(availability))
    }

    pub fn from_parse_error(
        err: ParseError,
        prop: &String,
        decl_type: DeclType,
        keyword: &str,
    ) -> Self {
        match err {
            ParseError::Empty => Error::empty_field(decl_type, keyword),
            ParseError::TooLong => Error::field_too_long(decl_type, keyword),
            ParseError::InvalidComponentUrl { details } => {
                Error::invalid_url(decl_type, keyword, format!(r#""{prop}": {details}"#))
            }
            ParseError::InvalidValue => Error::invalid_field(decl_type, keyword),
            ParseError::InvalidSegment => Error::field_invalid_segment(decl_type, keyword),
            ParseError::NoLeadingSlash => Error::invalid_field(decl_type, keyword),
        }
    }
}

// To regenerate:
//
// ```
//     fx exec env | \
//         grep FUCHSIA_BUILD_DIR | \
//         xargs -I {} bash -c 'export {}; grep -E "pub (enum|struct)" $FUCHSIA_BUILD_DIR/fidling/gen/sdk/fidl/fuchsia.component.decl/fuchsia.component.decl/rust/fidl_fuchsia_component_decl.rs' | \
//         awk '{print $3}' | \
//         sed 's/[:;]$//' | \
//         sort | uniq | sed 's/$/,/'
// ```
//
/// The list of all declarations in fuchsia.component.decl, for error reporting purposes.
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum DeclType {
    AllowedOffers,
    Availability,
    Capability,
    CapabilityRef,
    Child,
    ChildRef,
    Collection,
    CollectionRef,
    Component,
    Configuration,
    ConfigChecksum,
    ConfigField,
    ConfigMutability,
    ConfigOverride,
    ConfigSchema,
    ConfigSingleValue,
    ConfigType,
    ConfigTypeLayout,
    ConfigValue,
    ConfigValuesData,
    ConfigValueSource,
    ConfigValueSpec,
    ConfigVectorValue,
    DebugProtocolRegistration,
    DebugRef,
    DebugRegistration,
    DependencyType,
    Dictionary,
    Directory,
    Durability,
    Environment,
    EnvironmentExtends,
    EventStream,
    EventSubscription,
    Expose,
    ExposeConfig,
    ExposeDictionary,
    ExposeDirectory,
    ExposeProtocol,
    ExposeResolver,
    ExposeRunner,
    ExposeService,
    FrameworkRef,
    LayoutConstraint,
    LayoutParameter,
    NameMapping,
    Offer,
    OfferConfig,
    OfferDictionary,
    OfferDirectory,
    OfferEventStream,
    OfferProtocol,
    OfferResolver,
    OfferRunner,
    OfferService,
    OfferStorage,
    OnTerminate,
    ParentRef,
    Program,
    Protocol,
    Ref,
    ResolvedConfig,
    ResolvedConfigField,
    Resolver,
    ResolverRegistration,
    Runner,
    RunnerRegistration,
    SelfRef,
    Service,
    StartupMode,
    Storage,
    StorageId,
    Use,
    UseConfiguration,
    UseDirectory,
    UseEventStream,
    UseProtocol,
    UseRunner,
    UseService,
    UseStorage,
    VoidRef,
}

impl fmt::Display for DeclType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let name = match *self {
            // To regenerate:
            //
            // ```
            //     fx exec env | \
            //         grep FUCHSIA_BUILD_DIR | \
            //         xargs -I {} bash -c 'export {}; grep -E "pub (enum|struct)" $FUCHSIA_BUILD_DIR/fidling/gen/sdk/fidl/fuchsia.component.decl/fuchsia.component.decl/rust/fidl_fuchsia_component_decl.rs' | \
            //         awk '{print $3}' | \
            //         sed 's/[:;]$//' | \
            //         sort | uniq | sed 's/\(.*\)/DeclType::\1 => "\1",/'
            // ```
            DeclType::AllowedOffers => "AllowedOffers",
            DeclType::Availability => "Availability",
            DeclType::Capability => "Capability",
            DeclType::CapabilityRef => "CapabilityRef",
            DeclType::Child => "Child",
            DeclType::ChildRef => "ChildRef",
            DeclType::Collection => "Collection",
            DeclType::CollectionRef => "CollectionRef",
            DeclType::Component => "Component",
            DeclType::Configuration => "Configuration",
            DeclType::ConfigChecksum => "ConfigChecksum",
            DeclType::ConfigField => "ConfigField",
            DeclType::ConfigMutability => "ConfigMutability",
            DeclType::ConfigOverride => "ConfigOverride",
            DeclType::ConfigSchema => "ConfigSchema",
            DeclType::ConfigSingleValue => "ConfigSingleValue",
            DeclType::ConfigType => "ConfigType",
            DeclType::ConfigTypeLayout => "ConfigTypeLayout",
            DeclType::ConfigValue => "ConfigValue",
            DeclType::ConfigValuesData => "ConfigValuesData",
            DeclType::ConfigValueSource => "ConfigValueSource",
            DeclType::ConfigValueSpec => "ConfigValueSpec",
            DeclType::ConfigVectorValue => "ConfigVectorValue",
            DeclType::DebugProtocolRegistration => "DebugProtocolRegistration",
            DeclType::DebugRef => "DebugRef",
            DeclType::DebugRegistration => "DebugRegistration",
            DeclType::DependencyType => "DependencyType",
            DeclType::Dictionary => "Dictionary",
            DeclType::Directory => "Directory",
            DeclType::Durability => "Durability",
            DeclType::Environment => "Environment",
            DeclType::EnvironmentExtends => "EnvironmentExtends",
            DeclType::EventStream => "EventStream",
            DeclType::EventSubscription => "EventSubscription",
            DeclType::Expose => "Expose",
            DeclType::ExposeConfig => "ExposeConfig",
            DeclType::ExposeDictionary => "ExposeDictionary",
            DeclType::ExposeDirectory => "ExposeDirectory",
            DeclType::ExposeProtocol => "ExposeProtocol",
            DeclType::ExposeResolver => "ExposeResolver",
            DeclType::ExposeRunner => "ExposeRunner",
            DeclType::ExposeService => "ExposeService",
            DeclType::FrameworkRef => "FrameworkRef",
            DeclType::LayoutConstraint => "LayoutConstraint",
            DeclType::LayoutParameter => "LayoutParameter",
            DeclType::NameMapping => "NameMapping",
            DeclType::Offer => "Offer",
            DeclType::OfferConfig => "OfferConfig",
            DeclType::OfferDictionary => "OfferDictionary",
            DeclType::OfferDirectory => "OfferDirectory",
            DeclType::OfferEventStream => "OfferEventStream",
            DeclType::OfferProtocol => "OfferProtocol",
            DeclType::OfferResolver => "OfferResolver",
            DeclType::OfferRunner => "OfferRunner",
            DeclType::OfferService => "OfferService",
            DeclType::OfferStorage => "OfferStorage",
            DeclType::OnTerminate => "OnTerminate",
            DeclType::ParentRef => "ParentRef",
            DeclType::Program => "Program",
            DeclType::Protocol => "Protocol",
            DeclType::Ref => "Ref",
            DeclType::ResolvedConfig => "ResolvedConfig",
            DeclType::ResolvedConfigField => "ResolvedConfigField",
            DeclType::Resolver => "Resolver",
            DeclType::ResolverRegistration => "ResolverRegistration",
            DeclType::Runner => "Runner",
            DeclType::RunnerRegistration => "RunnerRegistration",
            DeclType::SelfRef => "SelfRef",
            DeclType::Service => "Service",
            DeclType::StartupMode => "StartupMode",
            DeclType::Storage => "Storage",
            DeclType::StorageId => "StorageId",
            DeclType::Use => "Use",
            DeclType::UseConfiguration => "UseConfiguration",
            DeclType::UseDirectory => "UseDirectory",
            DeclType::UseEventStream => "UseEventStream",
            DeclType::UseProtocol => "UseProtocol",
            DeclType::UseRunner => "UseRunner",
            DeclType::UseService => "UseService",
            DeclType::UseStorage => "UseStorage",
            DeclType::VoidRef => "VoidRef",
        };
        write!(f, "{}", name)
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct DeclField {
    pub decl: DeclType,
    pub field: String,
}

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

/// Represents a list of errors encountered during validation.
#[derive(Debug, Error, PartialEq, Clone)]
pub struct ErrorList {
    pub errs: Vec<Error>,
}

impl ErrorList {
    pub(crate) fn new(errs: Vec<Error>) -> ErrorList {
        ErrorList { errs }
    }
}

impl fmt::Display for ErrorList {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let strs: Vec<String> = self.errs.iter().map(|e| format!("{}", e)).collect();
        write!(f, "{}", strs.join(", "))
    }
}

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

    #[test]
    fn test_errors() {
        assert_eq!(
            format!("{}", Error::missing_field(DeclType::Child, "keyword")),
            "Field `keyword` is missing for Child."
        );
        assert_eq!(
            format!("{}", Error::empty_field(DeclType::Child, "keyword")),
            "Field `keyword` is empty for Child."
        );
        assert_eq!(
            format!("{}", Error::duplicate_field(DeclType::Child, "keyword", "foo")),
            "\"foo\" is duplicated for field `keyword` in Child."
        );
        assert_eq!(
            format!("{}", Error::invalid_field(DeclType::Child, "keyword")),
            "Field `keyword` for Child is invalid."
        );
        assert_eq!(
            format!("{}", Error::field_too_long(DeclType::Child, "keyword")),
            "Field `keyword` for Child is too long."
        );
        assert_eq!(
            format!("{}", Error::field_invalid_segment(DeclType::Child, "keyword")),
            "Field `keyword` for Child has an invalid path segment."
        );
        assert_eq!(
            format!("{}", Error::invalid_child(DeclType::Child, "source", "child")),
            "\"child\" is referenced in Child.source but it does not appear in children."
        );
        assert_eq!(
            format!("{}", Error::invalid_collection(DeclType::Child, "source", "child")),
            "\"child\" is referenced in Child.source but it does not appear in collections."
        );
    }
}