authly_common/
lib.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
45
46
47
48
49
50
51
52
53
54
55
//! `authly-common` defines common types and algorithms used in the authly ecosystem.

#![forbid(unsafe_code)]
#![warn(missing_docs)]
#![cfg_attr(feature = "unstable-doc-cfg", feature(doc_auto_cfg))]

use std::{fmt::Display, marker::PhantomData, str::FromStr};

use serde::de::{Error, Visitor};

pub mod id;
pub mod property;
pub mod proto;
pub mod service;

#[cfg(feature = "access_token")]
pub mod access_token;

#[cfg(feature = "document")]
pub mod document;

#[cfg(feature = "mtls_server")]
pub mod mtls_server;

pub mod policy;

#[derive(Default)]
struct FromStrVisitor<T> {
    expecting: &'static str,
    phantom: PhantomData<T>,
}

impl<T> FromStrVisitor<T> {
    pub fn new(expecting: &'static str) -> Self {
        Self {
            expecting,
            phantom: PhantomData,
        }
    }
}

impl<T: FromStr> Visitor<'_> for FromStrVisitor<T>
where
    T::Err: Display,
{
    type Value = T;

    fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.expecting)
    }

    fn visit_str<E: Error>(self, str: &str) -> Result<Self::Value, E> {
        T::from_str(str).map_err(|msg| E::custom(msg))
    }
}