1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Library to provide functions and structures to work with `rede` placeholders.
//! It's based on an execution flow with three steps:
//! - Extract the request' [`Placeholders`].
//! - Use a [`Resolver`] with different [`ValuePicker`] to identify the values to replace each placeholder.
//! - Pass the placeholders and the resolved values to the [`Renderer`] to create a new request.
//!
//! # Example
//!
//! ```
//! # use std::error::Error;
//! # use crate::rede_placeholders::{Placeholders, Renderer, Resolver, value_picker::{EnvVarPicker, VariablesPicker}};
//! #
//! # fn main() -> Result<(), Box<dyn Error>> {
//! # std::env::set_var("API_TOKEN", "token_from_env_var");
//! let toml = r#"
//! http = { url = "http://localhost:8080/{{api_version}}/example/{{id}}", method = "GET" }
//! query_params = { token = "{{API_TOKEN}}" }
//! variables = { api_version = "v1" }
//! "#;
//! let request = rede_parser::parse_request(toml).unwrap();
//!
//! // find placeholders
//! let placeholders = (&request).into();
//! // identify values, as the resolver uses the variables picker, it should be dropped before the render step
//! let ph_values = {
//! let resolver = Resolver::new()
//! .add_picker(Box::new(EnvVarPicker))
//! .add_picker(Box::new(VariablesPicker::new(&request.variables)));
//! resolver.resolve(&placeholders)
//! };
//! // render new request
//! let renderer = Renderer::new(&placeholders, ph_values);
//! let rendered = renderer.render(request)?;
//!
//! assert_eq!(rendered.url, "http://localhost:8080/v1/example/{{id}}");
//! assert_eq!(rendered.query_params[0].1, "token_from_env_var".to_string());
//! # Ok(())
//! # }
//! ```
pub use Placeholders;
pub use Renderer;
pub use PlaceholderValues;
pub use Resolver;
pub use ValuePicker;