pub use super::bus_ffi::StreamCandidate;
use super::*;

impl core::fmt::Debug for StreamCandidate {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("StreamCandidate")
            .field("peer_pubkey", &self.peer_pubkey())
            .field("topic", &self.topic())
            .finish()
    }
}
impl StreamCandidate {
    /// The public key of the peer this stream is connected to.
    pub fn peer_pubkey(&self) -> PeerPubkey {
        self.inner.peer_pubkey()
    }

    /// The [`Topic`] of the stream.
    pub fn topic(&self) -> Topic {
        self.inner.topic()
    }
    /// Opens the stream with no intent of receiving data: all received messages on the stream
    /// will be dropped immediately.
    pub fn open_write_only(self) -> Stream<()> {
        let stream = self.inner.open(TaggedOption::None).into_rust().unwrap();
        Stream {
            stream: Arc::new(stream),
            receiver: (),
        }
    }
    /// Opens the stream, setting `handler` as its receiver.
    ///
    /// Note that once a stream is open, the callback can never be removed: it will be called
    /// until all instances descending from the returned [`Stream`] have been destroyed.
    pub fn open<OverloadId, Handler>(self, handler: Handler) -> Stream<Handler::Receiver>
    where
        Handler: IntoChannel<OverloadId, Inbound>,
        Handler::Sender: Channel<OverloadId, Inbound>,
    {
        let (mut tx, rx) = handler.into_channel();
        let stream = self
            .inner
            .open(TaggedOption::Some(
                Box::new(move |payload| tx.send_infallibly_mut(Inbound(payload))).into(),
            ))
            .into_rust()
            .unwrap();
        Stream {
            stream: Arc::new(stream),

            receiver: rx,
        }
    }
}
