Skip to main content

async_graphql/types/external/
ip.rs

1use std::net::IpAddr;
2
3use crate::{InputValueError, InputValueResult, Scalar, ScalarType, Value};
4
5/// Implement the IP scalar
6///
7/// The input/output is in the respective IP format
8#[Scalar(
9    internal,
10    name = "IpAddr",
11    specified_by_url = "https://en.wikipedia.org/wiki/IP_address"
12)]
13impl ScalarType for IpAddr {
14    fn parse(value: Value) -> InputValueResult<Self> {
15        match &value {
16            Value::String(s) => Ok(s.parse()?),
17            _ => Err(InputValueError::expected_type(value)),
18        }
19    }
20
21    fn to_value(&self) -> Value {
22        Value::String(self.to_string())
23    }
24}