RpcClient

Struct RpcClient 

Source
pub struct RpcClient { /* private fields */ }
Expand description

Unified RPC client for calling host functions

Implementations§

Source§

impl RpcClient

Source

pub fn new() -> Self

Examples found in repository?
examples/rpc.rs (line 5)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let mut client = RpcClient::new();
6
7    // Example 1: Simple ping
8    println!("=== Example 1: Simple Ping ===");
9    match client.ping() {
10        Ok(response) => println!("Ping response: {}", response),
11        Err(e) => println!("Ping error: {}", e),
12    }
13
14    // Example 2: Echo with different data types
15    println!("\n=== Example 2: Echo Examples ===");
16
17    // Echo string
18    match client.echo("Hello, World!".to_string()) {
19        Ok(response) => println!("Echo string: {}", response),
20        Err(e) => println!("Echo error: {}", e),
21    }
22
23    // Echo number
24    match client.echo(42) {
25        Ok(response) => println!("Echo number: {}", response),
26        Err(e) => println!("Echo error: {}", e),
27    }
28
29    // Echo complex object
30    #[derive(Serialize, Deserialize, Debug)]
31    struct Person {
32        name: String,
33        age: u32,
34    }
35
36    let person = Person {
37        name: "Alice".to_string(),
38        age: 30,
39    };
40
41    match client.echo(person) {
42        Ok(response) => println!("Echo person: {:?}", response),
43        Err(e) => println!("Echo error: {}", e),
44    }
45
46    // Example 3: Get version
47    println!("\n=== Example 3: Get Version ===");
48    match client.version() {
49        Ok(version) => {
50            println!("Version info:");
51            for (key, value) in version {
52                println!("  {}: {}", key, value);
53            }
54        }
55        Err(e) => println!("Version error: {}", e),
56    }
57
58    // Example 4: Generic call with custom types
59    println!("\n=== Example 4: Generic Call ===");
60
61    #[derive(Serialize, Deserialize, Debug)]
62    struct CustomRequest {
63        message: String,
64        count: u32,
65    }
66
67    #[derive(Serialize, Deserialize, Debug)]
68    struct CustomResponse {
69        processed: String,
70        timestamp: u64,
71    }
72
73    let request = CustomRequest {
74        message: "Test message".to_string(),
75        count: 5,
76    };
77
78    // This would fail since "custom.process" doesn't exist in our test implementation
79    match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80        Ok(response) => {
81            if let Some(result) = response.result {
82                println!("Custom response: {:?}", result);
83            } else if let Some(error) = response.error {
84                println!("Custom error: {} (code: {})", error.message, error.code);
85            }
86        }
87        Err(e) => println!("Custom call error: {}", e),
88    }
89
90    // Example 5: Error handling
91    println!("\n=== Example 5: Error Handling ===");
92
93    // Try calling a non-existent method
94    match client.call::<(), String>("nonexistent.method", None) {
95        Ok(response) => {
96            if let Some(error) = response.error {
97                println!("Expected error: {} (code: {})", error.message, error.code);
98            }
99        }
100        Err(e) => println!("Call error: {}", e),
101    }
102
103    println!("\nAll examples completed!");
104    Ok(())
105}
Source

pub fn with_buffer_size(buffer_size: usize) -> Self

Source

pub fn set_buffer_size(&mut self, buffer_size: usize)

Source

pub fn buffer_size(&self) -> usize

Source

pub fn call<P: Serialize, R: DeserializeOwned>( &mut self, method: &str, params: Option<P>, ) -> Result<JsonRpcResponse<R>, RpcError>

