cml/types/child.rs
1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::types::environment::EnvironmentRef;
6pub use cm_types::{Name, OnTerminate, StartupMode, Url};
7use json_spanned_value::Spanned;
8use reference_doc::ReferenceDoc;
9use serde::{Deserialize, Serialize};
10
11/// Example:
12///
13/// ```json5
14/// children: [
15/// {
16/// name: "logger",
17/// url: "fuchsia-pkg://fuchsia.com/logger#logger.cm",
18/// },
19/// {
20/// name: "pkg_cache",
21/// url: "fuchsia-pkg://fuchsia.com/pkg_cache#meta/pkg_cache.cm",
22/// startup: "eager",
23/// },
24/// {
25/// name: "child",
26/// url: "#meta/child.cm",
27/// }
28/// ],
29/// ```
30///
31/// [component-url]: /docs/reference/components/url.md
32/// [doc-eager]: /docs/development/components/connect.md#eager
33/// [doc-reboot-on-terminate]: /docs/development/components/connect.md#reboot-on-terminate
34#[derive(ReferenceDoc, Deserialize, Debug, PartialEq, Serialize)]
35#[serde(deny_unknown_fields)]
36#[reference_doc(fields_as = "list", top_level_doc_after_fields)]
37pub struct Child {
38 /// The name of the child component instance, which is a string of one
39 /// or more of the following characters: `a-z`, `0-9`, `_`, `.`, `-`. The name
40 /// identifies this component when used in a [reference](#references).
41 pub name: Name,
42
43 /// The [component URL][component-url] for the child component instance.
44 pub url: Url,
45
46 /// The component instance's startup mode. One of:
47 /// - `lazy` _(default)_: Start the component instance only if another
48 /// component instance binds to it.
49 /// - [`eager`][doc-eager]: Start the component instance as soon as its parent
50 /// starts.
51 #[serde(default)]
52 #[serde(skip_serializing_if = "StartupMode::is_lazy")]
53 pub startup: StartupMode,
54
55 /// Determines the fault recovery policy to apply if this component terminates.
56 /// - `none` _(default)_: Do nothing.
57 /// - `reboot`: Gracefully reboot the system if the component terminates for
58 /// any reason other than graceful exit. This is a special feature for use only by a narrow
59 /// set of components; see [Termination policies][doc-reboot-on-terminate] for more
60 /// information.
61 #[serde(skip_serializing_if = "Option::is_none")]
62 pub on_terminate: Option<OnTerminate>,
63
64 /// If present, the name of the environment to be assigned to the child component instance, one
65 /// of [`environments`](#environments). If omitted, the child will inherit the same environment
66 /// assigned to this component.
67 #[serde(skip_serializing_if = "Option::is_none")]
68 pub environment: Option<EnvironmentRef>,
69}
70
71#[derive(Deserialize, Debug, PartialEq)]
72#[serde(deny_unknown_fields)]
73pub struct SpannedChild {
74 pub name: Spanned<Name>,
75 pub url: Spanned<Url>,
76 #[serde(default)]
77 pub startup: StartupMode,
78 pub on_terminate: Option<OnTerminate>,
79 pub environment: Option<EnvironmentRef>,
80}