poem_openapi/types/external/
char.rs1use std::borrow::Cow;
2
3use serde_json::Value;
4
5use crate::{
6 registry::{MetaSchema, MetaSchemaRef},
7 types::{ParseError, ParseFromJSON, ParseFromParameter, ParseResult, ToJSON, Type},
8};
9
10impl Type for char {
11 const IS_REQUIRED: bool = true;
12
13 type RawValueType = Self;
14
15 type RawElementValueType = Self;
16
17 fn name() -> Cow<'static, str> {
18 "char".into()
19 }
20
21 fn schema_ref() -> MetaSchemaRef {
22 MetaSchemaRef::Inline(Box::new(MetaSchema::new("char")))
23 }
24
25 fn as_raw_value(&self) -> Option<&Self::RawValueType> {
26 Some(self)
27 }
28
29 fn raw_element_iter<'a>(
30 &'a self,
31 ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
32 Box::new(self.as_raw_value().into_iter())
33 }
34}
35
36impl ParseFromJSON for char {
37 fn parse_from_json(value: Option<Value>) -> ParseResult<Self> {
38 let value = value.unwrap_or_default();
39 if let Value::String(value) = value {
40 match value.chars().next() {
41 Some(ch) => Ok(ch),
42 None => Err(ParseError::expected_input()),
43 }
44 } else {
45 Err(ParseError::expected_type(value))
46 }
47 }
48}
49
50impl ParseFromParameter for char {
51 fn parse_from_parameter(value: &str) -> ParseResult<Self> {
52 value.parse().map_err(ParseError::custom)
53 }
54}
55
56impl ToJSON for char {
57 fn to_json(&self) -> Option<Value> {
58 Some(Value::String(self.to_string()))
59 }
60}