Skip to main content

plexus_client/
parent.rs

1//! NEST-16 · what a graft's macro reads of its parent's client.
2//!
3//! `#[plexus_macros::activation(parent = ShopClient)]` infers what each
4//! method calls of its parent from the call chains rooted at the injected
5//! `parent` (`parent.items()`, `parent.ui().slots().list()`). Which link of a
6//! chain is the parent's method — and which are child accessors before it,
7//! or a stream's own methods after it (`.collect()`) — is known only to the
8//! parent's client: every generated client answers it through
9//! [`ParentShape`], and, where its server half is built, the method's shape
10//! hash through [`ParentHash`].
11
12use core::marker::PhantomData;
13
14/// A client's route grammar, for inferring a graft's needs.
15pub trait ParentShape {
16    /// The activation namespace the client speaks to.
17    fn namespace() -> &'static str;
18
19    /// `segments` are a call chain's names as the Rust code wrote them
20    /// (`["ui", "slots", "list", "collect"]`). The wire path of the method the
21    /// chain calls, below this client's node (`"ui.slots.list"`), and its
22    /// shape hash where known; `None` when the chain names no method of this
23    /// client (a dynamic child's or a peer slot's accessor, which take a name
24    /// only known when the code runs).
25    fn resolve(segments: &[&str]) -> Option<(String, Option<String>)>;
26}
27
28/// A client's methods' shape hashes (NEST-7's `Interface::method_hash`),
29/// emitted beside its activation's server half.
30pub trait ParentHash {
31    /// The shape hash of the method named `method` on the wire.
32    fn method_hash(method: &str) -> Option<String>;
33}
34
35#[doc(hidden)]
36pub mod __probe {
37    //! Autoref: a client's [`ParentHash`](super::ParentHash) when it has
38    //! one, `None` when it does not (a client-only build).
39    use super::*;
40
41    #[derive(Debug)]
42    pub struct Probe<C>(PhantomData<C>);
43
44    impl<C> Probe<C> {
45        #[allow(clippy::new_without_default)]
46        pub fn new() -> Self {
47            Probe(PhantomData)
48        }
49    }
50
51    pub trait ViaHash {
52        fn __plexus_hash(&self, method: &str) -> Option<String>;
53    }
54
55    impl<C: super::ParentHash> ViaHash for &Probe<C> {
56        fn __plexus_hash(&self, method: &str) -> Option<String> {
57            C::method_hash(method)
58        }
59    }
60
61    pub trait ViaNone {
62        fn __plexus_hash(&self, method: &str) -> Option<String>;
63    }
64
65    impl<C> ViaNone for Probe<C> {
66        fn __plexus_hash(&self, _method: &str) -> Option<String> {
67            None
68        }
69    }
70}