Skip to main content

systemprompt_extension/
any.rs

1use std::any::Any;
2use std::fmt::Debug;
3
4#[cfg(feature = "web")]
5use crate::typed::ApiExtensionTypedDyn;
6use crate::typed::{
7    ConfigExtensionTyped, JobExtensionTyped, ProviderExtensionTyped, SchemaExtensionTyped,
8};
9use crate::types::ExtensionType;
10
11pub trait AnyExtension: Send + Sync + 'static {
12    fn id(&self) -> &'static str;
13    fn name(&self) -> &'static str;
14    fn version(&self) -> &'static str;
15    fn priority(&self) -> u32;
16
17    fn as_schema(&self) -> Option<&dyn SchemaExtensionTyped> {
18        None
19    }
20    #[cfg(feature = "web")]
21    fn as_api(&self) -> Option<&dyn ApiExtensionTypedDyn> {
22        None
23    }
24    fn as_config(&self) -> Option<&dyn ConfigExtensionTyped> {
25        None
26    }
27    fn as_job(&self) -> Option<&dyn JobExtensionTyped> {
28        None
29    }
30    fn as_provider(&self) -> Option<&dyn ProviderExtensionTyped> {
31        None
32    }
33
34    fn as_any(&self) -> &dyn Any;
35    fn type_name(&self) -> &'static str;
36}
37
38#[derive(Debug)]
39pub struct ExtensionWrapper<T: Debug> {
40    inner: T,
41}
42
43impl<T: ExtensionType + Debug> ExtensionWrapper<T> {
44    #[must_use]
45    pub const fn new(inner: T) -> Self {
46        Self { inner }
47    }
48}
49
50impl<T: ExtensionType + Debug + 'static> AnyExtension for ExtensionWrapper<T> {
51    fn id(&self) -> &'static str {
52        T::ID
53    }
54
55    fn name(&self) -> &'static str {
56        T::NAME
57    }
58
59    fn version(&self) -> &'static str {
60        T::VERSION
61    }
62
63    fn priority(&self) -> u32 {
64        T::PRIORITY
65    }
66
67    fn as_any(&self) -> &dyn Any {
68        &self.inner
69    }
70
71    fn type_name(&self) -> &'static str {
72        std::any::type_name::<T>()
73    }
74}
75
76#[derive(Debug)]
77pub struct SchemaExtensionWrapper<T: Debug> {
78    inner: T,
79}
80
81impl<T: ExtensionType + SchemaExtensionTyped + Debug> SchemaExtensionWrapper<T> {
82    #[must_use]
83    pub const fn new(inner: T) -> Self {
84        Self { inner }
85    }
86}
87
88impl<T: ExtensionType + SchemaExtensionTyped + Debug + 'static> AnyExtension
89    for SchemaExtensionWrapper<T>
90{
91    fn id(&self) -> &'static str {
92        T::ID
93    }
94
95    fn name(&self) -> &'static str {
96        T::NAME
97    }
98
99    fn version(&self) -> &'static str {
100        T::VERSION
101    }
102
103    fn priority(&self) -> u32 {
104        T::PRIORITY
105    }
106
107    fn as_schema(&self) -> Option<&dyn SchemaExtensionTyped> {
108        Some(&self.inner)
109    }
110
111    fn as_any(&self) -> &dyn Any {
112        &self.inner
113    }
114
115    fn type_name(&self) -> &'static str {
116        std::any::type_name::<T>()
117    }
118}
119
120#[cfg(feature = "web")]
121#[derive(Debug)]
122pub struct ApiExtensionWrapper<T: Debug> {
123    inner: T,
124}
125
126#[cfg(feature = "web")]
127impl<T: ExtensionType + ApiExtensionTypedDyn + Debug> ApiExtensionWrapper<T> {
128    #[must_use]
129    pub const fn new(inner: T) -> Self {
130        Self { inner }
131    }
132}
133
134#[cfg(feature = "web")]
135impl<T: ExtensionType + ApiExtensionTypedDyn + Debug + 'static> AnyExtension
136    for ApiExtensionWrapper<T>
137{
138    fn id(&self) -> &'static str {
139        T::ID
140    }
141
142    fn name(&self) -> &'static str {
143        T::NAME
144    }
145
146    fn version(&self) -> &'static str {
147        T::VERSION
148    }
149
150    fn priority(&self) -> u32 {
151        T::PRIORITY
152    }
153
154    fn as_api(&self) -> Option<&dyn ApiExtensionTypedDyn> {
155        Some(&self.inner)
156    }
157
158    fn as_any(&self) -> &dyn Any {
159        &self.inner
160    }
161
162    fn type_name(&self) -> &'static str {
163        std::any::type_name::<T>()
164    }
165}