Crate capnweb_core

Crate capnweb_core 

Source
Expand description

§Cap’n Web Core Protocol

Core implementation of the Cap’n Web protocol, providing capability-based RPC with promise pipelining.

§Features

  • Capability-based security: Unforgeable object references with fine-grained access control
  • Promise pipelining: Chain dependent calls without waiting for intermediate results
  • IL expression evaluation: Execute complex operations with the Intermediate Language
  • Wire protocol compliance: Full compatibility with the official TypeScript implementation

§Quick Start

use capnweb_core::{CapId, RpcTarget, RpcError, Value};
use async_trait::async_trait;
use serde_json::json;

#[derive(Debug)]
struct Calculator;

#[async_trait]
impl RpcTarget for Calculator {
    async fn call(&self, method: &str, args: Vec<Value>) -> Result<Value, RpcError> {
        match method {
            "add" => {
                // Note: Value is from capnweb_core, not serde_json
                // In a real implementation, you'd need to convert between types
                Ok(Value::String("result".to_string()))
            }
            _ => Err(RpcError::not_found("method not found")),
        }
    }

    async fn get_property(&self, _property: &str) -> Result<Value, RpcError> {
        Err(RpcError::not_found("property access not implemented"))
    }
}

§Protocol Concepts

§Capabilities

Capabilities are unforgeable references to remote objects. They provide secure, fine-grained access control without ambient authority.

§Promise Pipelining

Calls can be chained on promises before they resolve, reducing round-trips:

let user = client.call(cap, "getUser", vec![user_id]);
let profile = client.pipeline(&user, vec!["profile"], "load", vec![]);

§IL (Intermediate Language)

The IL allows expressing complex operations that execute on the server:

let expr = ILExpression::if_expr(
    ILExpression::var(0),
    ILExpression::literal(json!("authenticated")),
    ILExpression::literal(json!("anonymous"))
);

Re-exports§

pub use codec::decode_message;
pub use codec::encode_message;
pub use error::ErrorCode;
pub use error::RpcError;
pub use ids::CallId;
pub use ids::CapId;
pub use ids::PromiseId;
pub use il::Op;
pub use il::Plan;
pub use il::Source;
pub use il_executor::ILExecutor;
pub use il_extended::ILContext;
pub use il_extended::ILError;
pub use il_extended::ILExpression;
pub use il_extended::ILOperation;
pub use il_extended::ILPlan;
pub use msg::Message;
pub use msg::Outcome;
pub use msg::Target;
pub use promise::ArgValue;
pub use promise::ExtendedTarget;
pub use promise::PendingPromise;
pub use promise::PromiseDependencyGraph;
pub use promise_map::MapOperation;
pub use promise_map::PipelinedCall;
pub use promise_map::PromiseMapExecutor;
pub use protocol::capability_registry::CapabilityRegistry;
pub use protocol::capability_registry::RegistrableCapability;
pub use protocol::ids::ExportId;
pub use protocol::ids::ImportId;
pub use protocol::tables::ExportTable;
pub use protocol::tables::ImportTable;
pub use protocol::tables::Value;
pub use protocol::wire::parse_wire_batch;
pub use protocol::wire::serialize_wire_batch;
pub use protocol::wire::PropertyKey;
pub use protocol::wire::WireExpression;
pub use protocol::wire::WireMessage;
pub use protocol::expression::Expression;
pub use protocol::message::Message as LegacyMessage;

Modules§

codec
error
ids
il
il_executor
il_extended
msg
promise
promise_map
protocol
validate

Traits§

RpcTarget

Attribute Macros§

async_trait