pub(crate) fn parse_jsonrpc_response<T>(response_bytes: &[u8]) -> Result<T>
where
    T: DeserializeOwned,
{
    let res_payload = std::str::from_utf8(response_bytes)
        .map_err(|err| Error::ClientError(format!("Failed to decode response data: {}", err)))?;

    match serde_json::from_str(&res_payload) {
        Ok(JsonRpcResponse {
            jsonrpc,
            result: Some(r),
            ..
        }) => {
            if jsonrpc != SN_AUTHD_JSONRPC_VERSION {
                Err(Error::ClientError(format!(
                    "JSON-RPC version {} not supported, only version {} is supported",
                    jsonrpc, SN_AUTHD_JSONRPC_VERSION
                )))
            } else {
                let result = serde_json::from_value(r).map_err(|err| {
                    Error::ClientError(format!("Failed to decode response result: {}", err))
                })?;

                Ok(result)
            }
        }
        Ok(JsonRpcResponse {
            error: Some(err), ..
        }) => Err(Error::RemoteEndpointError(err.message)),
        Ok(JsonRpcResponse {
            result: None,
            error: None,
            ..
        }) => Err(Error::ClientError(
            "Received an invalid JSON-RPC response from authd".to_string(),
        )),
        Err(err) => Err(Error::ClientError(format!(
            "Failed to parse authd response: {}",
            err
        ))),
    }
}
// Parse bytes to construct a JsonRpcResponse expected to contain a result of type T
Parse bytes to construct a JsonRpcResponse expected to contain a result of type T
Parse bytes to construct a JsonRpcResponse expected to contain a result of type T
pub(crate) fn parse_jsonrpc_response<T>(response_bytes: &[u8]) -> Result<T>
where
    T: DeserializeOwned,
{
    let res_payload = std::str::from_utf8(response_bytes)
        .map_err(|err| Error::ClientError(format!("Failed to decode response data: {}", err)))?;
    match serde_json::from_str(&res_payload) {
        Ok(JsonRpcResponse {
            jsonrpc,
            result: Some(r),
            ..
        }) => {
            if jsonrpc != SN_AUTHD_JSONRPC_VERSION {
                Err(Error::ClientError(format!(
                    "JSON-RPC version {} not supported, only version {} is supported",
                    jsonrpc, SN_AUTHD_JSONRPC_VERSION
                )))
            } else {
                let result = serde_json::from_value(r).map_err(|err| {
                    Error::ClientError(format!("Failed to decode response result: {}", err))
                })?;
                Ok(result)
            }
        }
        Ok(JsonRpcResponse {
            error: Some(err), ..
        }) => Err(Error::RemoteEndpointError(err.message)),
        Ok(JsonRpcResponse {
            result: None,
            error: None,
            ..
        }) => Err(Error::ClientError(
            "Received an invalid JSON-RPC response from authd".to_string(),
        )),
        Err(err) => Err(Error::ClientError(format!(
            "Failed to parse authd response: {}",
            err
        ))),
    }
}
pub ( crate ) fn parse_jsonrpc_response < T > ( response_bytes : & [ u8 ] ) -> Result < T > where T : DeserializeOwned , { let res_payload = std :: str :: from_utf8 ( response_bytes ) . map_err ( | err | Error :: ClientError ( format ! ( " " err ) ) ) ? ; match serde_json :: from_str ( & res_payload ) { Ok ( JsonRpcResponse { jsonrpc , result : Some ( r ) , .. } ) => { if jsonrpc != SN_AUTHD_JSONRPC_VERSION { Err ( Error :: ClientError ( format ! ( " " jsonrpc SN_AUTHD_JSONRPC_VERSION ) ) ) } else { let result = serde_json :: from_value ( r ) . map_err ( | err | { Error :: ClientError ( format ! ( " " err ) ) } ) ? ; Ok ( result ) } } Ok ( JsonRpcResponse { error : Some ( err ) , .. } ) => Err ( Error :: RemoteEndpointError ( err . message ) ) , Ok ( JsonRpcResponse { result : None , error : None , .. } ) => Err ( Error :: ClientError ( " " . to_string ( ) , ) ) , Err ( err ) => Err ( Error :: ClientError ( format ! ( " " err ) ) ) , } }
Parse bytes to construct a JsonRpcResponse expected to contain a result of type T
Parse bytes to construct a JsonRpcResponse expected to contain a result of type T
response_bytes
&[u8]