pub fn parse_binary(raw: &[u8]) -> Option<RpcMessage> {
    let len = raw.len();
    if len < 35 {
        // 30 is the smallest possible JSON-RPC message (notification, single-letter method)
        // + 4 bytes for the header length prefix
        // + 1 byte for the version prefix
        warn!("invalid raw message length ({}) received, ignoring", len);
        return None;
    }

    let version = &raw[0];
    if version != &1 {
        warn!(
            "invalid raw message version ({}) received, ignoring",
            version
        );
        return None;
    }

    // parse as header size + header + body
    let size = LittleEndian::read_u32(&raw[1..4]) as usize;
    if size + 5 > raw.len() {
        warn!(
            "invalid raw message header size ({}) received, ignoring",
            size
        );
        return None;
    }

    let chunks = parse_chunks(&raw[(6 + size)..]);

    let header = match std::str::from_utf8(&raw[5..(5 + size)]) {
        Err(err) => {
            warn!("invalid raw message header (utf-8 parsing error): {}", err);
            return None;
        }
        Ok(s) => s,
    };

    match serde_json::from_str::<RpcMessage>(header) {
        Err(err) => {
            warn!("invalid raw message header: {}", err);
            None
        }
        Ok(RpcMessage::Request(Request::Single(mut req))) => {
            match req {
                Call::Invalid { .. } => {
                    warn!("invalid raw message header: invalid call");
                    return None;
                }
                Call::MethodCall(ref mut meth) => {
                    meth.params = append_params(&meth.params, chunks);
                }
                Call::Notification(ref mut note) => {
                    note.params = append_params(&note.params, chunks);
                }
            };

            trace!("valid binary request parsed: {:?}", req);
            Some(RpcMessage::Request(Request::Single(req)))
        }
        Ok(RpcMessage::Response(Response::Single(mut res))) => {
            match res {
                Output::Success(ref mut succ) => {
                    succ.result = append_values(&succ.result, chunks);
                }
                Output::Failure(ref mut fail) => {
                    fail.error.data = Some(match fail.error.data {
                        None => Value::Array(chunks),
                        Some(ref val) => append_values(&val, chunks),
                    });
                }
            };

            trace!("valid binary response parsed: {:?}", res);
            Some(RpcMessage::Response(Response::Single(res)))
        }
        _ => {
            warn!("invalid raw message header: batch");
            None
        }
    }
}
/// Parses an extended binary message as an RPC Message.
///
/// This is an Armstrong extension to JSON-RPC to facilitate passing binary data along RPC messages.
/// The format is loosely inspired by Khronos' binary GLTF.
///
/// The binary data is expected to contain:
///  - **One byte** as version (expected to be `1`),
///  - **Four bytes** as header length, little-endian u32,
///  - **Header length bytes** as header, UTF-8 string parsed as JSON-RPC,
///  - **One byte** as the number of chunks, omitted if none,
///  - **Remainder** as raw binary chunks, each organised such:
///    - **Four bytes** as chunk length, little-endian u32,
///    - **Chunk length bytes** as a chunk, raw binary data,
///
/// for a minimum length of 35 bytes.
///
/// The handling of the binary data depends on the type of the JSON-RPC message:
///  - If a **Notification**, treat the same as a Request.
///  - If a **Request**, add as parameter:
///     + if the parameter structure is an **Array**, simply append each chunk,
///     + if the parameter structure is an **Object**, insert the data as an array under the `.raw` key.
///  - If a **Response**, vary based on `result` or `error` type:
///     + if a **Structured**, proceed as for Request,
///     + if a **Primitive**, replace with an Array containing `[original, ...chunks]`.
///
/// Only single JSON-RPC calls and responses are supported, not batches.
Parses an extended binary message as an RPC Message.
This is an Armstrong extension to JSON-RPC to facilitate passing binary data along RPC messages.
The format is loosely inspired by Khronos' binary GLTF.



for a minimum length of 35 bytes.

The handling of the binary data depends on the type of the JSON-RPC message:
If a **Notification**, treat the same as a Request.

