Skip to main content

aws_smithy_schema/schema/
trait_type.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use crate::ShapeId;
7use std::any::Any;
8use std::fmt;
9
10/// Trait representing a Smithy trait at runtime.
11///
12/// Traits provide additional metadata about shapes that affect serialization,
13/// validation, and other behaviors.
14pub trait Trait: Any + Send + Sync + fmt::Debug {
15    /// Returns the Shape ID of this trait.
16    fn trait_id(&self) -> &ShapeId;
17
18    /// Returns this trait as `&dyn Any` for downcasting.
19    fn as_any(&self) -> &dyn Any;
20}
21
22/// An annotation trait (no value), e.g. `@sensitive`, `@sparse`, `@httpPayload`.
23#[derive(Debug, Clone)]
24#[allow(dead_code)] // Will be used by generated code
25pub struct AnnotationTrait {
26    id: ShapeId,
27}
28
29#[allow(dead_code)]
30impl AnnotationTrait {
31    /// Creates a new annotation trait.
32    pub fn new(id: ShapeId) -> Self {
33        Self { id }
34    }
35}
36
37impl Trait for AnnotationTrait {
38    fn trait_id(&self) -> &ShapeId {
39        &self.id
40    }
41
42    fn as_any(&self) -> &dyn Any {
43        self
44    }
45}
46
47/// A trait with a string value, e.g. `@jsonName("foo")`, `@xmlName("bar")`.
48#[derive(Debug, Clone)]
49#[allow(dead_code)] // Will be used by generated code
50pub struct StringTrait {
51    id: ShapeId,
52    value: String,
53}
54
55#[allow(dead_code)]
56impl StringTrait {
57    /// Creates a new string-valued trait.
58    pub fn new(id: ShapeId, value: impl Into<String>) -> Self {
59        Self {
60            id,
61            value: value.into(),
62        }
63    }
64
65    /// Returns the string value of this trait.
66    pub fn value(&self) -> &str {
67        &self.value
68    }
69}
70
71impl Trait for StringTrait {
72    fn trait_id(&self) -> &ShapeId {
73        &self.id
74    }
75
76    fn as_any(&self) -> &dyn Any {
77        self
78    }
79}
80
81/// A trait with a Document value, used for unknown/custom traits.
82///
83/// When a trait is included in a schema but has no typed Rust representation,
84/// its value is stored as a [`Document`](aws_smithy_types::Document).
85#[derive(Debug, Clone)]
86#[allow(dead_code)] // Will be used by generated code
87pub struct DocumentTrait {
88    id: ShapeId,
89    value: aws_smithy_types::Document,
90}
91
92#[allow(dead_code)]
93impl DocumentTrait {
94    /// Creates a new document-valued trait.
95    pub fn new(id: ShapeId, value: aws_smithy_types::Document) -> Self {
96        Self { id, value }
97    }
98
99    /// Returns the document value of this trait.
100    pub fn value(&self) -> &aws_smithy_types::Document {
101        &self.value
102    }
103}
104
105impl Trait for DocumentTrait {
106    fn trait_id(&self) -> &ShapeId {
107        &self.id
108    }
109
110    fn as_any(&self) -> &dyn Any {
111        self
112    }
113}