From c94077f29b025fc1784a1dc5930635a09dcc43ce Mon Sep 17 00:00:00 2001 From: Michael Uti Date: Tue, 25 Jul 2023 17:42:49 +0100 Subject: [PATCH] Update rust library examples --- reference/libraries/rust/credentials.md | 6 ++++++ reference/libraries/rust/nodes.md | 7 ++++--- reference/libraries/rust/routing.md | 19 ++++++++++++++----- reference/libraries/rust/secure-channels.md | 3 +++ .../libraries/rust/vaults-and-identities.md | 4 +++- 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/reference/libraries/rust/credentials.md b/reference/libraries/rust/credentials.md index 2a23c280..d2e89d1d 100644 --- a/reference/libraries/rust/credentials.md +++ b/reference/libraries/rust/credentials.md @@ -42,6 +42,7 @@ In a later guide, we'll explore how Ockam enables you to define various pluggabl {% code lineNumbers="true" %} ```rust +// examples/06-credentials-exchange-issuer.rs use ockam::access_control::AllowAll; use ockam::access_control::IdentityIdAccessControl; use ockam::identity::CredentialsIssuer; @@ -112,6 +113,7 @@ async fn main(ctx: Context) -> Result<()> { println!("issuer started"); Ok(()) } + ``` {% endcode %} @@ -127,6 +129,7 @@ touch examples/06-credential-exchange-server.rs {% code lineNumbers="true" %} ```rust +// examples/06-credentials-exchange-server.rs // This node starts a tcp listener, a secure channel listener, and an echoer worker. // It then runs forever waiting for messages. use hello_ockam::Echoer; @@ -222,6 +225,7 @@ async fn main(ctx: Context) -> Result<()> { println!("server started"); Ok(()) } + ``` {% endcode %} @@ -237,6 +241,7 @@ touch examples/06-credential-exchange-client.rs {% code lineNumbers="true" %} ```rust +// examples/06-credentials-exchange-client.rs use ockam::identity::{AuthorityService, CredentialsIssuerClient, SecureChannelOptions, TrustContext}; use ockam::TcpTransportExtension; use ockam::{node, route, Context, Result, TcpConnectionOptions}; @@ -318,6 +323,7 @@ async fn main(ctx: Context) -> Result<()> { node.stop().await } + ``` {% endcode %} diff --git a/reference/libraries/rust/nodes.md b/reference/libraries/rust/nodes.md index 139fdfaa..73f9aab3 100644 --- a/reference/libraries/rust/nodes.md +++ b/reference/libraries/rust/nodes.md @@ -140,7 +140,6 @@ Add the following code to this file: ```rust // src/echoer.rs - use ockam::{Context, Result, Routed, Worker}; pub struct Echoer; @@ -157,6 +156,7 @@ impl Worker for Echoer { ctx.send(msg.return_route(), msg.body()).await } } + ``` Note that we define the `Message` associated type of the worker as `String`, which specifies that this worker expects to handle `String` messages. We then go on to define a `handle_message(..)` function that will be called whenever a new message arrives for this worker. @@ -166,10 +166,10 @@ In the Echoer's `handle_message(..)`, we print any incoming message, along with To make this Echoer type accessible to our main program, export it from `src/lib.rs` file by adding the following to it: ```rust -// src/lib.rs - mod echoer; + pub use echoer::*; + ``` #### App worker @@ -211,6 +211,7 @@ async fn main(ctx: Context) -> Result<()> { // Stop all workers, stop the node, cleanup and return. node.stop().await } + ``` To run this new node program: diff --git a/reference/libraries/rust/routing.md b/reference/libraries/rust/routing.md index b3af034b..fa43d9fe 100644 --- a/reference/libraries/rust/routing.md +++ b/reference/libraries/rust/routing.md @@ -76,8 +76,7 @@ Add the following code to this file: ```rust // src/hop.rs - -use ockam::{Any, Context, Result, Routed, Worker}; +use ockam::{Any, Context, LocalMessage, Result, Routed, Worker}; pub struct Hop; @@ -92,8 +91,7 @@ impl Worker for Hop { println!("Address: {}, Received: {}", ctx.address(), msg); // Some type conversion - let mut message = msg.into_local_message(); - let transport_message = message.transport_mut(); + let mut transport_message = msg.into_local_message().into_transport_message(); // Remove my address from the onward_route transport_message.onward_route.step()?; @@ -101,10 +99,14 @@ impl Worker for Hop { // Insert my address at the beginning return_route transport_message.return_route.modify().prepend(ctx.address()); + // Wipe all local info (e.g. transport types) + let message = LocalMessage::new(transport_message, vec![]); + // Send the message on its onward_route ctx.forward(message).await } } + ``` To make this `Hop` type accessible to our main program, export it from `src/lib.rs` by adding the following to it: @@ -161,6 +163,7 @@ async fn main(ctx: Context) -> Result<()> { // Stop all workers, stop the node, cleanup and return. node.stop().await } + ``` To run this new node program: @@ -216,6 +219,7 @@ async fn main(ctx: Context) -> Result<()> { // Stop all workers, stop the node, cleanup and return. node.stop().await } + ``` To run this new node program: @@ -270,6 +274,7 @@ async fn main(ctx: Context) -> Result<()> { // Don't call node.stop() here so this node runs forever. Ok(()) } + ``` #### Initiator node @@ -309,6 +314,7 @@ async fn main(ctx: Context) -> Result<()> { // Stop all workers, stop the node, cleanup and return. node.stop().await } + ``` #### Run @@ -351,7 +357,6 @@ Add the following code to this file: ```rust // src/forwarder.rs - use ockam::{Address, Any, Context, LocalMessage, Result, Routed, Worker}; pub struct Forwarder(pub Address); @@ -393,6 +398,7 @@ impl Worker for Forwarder { ctx.forward(message).await } } + ``` To make this `Forwarder` type accessible to our main program, export it from `src/lib.rs` by adding the following to it: @@ -440,6 +446,7 @@ async fn main(ctx: Context) -> Result<()> { // Don't call node.stop() here so this node runs forever. Ok(()) } + ``` #### Middle node @@ -487,6 +494,7 @@ async fn main(ctx: Context) -> Result<()> { // Don't call node.stop() here so this node runs forever. Ok(()) } + ``` #### Initiator node @@ -525,6 +533,7 @@ async fn main(ctx: Context) -> Result<()> { // Stop all workers, stop the node, cleanup and return. node.stop().await } + ``` #### Run diff --git a/reference/libraries/rust/secure-channels.md b/reference/libraries/rust/secure-channels.md index 8ae64f91..d272bc64 100644 --- a/reference/libraries/rust/secure-channels.md +++ b/reference/libraries/rust/secure-channels.md @@ -66,6 +66,7 @@ async fn main(ctx: Context) -> Result<()> { // Don't call node.stop() here so this node runs forever. Ok(()) } + ``` ### Middle node @@ -112,6 +113,7 @@ async fn main(ctx: Context) -> Result<()> { // Don't call node.stop() here so this node runs forever. Ok(()) } + ``` ### Initiator node @@ -160,6 +162,7 @@ async fn main(ctx: Context) -> Result<()> { // Stop all workers, stop the node, cleanup and return. node.stop().await } + ``` ### Run diff --git a/reference/libraries/rust/vaults-and-identities.md b/reference/libraries/rust/vaults-and-identities.md index faa01379..66065771 100644 --- a/reference/libraries/rust/vaults-and-identities.md +++ b/reference/libraries/rust/vaults-and-identities.md @@ -7,6 +7,7 @@ description: >- # Vaults and Identities ```rust +// examples/vault-and-identities.rs use ockam::node; use ockam::{Context, Result}; @@ -16,9 +17,10 @@ async fn main(ctx: Context) -> Result<()> { let mut node = node(ctx); // Create an Identity to represent Alice. - let alice = node.create_identity().await?; + let _alice = node.create_identity().await?; // Stop the node. node.stop().await } + ```