async fn walk_expose_chain<'a>(
    model: &'a Model,
    pos: &'a mut WalkPosition,
) -> Result<CapabilitySource, ModelError> {
    // If the first capability is an Expose, assume it corresponds to an Expose declared by the
    // first component.
    let mut first_expose = {
        match &pos.capability {
            ComponentCapability::Expose(e) => Some(e.clone()),
            _ => None,
        }
    };
    loop {
        // TODO(xbhatnag): See if the locking needs to be over the entire loop
        // Consider -> let current_decl = { .. };
        let current_moniker = pos.moniker();
        let current_realm = model.look_up_realm(current_moniker).await?;
        let realm_state = current_realm.lock_state().await;
        let realm_state = realm_state.as_ref().expect("walk_expose_chain: not resolved");
        // This `get()` is safe because look_up_realm populates this field.
        let first_expose = first_expose.take();
        let expose = first_expose
            .as_ref()
            .or_else(|| pos.capability.find_expose_source(realm_state.decl()))
            .ok_or(ModelError::capability_discovery_error(format_err!(
                "no matching offers found for capability {:?} from component {}",
                pos.capability,
                pos.moniker(),
            )))?;
        let (source, target) = match expose {
            ExposeDecl::Service(_) => return Err(ModelError::unsupported("Service capability")),
            ExposeDecl::ServiceProtocol(ls) => {
                (ExposeSource::ServiceProtocol(&ls.source), &ls.target)
            }
            ExposeDecl::Directory(d) => (ExposeSource::Directory(&d.source), &d.target),
            ExposeDecl::Runner(r) => (ExposeSource::Runner(&r.source), &r.target),
        };
        if target != &ExposeTarget::Realm {
            return Err(ModelError::capability_discovery_error(format_err!(
                "matching exposed capability {:?} from component {} has non-realm target",
                pos.capability,
                pos.moniker()
            )));
        }
        match source {
            ExposeSource::ServiceProtocol(cm_rust::ExposeSource::Self_)
            | ExposeSource::Directory(cm_rust::ExposeSource::Self_) => {
                // The offered capability comes from the current component, return our
                // current location in the tree.
                return Ok(CapabilitySource::Component {
                    capability: ComponentCapability::Expose(expose.clone()),
                    source_moniker: current_moniker.clone(),
                });
            }
            ExposeSource::Runner(cm_rust::ExposeSource::Self_) => {
                // The exposed capability comes from the current component.
                // Find the current component's Runner declaration.
                let cap = ComponentCapability::Expose(expose.clone());
                return Ok(CapabilitySource::Component {
                    capability: ComponentCapability::Runner(
                        cap.find_runner_source(realm_state.decl())
                            .ok_or(ModelError::capability_discovery_error(format_err!(
                                concat!(
                                    "component {} attempted to expose runner {:?}, ",
                                    "but no matching runner declaration was found"
                                ),
                                pos.moniker(),
                                cap.source_name().unwrap(),
                            )))?
                            .clone(),
                    ),
                    source_moniker: current_moniker.clone(),
                });
            }
            ExposeSource::ServiceProtocol(cm_rust::ExposeSource::Child(child_name))
            | ExposeSource::Directory(cm_rust::ExposeSource::Child(child_name))
            | ExposeSource::Runner(cm_rust::ExposeSource::Child(child_name)) => {
                // The offered capability comes from a child, so follow the child.
                pos.capability = ComponentCapability::Expose(expose.clone());
                let partial = PartialMoniker::new(child_name.to_string(), None);
                pos.moniker =
                    Some(realm_state.extend_moniker_with(&pos.moniker(), &partial).ok_or(
                        ModelError::capability_discovery_error(format_err!(
                            "no child {} found from component {} for expose source",
                            partial,
                            pos.moniker().clone(),
                        )),
                    )?);
                continue;
            }
            ExposeSource::ServiceProtocol(cm_rust::ExposeSource::Framework)
            | ExposeSource::Directory(cm_rust::ExposeSource::Framework) => {
                let capability =
                    FrameworkCapability::framework_from_expose_decl(expose).map_err(|_| {
                        ModelError::capability_discovery_error(format_err!(
                            "no matching offers found for capability {:?} from component {}",
                            pos.capability,
                            pos.moniker(),
                        ))
                    })?;
                return Ok(CapabilitySource::Framework {
                    capability,
                    scope_moniker: Some(current_moniker.clone()),
                });
            }
            ExposeSource::Runner(cm_rust::ExposeSource::Framework) => {
                return Err(ModelError::capability_discovery_error(format_err!(
                    "component {} attempted to use runner from framework",
                    pos.moniker().clone()
                )));
            }
        }
    }
}
/// Follows `expose` declarations down the component tree, starting at `pos`. The algorithm looks
/// for a matching `expose` in the child, as long as the `expose` is not from `self`.
///
/// Returns the source of the capability.
Follows `expose` declarations down the component tree, starting at `pos`.

