navy-nvim-rs 0.0.21

A library for writing neovim rpc clients
//! Handling requests and notifications received from Neovim.
//!
//! The core of a UI client is defining and implementing the
//! [`Handler`].
use std::io;

use rmpv::ValueRef;

use crate::rpc::payload::{NotificationPayload, RequestPayload};

/// A protocol-level response returned for a request from Neovim.
pub type ResponseValue<'a> = Result<ValueRef<'a>, ValueRef<'a>>;

/// The central functionality of a UI client.
pub trait Handler: Send + Sync + Clone + 'static {
    fn handle_notify(&self, _payload: NotificationPayload) -> io::Result<()> {
        Ok(())
    }

    fn handle_request(&self, _payload: RequestPayload) -> io::Result<ResponseValue<'_>> {
        // Note: Delegating response encoding to the request handler by passing `Mutex<Encoder<W>>`
        // instance to this method would be more efficient. However there is currently no use case
        // that requires a request handler to return a response value. Therefore we prioritize API
        // simplicity and make the handler return the response value instead.
        Ok(Err(ValueRef::from("Not implemented")))
    }
}

/// The dummy handler ignores requests and notifications.
///
/// It can be used if a client only wants to send requests to Neovim and get
/// responses.
#[derive(Clone)]
pub struct Dummy;

impl Handler for Dummy {}