Crate parsec_interface
source ·Expand description
Parsec Rust Interface
The Parsec Rust Interface provides methods to communicate easily with the Parsec service using the wire protocol and the operation contracts.
For the Parsec service
This library is used by the Parsec service to:
- read from a stream a
Request
sent to the service with theread_from_stream
method - use the
body_to_operation
method of theConvert
trait on a converter to parse the request body into aNativeOperation
use parsec_interface::operations::{Convert, NativeOperation};
use parsec_interface::requests::Request;
use parsec_interface::operations_protobuf::ProtobufConverter;
let converter = ProtobufConverter {};
// stream is a Read object
let request = Request::read_from_stream(&mut stream, 2048).unwrap();
let operation: NativeOperation = converter
.body_to_operation(request.body, request.header.opcode)
.unwrap();
The service can now execute the operation to yield a NativeResult
and:
- use the
result_to_body
method to serialize theNativeResult
- create a
Response
containing the result as its body and write it back to the stream with thewrite_to_stream
method.
use parsec_interface::operations::{Convert, NativeResult, psa_generate_key::Result};
use parsec_interface::requests::{ProviderId, Opcode, BodyType, Response, ResponseStatus};
use parsec_interface::requests::response::ResponseHeader;
use parsec_interface::operations_protobuf::ProtobufConverter;
let converter = ProtobufConverter {};
let result = NativeResult::PsaGenerateKey(Result {});
let result_body = converter.result_to_body(result).unwrap();
let response = Response {
header: ResponseHeader {
provider: ProviderId::MbedCrypto,
session: 0,
content_type: BodyType::Protobuf,
opcode: Opcode::PsaGenerateKey,
status: ResponseStatus::Success,
},
body: result_body,
};
// stream is a Write object
response.write_to_stream(&mut stream).unwrap();
For the Parsec Rust clients
This library is used by the Parsec Rust clients to:
- use the
operation_to_body
method to serialize theNativeOperation
to be sent as body of aRequest
- write it to the stream with the
write_to_stream
method.
use parsec_interface::operations::{Convert, NativeOperation};
use parsec_interface::requests::{Request, ProviderId, BodyType, AuthType, Opcode};
use parsec_interface::requests::request::{RequestHeader, RequestAuth};
use parsec_interface::operations_protobuf::ProtobufConverter;
use parsec_interface::operations::ping::Operation;
let converter = ProtobufConverter {};
let operation = NativeOperation::Ping(Operation {});
let request = Request {
header: RequestHeader {
provider: ProviderId::Core,
session: 0,
content_type: BodyType::Protobuf,
accept_type: BodyType::Protobuf,
auth_type: AuthType::Direct,
opcode: Opcode::Ping,
},
body: converter.operation_to_body(operation).unwrap(),
auth: RequestAuth::new(Vec::from("root")),
};
// stream is a Write object
request.write_to_stream(&mut stream).unwrap();
After the operation has been executed by the Parsec service:
- read from a stream the
Response
from the service with theread_from_stream
method - use the
body_to_result
method to parse the result body into aNativeResult
use parsec_interface::operations::{Convert, NativeResult};
use parsec_interface::requests::Response;
use parsec_interface::operations_protobuf::ProtobufConverter;
let converter = ProtobufConverter {};
// stream is a Read object
let response = Response::read_from_stream(&mut stream, 2048).unwrap();
let result: NativeResult = converter
.body_to_result(response.body, response.header.opcode)
.unwrap();
See the Parsec Test client as an example of a Rust client.
Re-exports
pub use secrecy;
Modules
- Rust representation of operations
- Protobuf converter
- Request and response definitions