jsonrpce 0.1.0

JSON-RPC 2.0 for Rust
Documentation
use std::collections::HashMap;
use std::io::{self, BufReader};

use crate::Transport;
// We reuse the generic logic and the Handler type alias
use crate::transports::{BoxedHandler, Stream};

/// A zero-cost wrapper that initializes a StreamTransport over Stdin/Stdout.
#[derive(Default)]
pub struct Stdio;

impl Stdio {
    pub fn new() -> Self {
        Stdio
    }
}

impl<C> Transport<C> for Stdio
where
    C: Send + Sync,
{
    type Handler = BoxedHandler<C>;

    type Output = ();
    type Error = std::io::Error;

    fn serve(
        self,
        ctx: C,
        handlers: HashMap<String, Self::Handler>,
    ) -> Result<Self::Output, Self::Error> {
        let stdin = io::stdin();
        // We lock stdin because `StdinLock` implements `BufRead`, which StreamTransport requires.
        // Wrapping it in BufReader adds explicit buffering logic.
        let reader = BufReader::new(stdin.lock());

        // Prepare Output (Writer)
        let writer = io::stdout();

        // Delegate to the Generic StreamTransport
        // Since `handlers` are the same type, we pass them straight through.
        Stream::new(reader, writer).serve(ctx, handlers)
    }
}