Skip to main content

param

Macro param 

Source
macro_rules! param {
    (Body $val:expr) => { ... };
    (Body, $val:expr) => { ... };
    ($style:ident, $name:expr, ( $( ($($val:expr),* $(,)?) ),+ $(,)? )) => { ... };
    ($style:ident, $name:expr, ($($val:expr),+ $(,)?)) => { ... };
    ($style:ident, $name:expr, vec![ $( vec![ $($val:expr),* $(,)? ] ),* $(,)? ]) => { ... };
    ($style:ident, $name:expr, $val:expr) => { ... };
    ($style:path, $name:expr, ( $( ($($val:expr),* $(,)?) ),+ $(,)? )) => { ... };
    ($style:path, $name:expr, ($($val:expr),+ $(,)?)) => { ... };
    ($style:path, $name:expr, vec![ $( vec![ $($val:expr),* $(,)? ] ),* $(,)? ]) => { ... };
    ($style:path, $name:expr, $val:expr) => { ... };
}
Expand description

Creates a Param with the given style, name, and values.

The macro supports two calling styles:

  • Ident style (shorthand): param!(QueryPair, "q", ...)
    The style is automatically qualified to ParamStyle::QueryPair
  • Path style (explicit): param!(ParamStyle::QueryPair, "q", ...)
    Use when the full path is preferred or when ParamStyle is in scope

§Parameter Styles

Common body type parameters and request styles:

  • Header: HTTP header (e.g., "PRIVATE-TOKEN")
  • Body: Request body payload (e.g., JSON or form data)
  • QueryPair: URL query string parameter
  • FieldList: Comma-separated field list
  • TemplateValue: URI template substitution
  • KeyValuePair: Key-value pair in query or body

§Value Syntaxes

The macro supports four different value syntaxes:

  1. Single value: param!(FieldList, "fl", "family-name")
  2. Single tuple: param!(QueryPair, "filter", ("status", "inactive"))
  3. Multiple tuples: param!(QueryPair, "q", (("key1", "val1"), ("key2", "val2")))
  4. Vec notation: param!(FieldList, "fields", vec![vec!["field1"], vec!["field2"]])
  5. Body shorthand: param!(Body &payload) (uses an empty key for raw curl -d semantics)

§Examples

// Shorthand with single value (ident style)
param!(FieldList, "fl", "family-name")

// Shorthand with single tuple (ident style)
param!(QueryPair, "filter", ("status", "inactive"))

// Shorthand with multiple tuples (ident style)
param!(QueryPair, "q", (
    ("affiliation-org-name", "Lyrasis"),
    ("ror-org-id", "\"https://ror.org/01qz5mb56\""),
))

// Path style (explicit ParamStyle reference)
param!(ParamStyle::FieldList, "fl", "family-name")

// KeyValuePair (query string key-value pair)
param!(KeyValuePair, "per_page", "100")

param!(ParamStyle::KeyValuePair, "page", "2")

// Header parameter
param!(Header, "PRIVATE-TOKEN", &token)

// Body shorthand (raw body, no key)
param!(Body &payload)

// Body parameter (with key name)
param!(Body, "body", &payload)

// Vec notation
param!(QueryPair, "q", vec![
    vec!["affiliation-org-name", "Lyrasis"],
])