Examples found in repository?
examples/rpc.rs (line 79)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let mut client = RpcClient::new();
6
7    // Example 1: Simple ping
8    println!("=== Example 1: Simple Ping ===");
9    match client.ping() {
10        Ok(response) => println!("Ping response: {}", response),
11        Err(e) => println!("Ping error: {}", e),
12    }
13
14    // Example 2: Echo with different data types
15    println!("\n=== Example 2: Echo Examples ===");
16
17    // Echo string
18    match client.echo("Hello, World!".to_string()) {
19        Ok(response) => println!("Echo string: {}", response),
20        Err(e) => println!("Echo error: {}", e),
21    }
22
23    // Echo number
24    match client.echo(42) {
25        Ok(response) => println!("Echo number: {}", response),
26        Err(e) => println!("Echo error: {}", e),
27    }
28
29    // Echo complex object
30    #[derive(Serialize, Deserialize, Debug)]
31    struct Person {
32        name: String,
33        age: u32,
34    }
35
36    let person = Person {
37        name: "Alice".to_string(),
38        age: 30,
39    };
40
41    match client.echo(person) {
42        Ok(response) => println!("Echo person: {:?}", response),
43        Err(e) => println!("Echo error: {}", e),
44    }
45
46    // Example 3: Get version
47    println!("\n=== Example 3: Get Version ===");
48    match client.version() {
49        Ok(version) => {
50            println!("Version info:");
51            for (key, value) in version {
52                println!("  {}: {}", key, value);
53            }
54        }
55        Err(e) => println!("Version error: {}", e),
56    }
57
58    // Example 4: Generic call with custom types
59    println!("\n=== Example 4: Generic Call ===");
60
61    #[derive(Serialize, Deserialize, Debug)]
62    struct CustomRequest {
63        message: String,
64        count: u32,
65    }
66
67    #[derive(Serialize, Deserialize, Debug)]
68    struct CustomResponse {
69        processed: String,
70        timestamp: u64,
71    }
72
73    let request = CustomRequest {
74        message: "Test message".to_string(),
75        count: 5,
76    };
77
78    // This would fail since "custom.process" doesn't exist in our test implementation
79    match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80        Ok(response) => {
81            if let Some(result) = response.result {
82                println!("Custom response: {:?}", result);
83            } else if let Some(error) = response.error {
84                println!("Custom error: {} (code: {})", error.message, error.code);
85            }
86        }
87        Err(e) => println!("Custom call error: {}", e),
88    }
89
90    // Example 5: Error handling
91    println!("\n=== Example 5: Error Handling ===");
92
93    // Try calling a non-existent method
94    match client.call::<(), String>("nonexistent.method", None) {
95        Ok(response) => {
96            if let Some(error) = response.error {
97                println!("Expected error: {} (code: {})", error.message, error.code);
98            }
99        }
100        Err(e) => println!("Call error: {}", e),
101    }
102
103    println!("\nAll examples completed!");
104    Ok(())
105}
Source

pub fn ping(&mut self) -> Result<String, RpcError>

Convenience method for ping

