| 1 | #![cfg(all(target_arch = "wasm32", target_os = "wasi", feature = "resolve-async"))] |
| 2 | |
| 3 | use futuresrust-analyzer cargo futures 0.3.31 crate/::executorrust-analyzer cargo futures 0.3.31 executor/::block_onrust-analyzer cargo futures-executor 0.3.31 local_pool/block_on().; |
| 4 | use jsonschemarust-analyzer cargo jsonschema 0.37.4 crate/::{async_optionsrust-analyzer cargo jsonschema 0.37.4 async_options()., AsyncRetrieverust-analyzer cargo referencing 0.37.4 retriever/AsyncRetrieve#, Urirust-analyzer cargo fluent-uri 0.4.1 imp/Uri#}; |
| 5 | use serde_jsonrust-analyzer cargo serde_json 1.0.145 crate/::{jsonrust-analyzer cargo serde_json 1.0.145 macros/json!, Valuerust-analyzer cargo serde_json 1.0.145 value/Value#}; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::HashMaprust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/hash/map/HashMap#; |
| 7 | |
| 8 | struct MapAsyncRetriever { |
| 9 | schemas: HashMap<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Value>, |
| 10 | } |
| 11 | |
| 12 | impl MapAsyncRetriever { |
| 13 | fn new(entries: impl IntoIteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#<Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/collect/IntoIterator#Item# = (Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Value)>) -> Self { |
| 14 | Self { |
| 15 | schemas: entries.into_iter().collect(), |
| 16 | } |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | #[async_traitrust-analyzer cargo async-trait 0.1.89 crate/::async_traitrust-analyzer cargo async-trait 0.1.89 async_trait:(?Send)] |
| 21 | impl AsyncRetrieve for MapAsyncRetriever { |
| 22 | async fn retrieve( |
| 23 | &self, |
| 24 | uri: &Uri<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>, |
| 25 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Value, Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# + Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync#>> { |
| 26 | selfrust-analyzer cargo jsonschema 0.37.4 crate/.schemas |
| 27 | .get(uri.as_str()) |
| 28 | .cloned() |
| 29 | .ok_or_else(|| "schema not found".into()) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 34 | fn async_retriever_resolves_refs_on_wasi() { |
| 35 | block_on(async { |
| 36 | let schema = json!({ |
| 37 | "$ref": "https://example.com/user.json" |
| 38 | }); |
| 39 | |
| 40 | let retriever = MapAsyncRetriever::new([( |
| 41 | "https://example.com/user.json".to_string(), |
| 42 | json!({ |
| 43 | "$id": "https://example.com/user.json", |
| 44 | "type": "object", |
| 45 | "properties": { |
| 46 | "name": { "type": "string" }, |
| 47 | "age": { "type": "integer", "minimum": 0 } |
| 48 | }, |
| 49 | "required": ["name"] |
| 50 | }), |
| 51 | )]); |
| 52 | |
| 53 | let validator = async_options() |
| 54 | .with_retriever(retriever) |
| 55 | .build(&schema) |
| 56 | .await |
| 57 | .expect("validator builds on wasm32-wasi"); |
| 58 | |
| 59 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(validator.is_valid(&json!({"name": "Ferris", "age": 13}))); |
| 60 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!validator.is_valid(&json!({"name": 42}))); |
| 61 | }); |
| 62 | } |