Returns the source of the capability.
Follows ` expose ` declarations down the component tree starting at ` pos ` . Returns the source of the capability .
async fn walk_expose_chain<'a>(
    model: &'a Model,
    pos: &'a mut WalkPosition,
) -> Result<CapabilitySource, ModelError> {
    // If the first capability is an Expose, assume it corresponds to an Expose declared by the
    // first component.
    let mut first_expose = {
        match &pos.capability {
            ComponentCapability::Expose(e) => Some(e.clone()),
            _ => None,
        }
    };
    loop {
        // TODO(xbhatnag): See if the locking needs to be over the entire loop
        // Consider -> let current_decl = { .. };
        let current_moniker = pos.moniker();
        let current_realm = model.look_up_realm(current_moniker).await?;
        let realm_state = current_realm.lock_state().await;
        let realm_state = realm_state.as_ref().expect("walk_expose_chain: not resolved");
        // This `get()` is safe because look_up_realm populates this field.
        let first_expose = first_expose.take();
        let expose = first_expose
            .as_ref()
            .or_else(|| pos.capability.find_expose_source(realm_state.decl()))
            .ok_or(ModelError::capability_discovery_error(format_err!(
                "no matching offers found for capability {:?} from component {}",
                pos.capability,
                pos.moniker(),
            )))?;
        let (source, target) = match expose {
            ExposeDecl::Service(_) => return Err(ModelError::unsupported("Service capability")),
            ExposeDecl::ServiceProtocol(ls) => {
                (ExposeSource::ServiceProtocol(&ls.source), &ls.target)
            }
            ExposeDecl::Directory(d) => (ExposeSource::Directory(&d.source), &d.target),
            ExposeDecl::Runner(r) => (ExposeSource::Runner(&r.source), &r.target),
        };
        if target != &ExposeTarget::Realm {
            return Err(ModelError::capability_discovery_error(format_err!(
                "matching exposed capability {:?} from component {} has non-realm target",
                pos.capability,
                pos.moniker()
            )));
        }
        match source {
            ExposeSource::ServiceProtocol(cm_rust::ExposeSource::Self_)
            | ExposeSource::Directory(cm_rust::ExposeSource::Self_) => {
                // The offered capability comes from the current component, return our
                // current location in the tree.
                return Ok(CapabilitySource::Component {
                    capability: ComponentCapability::Expose(expose.clone()),
                    source_moniker: current_moniker.clone(),
                });
            }
            ExposeSource::Runner(cm_rust::ExposeSource::Self_) => {
                // The exposed capability comes from the current component.
                // Find the current component's Runner declaration.
                let cap = ComponentCapability::Expose(expose.clone());
                return Ok(CapabilitySource::Component {
                    capability: ComponentCapability::Runner(
                        cap.find_runner_source(realm_state.decl())
                            .ok_or(ModelError::capability_discovery_error(format_err!(
                                concat!(
                                    "component {} attempted to expose runner {:?}, ",
                                    "but no matching runner declaration was found"
                                ),
                                pos.moniker(),
                                cap.source_name().unwrap(),
                            )))?
                            .clone(),
                    ),
                    source_moniker: current_moniker.clone(),
                });
            }
            ExposeSource::ServiceProtocol(cm_rust::ExposeSource::Child(child_name))
            | ExposeSource::Directory(cm_rust::ExposeSource::Child(child_name))
            | ExposeSource::Runner(cm_rust::ExposeSource::Child(child_name)) => {
                // The offered capability comes from a child, so follow the child.
                pos.capability = ComponentCapability::Expose(expose.clone());
                let partial = PartialMoniker::new(child_name.to_string(), None);
                pos.moniker =
                    Some(realm_state.extend_moniker_with(&pos.moniker(), &partial).ok_or(
                        ModelError::capability_discovery_error(format_err!(
                            "no child {} found from component {} for expose source",
                            partial,
                            pos.moniker().clone(),
                        )),
                    )?);
                continue;
            }
            ExposeSource::ServiceProtocol(cm_rust::ExposeSource::Framework)
            | ExposeSource::Directory(cm_rust::ExposeSource::Framework) => {
                let capability =
                    FrameworkCapability::framework_from_expose_decl(expose).map_err(|_| {
                        ModelError::capability_discovery_error(format_err!(
                            "no matching offers found for capability {:?} from component {}",
                            pos.capability,
                            pos.moniker(),
                        ))
                    })?;
                return Ok(CapabilitySource::Framework {
                    capability,
                    scope_moniker: Some(current_moniker.clone()),
                });
            }
            ExposeSource::Runner(cm_rust::ExposeSource::Framework) => {
                return Err(ModelError::capability_discovery_error(format_err!(
                    "component {} attempted to use runner from framework",
                    pos.moniker().clone()
                )));
            }
        }
    }
}
async fn walk_expose_chain < ' a > ( model : & ' a Model , pos : & ' a mut WalkPosition , ) -> Result < CapabilitySource , ModelError > { // If the first capability is an Expose, assume it corresponds to an Expose declared by the // first component. let mut first_expose = { match & pos . capability { ComponentCapability :: Expose ( e ) => Some ( e . clone ( ) ) , _ => None , } } ; loop { // TODO(xbhatnag): See if the locking needs to be over the entire loop // Consider -> let current_decl = { .. }; let current_moniker = pos . moniker ( ) ; let current_realm = model . look_up_realm ( current_moniker ) . await ? ; let realm_state = current_realm . lock_state ( ) . await ; let realm_state = realm_state . as_ref ( ) . expect ( " " ) ; // This `get()` is safe because look_up_realm populates this field. let first_expose = first_expose . take ( ) ; let expose = first_expose . as_ref ( ) . or_else ( | | pos . capability . find_expose_source ( realm_state . decl ( ) ) ) . ok_or ( ModelError :: capability_discovery_error ( format_err ! ( " " pos capability pos moniker ( ) ) ) ) ? ; let ( source , target ) = match expose { ExposeDecl :: Service ( _ ) => return Err ( ModelError :: unsupported ( " " ) ) , ExposeDecl :: ServiceProtocol ( ls ) => { ( ExposeSource :: ServiceProtocol ( & ls . source ) , & ls . target ) } ExposeDecl :: Directory ( d ) => ( ExposeSource :: Directory ( & d . source ) , & d . target ) , ExposeDecl :: Runner ( r ) => ( ExposeSource :: Runner ( & r . source ) , & r . target ) , } ; if target != & ExposeTarget :: Realm { return Err ( ModelError :: capability_discovery_error ( format_err ! ( " " pos capability pos moniker ( ) ) ) ) ; } match source { ExposeSource :: ServiceProtocol ( cm_rust :: ExposeSource :: Self_ ) | ExposeSource :: Directory ( cm_rust :: ExposeSource :: Self_ ) => { // The offered capability comes from the current component, return our // current location in the tree. return Ok ( CapabilitySource :: Component { capability : ComponentCapability :: Expose ( expose . clone ( ) ) , source_moniker : current_moniker . clone ( ) , } ) ; } ExposeSource :: Runner ( cm_rust :: ExposeSource :: Self_ ) => { // The exposed capability comes from the current component. // Find the current component's Runner declaration. let cap = ComponentCapability :: Expose ( expose . clone ( ) ) ; return Ok ( CapabilitySource :: Component { capability : ComponentCapability :: Runner ( cap . find_runner_source ( realm_state . decl ( ) ) . ok_or ( ModelError :: capability_discovery_error ( format_err ! ( concat ( " " " " ) pos moniker ( ) cap source_name ( ) unwrap ( ) ) ) ) ? . clone ( ) , ) , source_moniker : current_moniker . clone ( ) , } ) ; } ExposeSource :: ServiceProtocol ( cm_rust :: ExposeSource :: Child ( child_name ) ) | ExposeSource :: Directory ( cm_rust :: ExposeSource :: Child ( child_name ) ) | ExposeSource :: Runner ( cm_rust :: ExposeSource :: Child ( child_name ) ) => { // The offered capability comes from a child, so follow the child. pos . capability = ComponentCapability :: Expose ( expose . clone ( ) ) ; let partial = PartialMoniker :: new ( child_name . to_string ( ) , None ) ; pos . moniker = Some ( realm_state . extend_moniker_with ( & pos . moniker ( ) , & partial ) . ok_or ( ModelError :: capability_discovery_error ( format_err ! ( " " partial pos moniker ( ) clone ( ) ) ) , ) ? ) ; continue ; } ExposeSource :: ServiceProtocol ( cm_rust :: ExposeSource :: Framework ) | ExposeSource :: Directory ( cm_rust :: ExposeSource :: Framework ) => { let capability = FrameworkCapability :: framework_from_expose_decl ( expose ) . map_err ( | _ | { ModelError :: capability_discovery_error ( format_err ! ( " " pos capability pos moniker ( ) ) ) } ) ? ; return Ok ( CapabilitySource :: Framework { capability , scope_moniker : Some ( current_moniker . clone ( ) ) , } ) ; } ExposeSource :: Runner ( cm_rust :: ExposeSource :: Framework ) => { return Err ( ModelError :: capability_discovery_error ( format_err ! ( " " pos moniker ( ) clone ( ) ) ) ) ; } } } }
Follows `expose` declarations down the component tree, starting at `pos`.
Follows ` expose ` declarations down the component tree starting at ` pos ` .
model
&'a Model
pos
&'a mut WalkPosition