Only single JSON-RPC calls and responses are supported, not batches.
Parses an extended binary message as an RPC Message . This is an Armstrong extension to JSON - RPC to facilitate passing binary data along RPC messages . The format is loosely inspired by Khronos ' binary GLTF . for a minimum length of 35 bytes . The handling of the binary data depends on the type of the JSON - RPC message : If a ** Notification ** treat the same as a Request . Only single JSON - RPC calls and responses are supported not batches .
pub fn parse_binary(raw: &[u8]) -> Option<RpcMessage> {
    let len = raw.len();
    if len < 35 {
        // 30 is the smallest possible JSON-RPC message (notification, single-letter method)
        // + 4 bytes for the header length prefix
        // + 1 byte for the version prefix
        warn!("invalid raw message length ({}) received, ignoring", len);
        return None;
    }
    let version = &raw[0];
    if version != &1 {
        warn!(
            "invalid raw message version ({}) received, ignoring",
            version
        );
        return None;
    }
    // parse as header size + header + body
    let size = LittleEndian::read_u32(&raw[1..4]) as usize;
    if size + 5 > raw.len() {
        warn!(
            "invalid raw message header size ({}) received, ignoring",
            size
        );
        return None;
    }
    let chunks = parse_chunks(&raw[(6 + size)..]);
    let header = match std::str::from_utf8(&raw[5..(5 + size)]) {
        Err(err) => {
            warn!("invalid raw message header (utf-8 parsing error): {}", err);
            return None;
        }
        Ok(s) => s,
    };
    match serde_json::from_str::<RpcMessage>(header) {
        Err(err) => {
            warn!("invalid raw message header: {}", err);
            None
        }
        Ok(RpcMessage::Request(Request::Single(mut req))) => {
            match req {
                Call::Invalid { .. } => {
                    warn!("invalid raw message header: invalid call");
                    return None;
                }
                Call::MethodCall(ref mut meth) => {
                    meth.params = append_params(&meth.params, chunks);
                }
                Call::Notification(ref mut note) => {
                    note.params = append_params(&note.params, chunks);
                }
            };
            trace!("valid binary request parsed: {:?}", req);
            Some(RpcMessage::Request(Request::Single(req)))
        }
        Ok(RpcMessage::Response(Response::Single(mut res))) => {
            match res {
                Output::Success(ref mut succ) => {
                    succ.result = append_values(&succ.result, chunks);
                }
                Output::Failure(ref mut fail) => {
                    fail.error.data = Some(match fail.error.data {
                        None => Value::Array(chunks),
                        Some(ref val) => append_values(&val, chunks),
                    });
                }
            };
            trace!("valid binary response parsed: {:?}", res);
            Some(RpcMessage::Response(Response::Single(res)))
        }
        _ => {
            warn!("invalid raw message header: batch");
            None
        }
    }
}
pub fn parse_binary ( raw : & [ u8 ] ) -> Option < RpcMessage > { let len = raw . len ( ) ; if len < 35 { // 30 is the smallest possible JSON-RPC message (notification, single-letter method) // + 4 bytes for the header length prefix // + 1 byte for the version prefix warn ! ( " " len ) ; return None ; } let version = & raw [ 0 ] ; if version != & 1 { warn ! ( " " version ) ; return None ; } // parse as header size + header + body let size = LittleEndian :: read_u32 ( & raw [ 1 .. 4 ] ) as usize ; if size + 5 > raw . len ( ) { warn ! ( " " size ) ; return None ; } let chunks = parse_chunks ( & raw [ ( 6 + size ) .. ] ) ; let header = match std :: str :: from_utf8 ( & raw [ 5 .. ( 5 + size ) ] ) { Err ( err ) => { warn ! ( " " err ) ; return None ; } Ok ( s ) => s , } ; match serde_json :: from_str :: < RpcMessage > ( header ) { Err ( err ) => { warn ! ( " " err ) ; None } Ok ( RpcMessage :: Request ( Request :: Single ( mut req ) ) ) => { match req { Call :: Invalid { .. } => { warn ! ( " " ) ; return None ; } Call :: MethodCall ( ref mut meth ) => { meth . params = append_params ( & meth . params , chunks ) ; } Call :: Notification ( ref mut note ) => { note . params = append_params ( & note . params , chunks ) ; } } ; trace ! ( " " req ) ; Some ( RpcMessage :: Request ( Request :: Single ( req ) ) ) } Ok ( RpcMessage :: Response ( Response :: Single ( mut res ) ) ) => { match res { Output :: Success ( ref mut succ ) => { succ . result = append_values ( & succ . result , chunks ) ; } Output :: Failure ( ref mut fail ) => { fail . error . data = Some ( match fail . error . data { None => Value :: Array ( chunks ) , Some ( ref val ) => append_values ( & val , chunks ) , } ) ; } } ; trace ! ( " " res ) ; Some ( RpcMessage :: Response ( Response :: Single ( res ) ) ) } _ => { warn ! ( " " ) ; None } } }
Parses an extended binary message as an RPC Message.
Parses an extended binary message as an RPC Message .
raw
&[u8]