Skip to content

Commit 2d9990a

Browse files
added missing backticks, tested all scenarios again and added missing stuff
Signed-off-by: Anton Engelhardt <[email protected]>
1 parent 084d039 commit 2d9990a

File tree

1 file changed

+62
-31
lines changed

1 file changed

+62
-31
lines changed

src/traits.rs

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ pub trait Context {
8585
///
8686
/// use log::warn;
8787
///
88-
/// struct MyContext;
88+
/// struct MyPlugin;
8989
///
90-
/// impl HttpContext for MyContext {
90+
/// impl HttpContext for MyPlugin {
9191
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
9292
/// match self.dispatch_http_call(
93-
/// "cluster_name",
93+
/// "cluster_name_from_envoy_config",
9494
/// vec![
9595
/// (":method", "GET"),
9696
/// (":path", "/"),
@@ -108,7 +108,7 @@ pub trait Context {
108108
/// }
109109
/// }
110110
///
111-
/// impl Context for MyContext {
111+
/// impl Context for MyPlugin {
112112
/// fn on_http_call_response(&mut self, _token_id: u32, _: usize, body_size: usize, _: usize) {
113113
/// let headers = self.get_http_call_response_headers();
114114
/// let body = self.get_http_call_response_body(0, body_size);
@@ -151,14 +151,14 @@ pub trait Context {
151151
/// use proxy_wasm::traits::*;
152152
/// use proxy_wasm::types::*;
153153
///
154-
/// use log::warn;
154+
/// use log::{debug, warn};
155155
///
156-
/// struct MyContext;
156+
/// struct MyPlugin;
157157
///
158-
/// impl HttpContext for MyContext {
158+
/// impl HttpContext for MyPlugin {
159159
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
160160
/// match self.dispatch_http_call(
161-
/// "cluster_name",
161+
/// "google",
162162
/// vec![
163163
/// (":method", "GET"),
164164
/// (":path", "/"),
@@ -176,12 +176,12 @@ pub trait Context {
176176
/// }
177177
/// }
178178
///
179-
/// impl Context for MyContext {
179+
/// impl Context for MyPlugin {
180180
/// fn on_http_call_response(&mut self, _token_id: u32, _: usize, body_size: usize, _: usize) {
181181
/// let headers = self.get_http_call_response_headers();
182182
/// let body = self.get_http_call_response_body(0, body_size);
183183
///
184-
/// info!("Received response headers: {:?}", headers);
184+
/// debug!("Received response headers: {:?}", headers);
185185
///
186186
/// // Do something with the response
187187
/// }
@@ -345,29 +345,33 @@ pub trait RootContext: Context {
345345
///
346346
/// * `bool` - `true` if the configuration was processed successfully, `false` otherwise
347347
///
348-
/// /// # Example
348+
/// # Example
349349
///
350350
/// ```rust
351351
/// use proxy_wasm::traits::RootContext;
352352
///
353353
/// struct MyRootContext;
354354
///
355+
/// #[derive(serde::Deserialize)]
356+
/// #[derive(Debug)]
355357
/// struct MyVmConfiguration {
356358
/// /// Some key
357359
/// pub key: String,
358360
/// }
359361
///
360362
/// impl RootContext for MyRootContext {
361363
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
362-
/// let vm_confuguration = self.get_vm_configuration().unwrap();
364+
/// let vm_configuration = self.get_vm_configuration().unwrap();
363365
///
364-
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&vm_confuguration).unwrap();
366+
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&vm_configuration).unwrap();
365367
///
366368
/// // Do something with the parsed vm configuration
369+
/// debug!("vm_configuration: {:?}", parsed_vm_configuration)
367370
///
368371
/// true
369372
/// }
370373
/// }
374+
/// ```
371375
fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
372376
true
373377
}
@@ -385,22 +389,26 @@ pub trait RootContext: Context {
385389
///
386390
/// struct MyRootContext;
387391
///
392+
/// #[derive(serde::Deserialize)]
393+
/// #[derive(Debug)]
388394
/// struct MyVmConfiguration {
389395
/// /// Some key
390396
/// pub key: String,
391397
/// }
392398
///
393399
/// impl RootContext for MyRootContext {
394400
/// fn on_vm_start(&mut self, _vm_configuration_size: usize) -> bool {
395-
/// let vm_confuguration = self.get_vm_configuration().unwrap();
401+
/// let vm_configuration = self.get_vm_configuration().unwrap();
396402
///
397-
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&vm_confuguration).unwrap();
403+
/// let parsed_vm_configuration: MyVmConfiguration = serde_json::from_slice::<MyVmConfiguration>(&vm_configuration).unwrap();
398404
///
399405
/// // Do something with the parsed vm configuration
406+
/// debug!("vm_configuration: {:?}", parsed_vm_configuration)
400407
///
401408
/// true
402409
/// }
403410
/// }
411+
/// ```
404412
fn get_vm_configuration(&self) -> Option<Bytes> {
405413
hostcalls::get_buffer(BufferType::VmConfiguration, 0, usize::MAX).unwrap()
406414
}
@@ -423,6 +431,8 @@ pub trait RootContext: Context {
423431
///
424432
/// struct MyRootContext;
425433
///
434+
/// #[derive(serde::Deserialize)]
435+
/// #[derive(Debug)]
426436
/// struct MyPluginConfiguration {
427437
/// /// Some key
428438
/// pub key: String,
@@ -439,6 +449,7 @@ pub trait RootContext: Context {
439449
/// true
440450
/// }
441451
/// }
452+
/// ```
442453
fn on_configure(&mut self, _plugin_configuration_size: usize) -> bool {
443454
true
444455
}
@@ -456,9 +467,11 @@ pub trait RootContext: Context {
456467
///
457468
/// struct MyRootContext;
458469
///
470+
/// #[derive(serde::Deserialize)]
471+
/// #[derive(Debug)]
459472
/// struct MyPluginConfiguration {
460-
/// /// Some key
461-
/// pub key: String,
473+
/// /// Some key
474+
/// pub key: String,
462475
/// }
463476
///
464477
/// impl RootContext for MyRootContext {
@@ -472,6 +485,7 @@ pub trait RootContext: Context {
472485
/// true
473486
/// }
474487
/// }
488+
/// ```
475489
fn get_plugin_configuration(&self) -> Option<Bytes> {
476490
hostcalls::get_buffer(BufferType::PluginConfiguration, 0, usize::MAX).unwrap()
477491
}
@@ -503,6 +517,7 @@ pub trait RootContext: Context {
503517
/// info!("tick!")
504518
/// }
505519
/// }
520+
/// ```
506521
fn set_tick_period(&self, period: Duration) {
507522
hostcalls::set_tick_period(period).unwrap()
508523
}
@@ -532,6 +547,7 @@ pub trait RootContext: Context {
532547
/// info!("tick!")
533548
/// }
534549
/// }
550+
/// ```
535551
fn on_tick(&mut self) {}
536552

537553
fn on_queue_ready(&mut self, _queue_id: u32) {}
@@ -631,13 +647,16 @@ pub trait HttpContext: Context {
631647
/// ```rust
632648
/// use proxy_wasm::traits::*;
633649
/// use proxy_wasm::types::*;
650+
/// use log::debug;
634651
///
635-
/// use log::info;
652+
/// struct MyPlugin;
636653
///
637654
/// impl HttpContext for MyPlugin {
638655
/// fn on_http_request_headers(&mut self, num_headers: usize, end_of_stream: bool) -> Action {
639656
/// let headers = self.get_http_request_headers();
640657
///
658+
/// debug!("Received request headers: {:?}", headers);
659+
///
641660
/// // Process the request
642661
///
643662
/// Action::Continue
@@ -657,20 +676,24 @@ pub trait HttpContext: Context {
657676
/// # Example
658677
///
659678
/// ```rust
660-
/// use log::info;
661-
/// use proxy_wasm::traits::HttpContext;
679+
/// use proxy_wasm::traits::*;
680+
/// use proxy_wasm::types::*;
681+
/// use log::debug;
682+
///
683+
/// struct MyPlugin;
662684
///
663685
/// impl HttpContext for MyPlugin {
664-
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
686+
/// fn on_http_request_headers(&mut self, num_headers: usize, end_of_stream: bool) -> Action {
665687
/// let headers = self.get_http_request_headers();
666688
///
667-
/// for (name, value) in headers {
668-
/// info!("{}: {}", name, value);
669-
/// }
689+
/// debug!("Received request headers: {:?}", headers);
690+
///
691+
/// // Process the request
692+
///
670693
/// Action::Continue
671-
/// }
694+
/// }
672695
/// }
673-
///
696+
/// ```
674697
fn get_http_request_headers(&self) -> Vec<(String, String)> {
675698
hostcalls::get_map(MapType::HttpRequestHeaders).unwrap()
676699
}
@@ -700,21 +723,24 @@ pub trait HttpContext: Context {
700723
/// # Example
701724
///
702725
/// ```rust
703-
/// use log::info;
726+
/// use log::debug;
704727
/// use proxy_wasm::traits:*;
705728
/// use proxy_wasm::types::Action;
706729
///
730+
/// struct MyPlugin;
731+
///
707732
/// impl HttpContext for MyPlugin {
708733
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
709734
/// let header = self.get_http_request_header(":path");
710735
///
711736
/// match header {
712-
/// Some(value) => info!("The path is: {}", value),
713-
/// None => info!("The path is missing")
737+
/// Some(value) => debug!("The path is: {}", value),
738+
/// None => debug!("The path is missing")
714739
/// }
715740
/// Action::Continue
716741
/// }
717742
/// }
743+
/// ```
718744
fn get_http_request_header(&self, name: &str) -> Option<String> {
719745
hostcalls::get_map_value(MapType::HttpRequestHeaders, name).unwrap()
720746
}
@@ -744,6 +770,8 @@ pub trait HttpContext: Context {
744770
/// use proxy_wasm::traits::*;
745771
/// use proxy_wasm::types::Action;
746772
///
773+
/// struct MyPlugin;
774+
///
747775
/// impl HttpContext for MyPlugin {
748776
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
749777
/// self.add_http_request_header("x-my-header", "my-value");
@@ -950,9 +978,11 @@ pub trait HttpContext: Context {
950978
/// use proxy_wasm::traits::*;
951979
/// use proxy_wasm::types::*;
952980
///
953-
/// impl HttpContext for MyHttpContext {
981+
/// struct MyPlugin;
982+
///
983+
/// impl HttpContext for MyPlugin {
954984
/// fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action {
955-
/// let auth = self.get_http_request_header("Authorization").unwrap_or_defauklt();
985+
/// let auth = self.get_http_request_header("Authorization").unwrap_or_default();
956986
///
957987
/// if auth == "I am authorized!" {
958988
/// // Send an HTTP response with a status code of 200 and a body of "Hello, World!"
@@ -965,6 +995,7 @@ pub trait HttpContext: Context {
965995
/// Action::Pause
966996
/// }
967997
/// }
998+
/// ```
968999
fn send_http_response(
9691000
&self,
9701001
status_code: u32,

0 commit comments

Comments
 (0)