/// Adds a new component to the realm. The `moniker` field should be one or more component
/// child names separated by `/`s.
///
/// Any missing parent components will be automatically filled in with empty component
/// declarations, so when adding components to the builder parents should be added before
/// children. As an example, to add components "a" and "a/b", the calls must be made in this
/// order:
///
/// ```
/// let mut builder = RealmBuilder::new().await?;
/// builder.add_component("a", ComponentSource::Mock(...))?
///        .add_component("a/b", ComponentSource::Mock(...))?
/// ```
///
/// If the `add_component` calls were reversed the second one would cause a
/// `ComponentAlreadyExists` error, because an `a` component would be generated as part of the
/// call to add `a/b`.
Adds a new component to the realm. The `moniker` field should be one or more component
child names separated by `/`s.

Any missing parent components will be automatically filled in with empty component
declarations, so when adding components to the builder parents should be added before
children. As an example, to add components "a" and "a/b", the calls must be made in this
order.




Adds a new component to the realm . The ` moniker ` field should be one or more component child names separated by ` / ` s . Any missing parent components will be automatically filled in with empty component declarations so when adding components to the builder parents should be added before children . As an example to add components " a " and " a / b " the calls must be made in this order .
pub async fn add_component<M>(
        &mut self,
        moniker: M,
        source: ComponentSource,
    ) -> Result<&mut Self, Error>
    where
        M: Into<Moniker>,
    {
        let moniker = moniker.into();
        if self.realm.contains(&moniker) {
            // TODO: differentiate errors between "this already exists" and "this is a leaf node"
            return Err(BuilderError::ComponentAlreadyExists(moniker).into());
        }
        if moniker != Moniker::root() && !self.realm.contains(&Moniker::root()) {
            self.realm.add_component(Moniker::root(), cm_rust::ComponentDecl::default())?;
        }
        let ancestry = moniker.ancestry();
        // The ancestry is sorted from child to parent, but we need to add components to
        // the tree in the other direction.
        for ancestor in ancestry.into_iter().rev() {
            if !self.realm.contains(&ancestor) {
                self.realm.add_component(ancestor, cm_rust::ComponentDecl::default())?;
            }
        }
        match source {
            ComponentSource::Url(url) => {
                if moniker.is_root() {
                    return Err(BuilderError::RootComponentCantHaveUrl.into());
                }
                let parent_moniker = moniker.parent().unwrap();
                let parent_decl = self.realm.get_decl_mut(&parent_moniker)?;
                parent_decl.children.push(cm_rust::ChildDecl {
                    name: moniker.child_name().unwrap().clone(),
                    url: url.to_string(),
                    startup: fsys::StartupMode::Lazy,
                    environment: None,
                });
            }
            ComponentSource::Mock(mock) => {
                self.realm.add_mocked_component(moniker, mock).await?;
            }
        }
        Ok(self)
    }
pub async fn add_component < M > ( & mut self , moniker : M , source : ComponentSource , ) -> Result < & mut Self , Error > where M : Into < Moniker > , { let moniker = moniker . into ( ) ; if self . realm . contains ( & moniker ) { // TODO: differentiate errors between "this already exists" and "this is a leaf node" return Err ( BuilderError :: ComponentAlreadyExists ( moniker ) . into ( ) ) ; } if moniker != Moniker :: root ( ) && ! self . realm . contains ( & Moniker :: root ( ) ) { self . realm . add_component ( Moniker :: root ( ) , cm_rust :: ComponentDecl :: default ( ) ) ? ; } let ancestry = moniker . ancestry ( ) ; // The ancestry is sorted from child to parent, but we need to add components to // the tree in the other direction. for ancestor in ancestry . into_iter ( ) . rev ( ) { if ! self . realm . contains ( & ancestor ) { self . realm . add_component ( ancestor , cm_rust :: ComponentDecl :: default ( ) ) ? ; } } match source { ComponentSource :: Url ( url ) => { if moniker . is_root ( ) { return Err ( BuilderError :: RootComponentCantHaveUrl . into ( ) ) ; } let parent_moniker = moniker . parent ( ) . unwrap ( ) ; let parent_decl = self . realm . get_decl_mut ( & parent_moniker ) ? ; parent_decl . children . push ( cm_rust :: ChildDecl { name : moniker . child_name ( ) . unwrap ( ) . clone ( ) , url : url . to_string ( ) , startup : fsys :: StartupMode :: Lazy , environment : None , } ) ; } ComponentSource :: Mock ( mock ) => { self . realm . add_mocked_component ( moniker , mock ) . await ? ; } } Ok ( self ) }
Adds a new component to the realm.
Adds a new component to the realm .
&mut self
moniker
M
source
ComponentSource