|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +use crate::Number; |
| 7 | +use std::collections::HashMap; |
| 8 | + |
| 9 | +/* ANCHOR: document */ |
| 10 | + |
| 11 | +/// Document Type |
| 12 | +/// |
| 13 | +/// Document types represents protocol-agnostic open content that is accessed like JSON data. |
| 14 | +/// Open content is useful for modeling unstructured data that has no schema, data that can't be |
| 15 | +/// modeled using rigid types, or data that has a schema that evolves outside of the purview of a model. |
| 16 | +/// The serialization format of a document is an implementation detail of a protocol. |
| 17 | +#[derive(Debug, Clone, PartialEq)] |
| 18 | +pub enum Document { |
| 19 | + /// JSON object |
| 20 | + Object(HashMap<String, Document>), |
| 21 | + /// JSON array |
| 22 | + Array(Vec<Document>), |
| 23 | + /// JSON number |
| 24 | + Number(Number), |
| 25 | + /// JSON string |
| 26 | + String(String), |
| 27 | + /// JSON boolean |
| 28 | + Bool(bool), |
| 29 | + /// JSON null |
| 30 | + Null, |
| 31 | +} |
| 32 | + |
| 33 | +impl From<bool> for Document { |
| 34 | + fn from(value: bool) -> Self { |
| 35 | + Document::Bool(value) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl From<String> for Document { |
| 40 | + fn from(value: String) -> Self { |
| 41 | + Document::String(value) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl From<Vec<Document>> for Document { |
| 46 | + fn from(values: Vec<Document>) -> Self { |
| 47 | + Document::Array(values) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl From<HashMap<String, Document>> for Document { |
| 52 | + fn from(values: HashMap<String, Document>) -> Self { |
| 53 | + Document::Object(values) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl From<u64> for Document { |
| 58 | + fn from(value: u64) -> Self { |
| 59 | + Document::Number(Number::PosInt(value)) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl From<i64> for Document { |
| 64 | + fn from(value: i64) -> Self { |
| 65 | + Document::Number(Number::NegInt(value)) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +impl From<i32> for Document { |
| 70 | + fn from(value: i32) -> Self { |
| 71 | + Document::Number(Number::NegInt(value as i64)) |
| 72 | + } |
| 73 | +} |
0 commit comments