Examples found in repository?
examples/rpc.rs (line 9)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let mut client = RpcClient::new();
6
7    // Example 1: Simple ping
8    println!("=== Example 1: Simple Ping ===");
9    match client.ping() {
10        Ok(response) => println!("Ping response: {}", response),
11        Err(e) => println!("Ping error: {}", e),
12    }
13
14    // Example 2: Echo with different data types
15    println!("\n=== Example 2: Echo Examples ===");
16
17    // Echo string
18    match client.echo("Hello, World!".to_string()) {
19        Ok(response) => println!("Echo string: {}", response),
20        Err(e) => println!("Echo error: {}", e),
21    }
22
23    // Echo number
24    match client.echo(42) {
25        Ok(response) => println!("Echo number: {}", response),
26        Err(e) => println!("Echo error: {}", e),
27    }
28
29    // Echo complex object
30    #[derive(Serialize, Deserialize, Debug)]
31    struct Person {
32        name: String,
33        age: u32,
34    }
35
36    let person = Person {
37        name: "Alice".to_string(),
38        age: 30,
39    };
40
41    match client.echo(person) {
42        Ok(response) => println!("Echo person: {:?}", response),
43        Err(e) => println!("Echo error: {}", e),
44    }
45
46    // Example 3: Get version
47    println!("\n=== Example 3: Get Version ===");
48    match client.version() {
49        Ok(version) => {
50            println!("Version info:");
51            for (key, value) in version {
52                println!("  {}: {}", key, value);
53            }
54        }
55        Err(e) => println!("Version error: {}", e),
56    }
57
58    // Example 4: Generic call with custom types
59    println!("\n=== Example 4: Generic Call ===");
60
61    #[derive(Serialize, Deserialize, Debug)]
62    struct CustomRequest {
63        message: String,
64        count: u32,
65    }
66
67    #[derive(Serialize, Deserialize, Debug)]
68    struct CustomResponse {
69        processed: String,
70        timestamp: u64,
71    }
72
73    let request = CustomRequest {
74        message: "Test message".to_string(),
75        count: 5,
76    };
77
78    // This would fail since "custom.process" doesn't exist in our test implementation
79    match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80        Ok(response) => {
81            if let Some(result) = response.result {
82                println!("Custom response: {:?}", result);
83            } else if let Some(error) = response.error {
84                println!("Custom error: {} (code: {})", error.message, error.code);
85            }
86        }
87        Err(e) => println!("Custom call error: {}", e),
88    }
89
90    // Example 5: Error handling
91    println!("\n=== Example 5: Error Handling ===");
92
93    // Try calling a non-existent method
94    match client.call::<(), String>("nonexistent.method", None) {
95        Ok(response) => {
96            if let Some(error) = response.error {
97                println!("Expected error: {} (code: {})", error.message, error.code);
98            }
99        }
100        Err(e) => println!("Call error: {}", e),
101    }
102
103    println!("\nAll examples completed!");
104    Ok(())
105}
Source

pub fn echo<T: Serialize + DeserializeOwned>( &mut self, data: T, ) -> Result<T, RpcError>

Convenience method for echo

Examples found in repository?
examples/rpc.rs (line 18)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let mut client = RpcClient::new();
6
7    // Example 1: Simple ping
8    println!("=== Example 1: Simple Ping ===");
9    match client.ping() {
10        Ok(response) => println!("Ping response: {}", response),
11        Err(e) => println!("Ping error: {}", e),
12    }
13
14    // Example 2: Echo with different data types
15    println!("\n=== Example 2: Echo Examples ===");
16
17    // Echo string
18    match client.echo("Hello, World!".to_string()) {
19        Ok(response) => println!("Echo string: {}", response),
20        Err(e) => println!("Echo error: {}", e),
21    }
22
23    // Echo number
24    match client.echo(42) {
25        Ok(response) => println!("Echo number: {}", response),
26        Err(e) => println!("Echo error: {}", e),
27    }
28
29    // Echo complex object
30    #[derive(Serialize, Deserialize, Debug)]
31    struct Person {
32        name: String,
33        age: u32,
34    }
35
36    let person = Person {
37        name: "Alice".to_string(),
38        age: 30,
39    };
40
41    match client.echo(person) {
42        Ok(response) => println!("Echo person: {:?}", response),
43        Err(e) => println!("Echo error: {}", e),
44    }
45
46    // Example 3: Get version
47    println!("\n=== Example 3: Get Version ===");
48    match client.version() {
49        Ok(version) => {
50            println!("Version info:");
51            for (key, value) in version {
52                println!("  {}: {}", key, value);
53            }
54        }
55        Err(e) => println!("Version error: {}", e),
56    }
57
58    // Example 4: Generic call with custom types
59    println!("\n=== Example 4: Generic Call ===");
60
61    #[derive(Serialize, Deserialize, Debug)]
62    struct CustomRequest {
63        message: String,
64        count: u32,
65    }
66
67    #[derive(Serialize, Deserialize, Debug)]
68    struct CustomResponse {
69        processed: String,
70        timestamp: u64,
71    }
72
73    let request = CustomRequest {
74        message: "Test message".to_string(),
75        count: 5,
76    };
77
78    // This would fail since "custom.process" doesn't exist in our test implementation
79    match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80        Ok(response) => {
81            if let Some(result) = response.result {
82                println!("Custom response: {:?}", result);
83            } else if let Some(error) = response.error {
84                println!("Custom error: {} (code: {})", error.message, error.code);
85            }
86        }
87        Err(e) => println!("Custom call error: {}", e),
88    }
89
90    // Example 5: Error handling
91    println!("\n=== Example 5: Error Handling ===");
92
93    // Try calling a non-existent method
94    match client.call::<(), String>("nonexistent.method", None) {
95        Ok(response) => {
96            if let Some(error) = response.error {
97                println!("Expected error: {} (code: {})", error.message, error.code);
98            }
99        }
100        Err(e) => println!("Call error: {}", e),
101    }
102
103    println!("\nAll examples completed!");
104    Ok(())
105}
Source

