aws_smithy_schema/schema/
prelude.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Prelude schemas for built-in Smithy types.
7//!
8//! This module provides const schemas for Smithy's prelude types,
9//! which are the fundamental types available in all Smithy models.
10
11use std::sync::LazyLock;
12
13use crate::{Schema, ShapeId, ShapeType, TraitMap};
14
15/// A simple schema implementation for prelude types.
16#[derive(Debug)]
17pub struct PreludeSchema {
18    id: ShapeId,
19    shape_type: ShapeType,
20}
21
22impl PreludeSchema {
23    /// Creates a new prelude schema.
24    pub const fn new(id: ShapeId, shape_type: ShapeType) -> Self {
25        Self { id, shape_type }
26    }
27}
28
29impl Schema for PreludeSchema {
30    fn shape_id(&self) -> &ShapeId {
31        &self.id
32    }
33
34    fn shape_type(&self) -> ShapeType {
35        self.shape_type
36    }
37
38    fn traits(&self) -> &TraitMap {
39        static MAP: LazyLock<TraitMap> = LazyLock::new(TraitMap::empty);
40
41        &MAP
42    }
43}
44
45// TODO(schema): We should probably test with these as `pub static` too since that could
46// theoretically cut down on binary size (at the expense of some runtime performance)
47
48/// Schema for `smithy.api#String`
49pub const STRING: PreludeSchema = PreludeSchema::new(
50    ShapeId::from_static("smithy.api#String", "smithy.api", "String"),
51    ShapeType::String,
52);
53
54/// Schema for `smithy.api#Boolean`
55pub const BOOLEAN: PreludeSchema = PreludeSchema::new(
56    ShapeId::from_static("smithy.api#Boolean", "smithy.api", "Boolean"),
57    ShapeType::Boolean,
58);
59
60/// Schema for `smithy.api#Byte`
61pub const BYTE: PreludeSchema = PreludeSchema::new(
62    ShapeId::from_static("smithy.api#Byte", "smithy.api", "Byte"),
63    ShapeType::Byte,
64);
65
66/// Schema for `smithy.api#Short`
67pub const SHORT: PreludeSchema = PreludeSchema::new(
68    ShapeId::from_static("smithy.api#Short", "smithy.api", "Short"),
69    ShapeType::Short,
70);
71
72/// Schema for `smithy.api#Integer`
73pub const INTEGER: PreludeSchema = PreludeSchema::new(
74    ShapeId::from_static("smithy.api#Integer", "smithy.api", "Integer"),
75    ShapeType::Integer,
76);
77
78/// Schema for `smithy.api#Long`
79pub const LONG: PreludeSchema = PreludeSchema::new(
80    ShapeId::from_static("smithy.api#Long", "smithy.api", "Long"),
81    ShapeType::Long,
82);
83
84/// Schema for `smithy.api#Float`
85pub const FLOAT: PreludeSchema = PreludeSchema::new(
86    ShapeId::from_static("smithy.api#Float", "smithy.api", "Float"),
87    ShapeType::Float,
88);
89
90/// Schema for `smithy.api#Double`
91pub const DOUBLE: PreludeSchema = PreludeSchema::new(
92    ShapeId::from_static("smithy.api#Double", "smithy.api", "Double"),
93    ShapeType::Double,
94);
95
96/// Schema for `smithy.api#BigInteger`
97pub const BIG_INTEGER: PreludeSchema = PreludeSchema::new(
98    ShapeId::from_static("smithy.api#BigInteger", "smithy.api", "BigInteger"),
99    ShapeType::BigInteger,
100);
101
102/// Schema for `smithy.api#BigDecimal`
103pub const BIG_DECIMAL: PreludeSchema = PreludeSchema::new(
104    ShapeId::from_static("smithy.api#BigDecimal", "smithy.api", "BigDecimal"),
105    ShapeType::BigDecimal,
106);
107
108/// Schema for `smithy.api#Blob`
109pub const BLOB: PreludeSchema = PreludeSchema::new(
110    ShapeId::from_static("smithy.api#Blob", "smithy.api", "Blob"),
111    ShapeType::Blob,
112);
113
114/// Schema for `smithy.api#Timestamp`
115pub const TIMESTAMP: PreludeSchema = PreludeSchema::new(
116    ShapeId::from_static("smithy.api#Timestamp", "smithy.api", "Timestamp"),
117    ShapeType::Timestamp,
118);
119
120/// Schema for `smithy.api#Document`
121pub const DOCUMENT: PreludeSchema = PreludeSchema::new(
122    ShapeId::from_static("smithy.api#Document", "smithy.api", "Document"),
123    ShapeType::Document,
124);
125
126#[cfg(test)]
127mod tests {
128    use super::*;
129    use crate::SchemaExt;
130
131    #[test]
132    fn test_string_schema() {
133        assert_eq!(STRING.shape_id().as_str(), "smithy.api#String");
134        assert_eq!(STRING.shape_type(), ShapeType::String);
135        assert!(STRING.is_string());
136        assert!(STRING.traits().is_empty());
137    }
138
139    #[test]
140    fn test_boolean_schema() {
141        assert_eq!(BOOLEAN.shape_id().as_str(), "smithy.api#Boolean");
142        assert_eq!(BOOLEAN.shape_type(), ShapeType::Boolean);
143        assert!(BOOLEAN.traits().is_empty());
144    }
145
146    #[test]
147    fn test_numeric_schemas() {
148        assert_eq!(BYTE.shape_type(), ShapeType::Byte);
149        assert_eq!(SHORT.shape_type(), ShapeType::Short);
150        assert_eq!(INTEGER.shape_type(), ShapeType::Integer);
151        assert_eq!(LONG.shape_type(), ShapeType::Long);
152        assert_eq!(FLOAT.shape_type(), ShapeType::Float);
153        assert_eq!(DOUBLE.shape_type(), ShapeType::Double);
154        assert_eq!(BIG_INTEGER.shape_type(), ShapeType::BigInteger);
155        assert_eq!(BIG_DECIMAL.shape_type(), ShapeType::BigDecimal);
156    }
157
158    #[test]
159    fn test_blob_schema() {
160        assert_eq!(BLOB.shape_id().as_str(), "smithy.api#Blob");
161        assert_eq!(BLOB.shape_type(), ShapeType::Blob);
162        assert!(BLOB.is_blob());
163    }
164
165    #[test]
166    fn test_timestamp_schema() {
167        assert_eq!(TIMESTAMP.shape_id().as_str(), "smithy.api#Timestamp");
168        assert_eq!(TIMESTAMP.shape_type(), ShapeType::Timestamp);
169    }
170
171    #[test]
172    fn test_document_schema() {
173        assert_eq!(DOCUMENT.shape_id().as_str(), "smithy.api#Document");
174        assert_eq!(DOCUMENT.shape_type(), ShapeType::Document);
175    }
176
177    #[test]
178    fn test_all_prelude_types_are_simple() {
179        assert!(STRING.shape_type().is_simple());
180        assert!(BOOLEAN.shape_type().is_simple());
181        assert!(BYTE.shape_type().is_simple());
182        assert!(SHORT.shape_type().is_simple());
183        assert!(INTEGER.shape_type().is_simple());
184        assert!(LONG.shape_type().is_simple());
185        assert!(FLOAT.shape_type().is_simple());
186        assert!(DOUBLE.shape_type().is_simple());
187        assert!(BIG_INTEGER.shape_type().is_simple());
188        assert!(BIG_DECIMAL.shape_type().is_simple());
189        assert!(BLOB.shape_type().is_simple());
190        assert!(TIMESTAMP.shape_type().is_simple());
191        assert!(DOCUMENT.shape_type().is_simple());
192    }
193}