//! Tauri implementation of the shared outbound stream sink contract.
//!
//! The backend core owns the transport-agnostic [`backend::stream::OutputSink`]
//! abstraction. This adapter is the only layer that turns it into a concrete
//! [`tauri::ipc::Channel`] send.

use backend::stream::{OutputSink, OutputSinkError};
use serde::Serialize;
use tauri::ipc::Channel;

/// [`OutputSink`] backed by a per-attach Tauri IPC [`Channel`].
pub struct TauriChannelSink<T> {
    channel: Channel<T>,
}

impl<T> TauriChannelSink<T> {
    /// Wraps a Tauri IPC channel as a shared backend stream sink.
    #[must_use]
    pub fn new(channel: Channel<T>) -> Self {
        Self { channel }
    }
}

impl<T> OutputSink<T> for TauriChannelSink<T>
where
    T: Serialize + Send + Sync + 'static,
{
    fn send(&self, item: T) -> Result<(), OutputSinkError> {
        self.channel.send(item).map_err(|_| OutputSinkError::Closed)
    }
}
