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// Static schemas for prelude types to allow static lifetime references
46
47/// Schema for `smithy.api#String`
48pub static STRING: PreludeSchema = PreludeSchema::new(
49    ShapeId::from_static("smithy.api#String", "smithy.api", "String"),
50    ShapeType::String,
51);
52
53/// Schema for `smithy.api#Boolean`
54pub static BOOLEAN: PreludeSchema = PreludeSchema::new(
55    ShapeId::from_static("smithy.api#Boolean", "smithy.api", "Boolean"),
56    ShapeType::Boolean,
57);
58
59/// Schema for `smithy.api#Byte`
60pub static BYTE: PreludeSchema = PreludeSchema::new(
61    ShapeId::from_static("smithy.api#Byte", "smithy.api", "Byte"),
62    ShapeType::Byte,
63);
64
65/// Schema for `smithy.api#Short`
66pub static SHORT: PreludeSchema = PreludeSchema::new(
67    ShapeId::from_static("smithy.api#Short", "smithy.api", "Short"),
68    ShapeType::Short,
69);
70
71/// Schema for `smithy.api#Integer`
72pub static INTEGER: PreludeSchema = PreludeSchema::new(
73    ShapeId::from_static("smithy.api#Integer", "smithy.api", "Integer"),
74    ShapeType::Integer,
75);
76
77/// Schema for `smithy.api#Long`
78pub static LONG: PreludeSchema = PreludeSchema::new(
79    ShapeId::from_static("smithy.api#Long", "smithy.api", "Long"),
80    ShapeType::Long,
81);
82
83/// Schema for `smithy.api#Float`
84pub static FLOAT: PreludeSchema = PreludeSchema::new(
85    ShapeId::from_static("smithy.api#Float", "smithy.api", "Float"),
86    ShapeType::Float,
87);
88
89/// Schema for `smithy.api#Double`
90pub static DOUBLE: PreludeSchema = PreludeSchema::new(
91    ShapeId::from_static("smithy.api#Double", "smithy.api", "Double"),
92    ShapeType::Double,
93);
94
95/// Schema for `smithy.api#BigInteger`
96pub static BIG_INTEGER: PreludeSchema = PreludeSchema::new(
97    ShapeId::from_static("smithy.api#BigInteger", "smithy.api", "BigInteger"),
98    ShapeType::BigInteger,
99);
100
101/// Schema for `smithy.api#BigDecimal`
102pub static BIG_DECIMAL: PreludeSchema = PreludeSchema::new(
103    ShapeId::from_static("smithy.api#BigDecimal", "smithy.api", "BigDecimal"),
104    ShapeType::BigDecimal,
105);
106
107/// Schema for `smithy.api#Blob`
108pub static BLOB: PreludeSchema = PreludeSchema::new(
109    ShapeId::from_static("smithy.api#Blob", "smithy.api", "Blob"),
110    ShapeType::Blob,
111);
112
113/// Schema for `smithy.api#Timestamp`
114pub static TIMESTAMP: PreludeSchema = PreludeSchema::new(
115    ShapeId::from_static("smithy.api#Timestamp", "smithy.api", "Timestamp"),
116    ShapeType::Timestamp,
117);
118
119/// Schema for `smithy.api#Document`
120pub static DOCUMENT: PreludeSchema = PreludeSchema::new(
121    ShapeId::from_static("smithy.api#Document", "smithy.api", "Document"),
122    ShapeType::Document,
123);
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128    use crate::SchemaExt;
129
130    #[test]
131    fn test_string_schema() {
132        assert_eq!(STRING.shape_id().as_str(), "smithy.api#String");
133        assert_eq!(STRING.shape_type(), ShapeType::String);
134        assert!(STRING.is_string());
135        assert!(STRING.traits().is_empty());
136    }
137
138    #[test]
139    fn test_boolean_schema() {
140        assert_eq!(BOOLEAN.shape_id().as_str(), "smithy.api#Boolean");
141        assert_eq!(BOOLEAN.shape_type(), ShapeType::Boolean);
142        assert!(BOOLEAN.traits().is_empty());
143    }
144
145    #[test]
146    fn test_numeric_schemas() {
147        assert_eq!(BYTE.shape_type(), ShapeType::Byte);
148        assert_eq!(SHORT.shape_type(), ShapeType::Short);
149        assert_eq!(INTEGER.shape_type(), ShapeType::Integer);
150        assert_eq!(LONG.shape_type(), ShapeType::Long);
151        assert_eq!(FLOAT.shape_type(), ShapeType::Float);
152        assert_eq!(DOUBLE.shape_type(), ShapeType::Double);
153        assert_eq!(BIG_INTEGER.shape_type(), ShapeType::BigInteger);
154        assert_eq!(BIG_DECIMAL.shape_type(), ShapeType::BigDecimal);
155    }
156
157    #[test]
158    fn test_blob_schema() {
159        assert_eq!(BLOB.shape_id().as_str(), "smithy.api#Blob");
160        assert_eq!(BLOB.shape_type(), ShapeType::Blob);
161        assert!(BLOB.is_blob());
162    }
163
164    #[test]
165    fn test_timestamp_schema() {
166        assert_eq!(TIMESTAMP.shape_id().as_str(), "smithy.api#Timestamp");
167        assert_eq!(TIMESTAMP.shape_type(), ShapeType::Timestamp);
168    }
169
170    #[test]
171    fn test_document_schema() {
172        assert_eq!(DOCUMENT.shape_id().as_str(), "smithy.api#Document");
173        assert_eq!(DOCUMENT.shape_type(), ShapeType::Document);
174    }
175
176    #[test]
177    fn test_all_prelude_types_are_simple() {
178        assert!(STRING.shape_type().is_simple());
179        assert!(BOOLEAN.shape_type().is_simple());
180        assert!(BYTE.shape_type().is_simple());
181        assert!(SHORT.shape_type().is_simple());
182        assert!(INTEGER.shape_type().is_simple());
183        assert!(LONG.shape_type().is_simple());
184        assert!(FLOAT.shape_type().is_simple());
185        assert!(DOUBLE.shape_type().is_simple());
186        assert!(BIG_INTEGER.shape_type().is_simple());
187        assert!(BIG_DECIMAL.shape_type().is_simple());
188        assert!(BLOB.shape_type().is_simple());
189        assert!(TIMESTAMP.shape_type().is_simple());
190        assert!(DOCUMENT.shape_type().is_simple());
191    }
192}