golem_wasm/
poem.rs

1// Copyright 2024-2025 Golem Cloud
2//
3// Licensed under the Golem Source License v1.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://license.golem.cloud/LICENSE
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::analysis::AnalysedType;
16use crate::ValueAndType;
17use poem_openapi::registry::{MetaSchema, MetaSchemaRef, Registry};
18use poem_openapi::types::{IsObjectType, ParseFromJSON, ParseResult, ToJSON, Type};
19use serde_json::Value;
20use std::borrow::Cow;
21
22impl Type for ValueAndType {
23    const IS_REQUIRED: bool = true;
24    type RawValueType = Self;
25    type RawElementValueType = Self;
26
27    fn name() -> Cow<'static, str> {
28        "ValueAndType".into()
29    }
30
31    fn schema_ref() -> MetaSchemaRef {
32        MetaSchemaRef::Reference(Self::name().into_owned())
33    }
34
35    fn register(registry: &mut Registry) {
36        registry.create_schema::<Self, _>(Self::name().into_owned(), |registry| {
37            AnalysedType::register(registry);
38            serde_json::Value::register(registry);
39            let mut schema = MetaSchema::new("object");
40            schema.required = vec!["typ", "value"];
41            schema.properties = vec![
42                ("typ", AnalysedType::schema_ref()),
43                ("value", Value::schema_ref()),
44            ];
45
46            schema
47        });
48    }
49
50    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
51        Some(self)
52    }
53
54    fn raw_element_iter<'a>(
55        &'a self,
56    ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
57        Box::new(self.as_raw_value().into_iter())
58    }
59}
60
61impl ToJSON for ValueAndType {
62    fn to_json(&self) -> Option<Value> {
63        serde_json::to_value(self).ok()
64    }
65}
66
67impl ParseFromJSON for ValueAndType {
68    fn parse_from_json(value: Option<Value>) -> ParseResult<Self> {
69        Ok(serde_json::from_value(value.unwrap_or_default())?)
70    }
71}
72
73impl IsObjectType for ValueAndType {}
74
75#[cfg(feature = "host")]
76mod json {
77    use crate::analysis::AnalysedType;
78    use crate::json::OptionallyValueAndTypeJson;
79    use poem_openapi::registry::{MetaSchema, MetaSchemaRef, Registry};
80    use poem_openapi::types::{IsObjectType, ParseFromJSON, ParseResult, ToJSON, Type};
81    use serde_json::Value;
82    use std::borrow::Cow;
83
84    impl Type for OptionallyValueAndTypeJson {
85        const IS_REQUIRED: bool = true;
86        type RawValueType = Self;
87        type RawElementValueType = Self;
88
89        fn name() -> Cow<'static, str> {
90            "ValueAndOptionalType".into()
91        }
92
93        fn schema_ref() -> MetaSchemaRef {
94            MetaSchemaRef::Reference(Self::name().into_owned())
95        }
96
97        fn register(registry: &mut Registry) {
98            registry.create_schema::<Self, _>(Self::name().into_owned(), |registry| {
99                AnalysedType::register(registry);
100                serde_json::Value::register(registry);
101                let mut schema = MetaSchema::new("object");
102                schema.required = vec!["value"];
103                schema.properties = vec![
104                    ("typ", AnalysedType::schema_ref()),
105                    ("value", Value::schema_ref()),
106                ];
107
108                schema
109            });
110        }
111
112        fn as_raw_value(&self) -> Option<&Self::RawValueType> {
113            Some(self)
114        }
115
116        fn raw_element_iter<'a>(
117            &'a self,
118        ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
119            Box::new(self.as_raw_value().into_iter())
120        }
121    }
122
123    impl ToJSON for OptionallyValueAndTypeJson {
124        fn to_json(&self) -> Option<Value> {
125            serde_json::to_value(self).ok()
126        }
127    }
128
129    impl ParseFromJSON for OptionallyValueAndTypeJson {
130        fn parse_from_json(value: Option<Value>) -> ParseResult<Self> {
131            Ok(serde_json::from_value(value.unwrap_or_default())?)
132        }
133    }
134
135    impl IsObjectType for OptionallyValueAndTypeJson {}
136}