async_graphql/types/external/
secrecy.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
56
57
58
59
60
61
62
63
64
65
use std::borrow::Cow;

use secrecy::{zeroize::Zeroize, ExposeSecret, SecretBox, SecretString};

use crate::{registry, InputType, InputValueError, InputValueResult, Value};

impl<T: InputType + Zeroize> InputType for SecretBox<T> {
    type RawValueType = T::RawValueType;

    fn type_name() -> Cow<'static, str> {
        T::type_name()
    }

    fn qualified_type_name() -> String {
        T::qualified_type_name()
    }

    fn create_type_info(registry: &mut registry::Registry) -> String {
        T::create_type_info(registry)
    }

    fn parse(value: Option<Value>) -> InputValueResult<Self> {
        T::parse(value)
            .map(|value| SecretBox::new(Box::new(value)))
            .map_err(InputValueError::propagate)
    }

    fn to_value(&self) -> Value {
        Value::Null
    }

    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
        self.expose_secret().as_raw_value()
    }
}

impl InputType for SecretString {
    type RawValueType = str;

    fn type_name() -> Cow<'static, str> {
        String::type_name()
    }

    fn qualified_type_name() -> String {
        String::qualified_type_name()
    }

    fn create_type_info(registry: &mut registry::Registry) -> String {
        String::create_type_info(registry)
    }

    fn parse(value: Option<Value>) -> InputValueResult<Self> {
        String::parse(value)
            .map(SecretString::from)
            .map_err(InputValueError::propagate)
    }

    fn to_value(&self) -> Value {
        Value::Null
    }

    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
        Some(self.expose_secret())
    }
}