Crate rede_placeholders

Crate rede_placeholders 

Source
Expand description

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

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());

Re-exports§

pub use value_picker::ValuePicker;

Modules§

value_picker
Module containing the ValuePicker trait and some pickers provided by the crate. In case that some custom picker is required, the trait can be implemented and used in the Resolver like those provided.

Structs§

PlaceholderValues
Wrapper over the resolved values for the placeholders. You can directly access its internal values or use the convinient methods to ease access to its content.
Placeholders
The Placeholders struct analyzes a request and extracts all the placeholders from its parts.
Renderer
A renderer is responsible for rendering a request using the placeholders and values provided. It should be used with render to replace the placeholders with the values in the request.
Resolver
Uses a list of ValuePicker to resolve the value of Placeholders.