eio_okta_api/
query_string.rs

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
use std::ops::Deref;

use iri_string::types::{UriQueryStr, UriQueryString};
use serde::Serialize;

#[derive(Debug, thiserror::Error)]
#[remain::sorted]
pub enum Error {
  #[error("complex: {0}")]
  Complex(#[from] serde_qs::Error),
  #[error("invalid: {0}")]
  Invalid(#[from] iri_string::validate::Error),
  #[error("simple: {0}")]
  Simple(#[from] serde_urlencoded::ser::Error),
  #[error(transparent)]
  Validation(#[from] validator::ValidationErrors),
}

pub struct QueryString(UriQueryString);

impl Deref for QueryString {
  type Target = UriQueryStr;
  fn deref(&self) -> &Self::Target {
    self.0.deref()
  }
}

impl QueryString {
  pub fn simple<T>(input: &T) -> Result<Self, Error>
  where
    T: Serialize,
  {
    let s = serde_urlencoded::to_string(input)?;
    Ok(Self(UriQueryStr::new(&s)?.into()))
  }

  pub fn nested<T>(input: &T) -> Result<Self, Error>
  where
    T: Serialize,
  {
    let s = serde_qs::to_string(input)?;
    Ok(Self(UriQueryStr::new(&s)?.into()))
  }
}