pub fn version(&mut self) -> Result<HashMap<String, String>, RpcError>

Convenience method for getting version

Examples found in repository?
examples/rpc.rs (line 48)
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let mut client = RpcClient::new();
6
7    // Example 1: Simple ping
8    println!("=== Example 1: Simple Ping ===");
9    match client.ping() {
10        Ok(response) => println!("Ping response: {}", response),
11        Err(e) => println!("Ping error: {}", e),
12    }
13
14    // Example 2: Echo with different data types
15    println!("\n=== Example 2: Echo Examples ===");
16
17    // Echo string
18    match client.echo("Hello, World!".to_string()) {
19        Ok(response) => println!("Echo string: {}", response),
20        Err(e) => println!("Echo error: {}", e),
21    }
22
23    // Echo number
24    match client.echo(42) {
25        Ok(response) => println!("Echo number: {}", response),
26        Err(e) => println!("Echo error: {}", e),
27    }
28
29    // Echo complex object
30    #[derive(Serialize, Deserialize, Debug)]
31    struct Person {
32        name: String,
33        age: u32,
34    }
35
36    let person = Person {
37        name: "Alice".to_string(),
38        age: 30,
39    };
40
41    match client.echo(person) {
42        Ok(response) => println!("Echo person: {:?}", response),
43        Err(e) => println!("Echo error: {}", e),
44    }
45
46    // Example 3: Get version
47    println!("\n=== Example 3: Get Version ===");
48    match client.version() {
49        Ok(version) => {
50            println!("Version info:");
51            for (key, value) in version {
52                println!("  {}: {}", key, value);
53            }
54        }
55        Err(e) => println!("Version error: {}", e),
56    }
57
58    // Example 4: Generic call with custom types
59    println!("\n=== Example 4: Generic Call ===");
60
61    #[derive(Serialize, Deserialize, Debug)]
62    struct CustomRequest {
63        message: String,
64        count: u32,
65    }
66
67    #[derive(Serialize, Deserialize, Debug)]
68    struct CustomResponse {
69        processed: String,
70        timestamp: u64,
71    }
72
73    let request = CustomRequest {
74        message: "Test message".to_string(),
75        count: 5,
76    };
77
78    // This would fail since "custom.process" doesn't exist in our test implementation
79    match client.call::<CustomRequest, CustomResponse>("custom.process", Some(request)) {
80        Ok(response) => {
81            if let Some(result) = response.result {
82                println!("Custom response: {:?}", result);
83            } else if let Some(error) = response.error {
84                println!("Custom error: {} (code: {})", error.message, error.code);
85            }
86        }
87        Err(e) => println!("Custom call error: {}", e),
88    }
89
90    // Example 5: Error handling
91    println!("\n=== Example 5: Error Handling ===");
92
93    // Try calling a non-existent method
94    match client.call::<(), String>("nonexistent.method", None) {
95        Ok(response) => {
96            if let Some(error) = response.error {
97                println!("Expected error: {} (code: {})", error.message, error.code);
98            }
99        }
100        Err(e) => println!("Call error: {}", e),
101    }
102
103    println!("\nAll examples completed!");
104    Ok(())
105}

Trait Implementations§

Source§

impl Default for RpcClient

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,