Skip to main content

type_bridge/remote/
deprecated.rs

1//! The 2.2 proxy names, kept until 3.0. Each builds its 2.3 replacement and
2//! adds no behaviour.
3
4use std::future::Future;
5use std::pin::Pin;
6
7use type_bridge_orm::{
8    DEFAULT_PROXY_QUERY_STATEMENTS, DEFAULT_PROXY_QUERY_TIMEOUT_MILLISECONDS,
9    QueryExecutionResourceLimits,
10};
11
12use super::{ProxyConnectionPolicy, ProxyDatabase, ProxyExchange};
13use crate::Result;
14use crate::schema::Unbound;
15
16/// The 2.2 name of [`ProxyDatabase`].
17#[deprecated(since = "2.3.0", note = "use ProxyDatabase; removed in 3.0")]
18pub type RemoteDatabase<S = Unbound> = ProxyDatabase<S>;
19
20/// The 2.2 name of [`ProxyExchange`]. Every implementation is a
21/// [`ProxyExchange`].
22#[deprecated(since = "2.3.0", note = "use ProxyExchange; removed in 3.0")]
23pub trait RemoteQueryTransport: Send + Sync + 'static {
24    /// Fetch the executor's exact signed capability advertisement.
25    fn capabilities(&self) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + '_>>;
26
27    /// Exchange one exact canonical request for one exact signed reply.
28    fn exchange<'a>(
29        &'a self,
30        request: &'a [u8],
31    ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'a>>;
32}
33
34#[allow(deprecated)]
35impl<T: RemoteQueryTransport> ProxyExchange for T {
36    fn capabilities(&self) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + '_>> {
37        RemoteQueryTransport::capabilities(self)
38    }
39
40    fn exchange<'a>(
41        &'a self,
42        request: &'a [u8],
43    ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'a>> {
44        RemoteQueryTransport::exchange(self, request)
45    }
46}
47
48/// The 2.2 constructors of [`ProxyConnectionPolicy`]. Both return a
49/// [`ProxyConnectionPolicy`]; the 2.2 type name itself is not kept, since
50/// `ProxyConnectionPolicy::new` now takes an endpoint.
51#[deprecated(since = "2.3.0", note = "use ProxyConnectionPolicy; removed in 3.0")]
52#[derive(Debug)]
53pub enum RemoteConnectionOptions {}
54
55#[allow(deprecated)]
56impl RemoteConnectionOptions {
57    /// `ProxyConnectionPolicy::with_exchange(transport).answer_limits(limits)
58    /// .authority(scope, semantic_profile)`.
59    #[deprecated(
60        since = "2.3.0",
61        note = "use ProxyConnectionPolicy::with_exchange(transport).answer_limits(limits)\
62                .authority(scope, semantic_profile); removed in 3.0"
63    )]
64    #[must_use]
65    // The 2.2 constructor name; it now returns the replacement type.
66    #[allow(clippy::new_ret_no_self)]
67    pub fn new(
68        scope: impl Into<String>,
69        semantic_profile: impl Into<String>,
70        limits: impl Into<QueryExecutionResourceLimits>,
71        transport: impl ProxyExchange,
72    ) -> ProxyConnectionPolicy {
73        Self::generated(limits, transport).authority(scope, semantic_profile)
74    }
75
76    /// `ProxyConnectionPolicy::with_exchange(transport).answer_limits(limits)`.
77    #[deprecated(
78        since = "2.3.0",
79        note = "use ProxyConnectionPolicy::with_exchange(transport).answer_limits(limits); \
80                removed in 3.0"
81    )]
82    #[must_use]
83    pub fn generated(
84        limits: impl Into<QueryExecutionResourceLimits>,
85        transport: impl ProxyExchange,
86    ) -> ProxyConnectionPolicy {
87        ProxyConnectionPolicy::with_exchange(transport).answer_limits(limits.into())
88    }
89}
90
91/// The 2.2 answer budget: [`QueryExecutionResourceLimits`] built from the
92/// 2.2 constructor arguments, with the proxy-default timeout and statements.
93#[deprecated(
94    since = "2.3.0",
95    note = "use QueryExecutionResourceLimits; removed in 3.0"
96)]
97#[derive(Clone, Copy, Debug, Eq, PartialEq)]
98pub struct RemoteQueryLimits {
99    resources: QueryExecutionResourceLimits,
100}
101
102#[allow(deprecated)]
103impl RemoteQueryLimits {
104    /// Construct the limits with the matching
105    /// [`QueryExecutionResourceLimits`] fields.
106    #[must_use]
107    pub const fn new(
108        max_items: u64,
109        max_bytes: u64,
110        max_collection_members: u64,
111        max_graph_nodes: u64,
112        max_attribute_values: u64,
113        max_role_players: u64,
114    ) -> Self {
115        Self {
116            resources: QueryExecutionResourceLimits {
117                timeout_milliseconds: DEFAULT_PROXY_QUERY_TIMEOUT_MILLISECONDS,
118                items: max_items,
119                bytes: max_bytes,
120                graph_nodes: max_graph_nodes,
121                attribute_values: max_attribute_values,
122                collection_members: max_collection_members,
123                role_players: max_role_players,
124                statements: DEFAULT_PROXY_QUERY_STATEMENTS,
125            },
126        }
127    }
128
129    /// Set `timeout_milliseconds`.
130    #[must_use]
131    pub const fn deadline_ms(mut self, deadline_ms: u64) -> Self {
132        self.resources.timeout_milliseconds = deadline_ms;
133        self
134    }
135}
136
137#[allow(deprecated)]
138impl From<RemoteQueryLimits> for QueryExecutionResourceLimits {
139    fn from(limits: RemoteQueryLimits) -> Self {
140        limits.resources
141    }
142}
143
144#[allow(deprecated)]
145impl From<QueryExecutionResourceLimits> for RemoteQueryLimits {
146    fn from(resources: QueryExecutionResourceLimits) -> Self {
147        Self { resources }
148    }
149}
150
151#[cfg(test)]
152#[allow(deprecated)]
153mod tests {
154    use super::*;
155
156    struct Offline;
157
158    impl RemoteQueryTransport for Offline {
159        fn capabilities(&self) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + '_>> {
160            Box::pin(async { Ok(Vec::new()) })
161        }
162
163        fn exchange<'a>(
164            &'a self,
165            _request: &'a [u8],
166        ) -> Pin<Box<dyn Future<Output = Result<Vec<u8>>> + Send + 'a>> {
167            Box::pin(async { Ok(Vec::new()) })
168        }
169    }
170
171    fn fields(
172        policy: &ProxyConnectionPolicy,
173    ) -> (
174        Option<&str>,
175        Option<&str>,
176        QueryExecutionResourceLimits,
177        QueryExecutionResourceLimits,
178        bool,
179    ) {
180        (
181            policy.scope.as_deref(),
182            policy.semantic_profile.as_deref(),
183            policy.connection_limits,
184            policy.answer_limits,
185            policy.advertisement.is_some(),
186        )
187    }
188
189    #[test]
190    fn remote_connection_options_build_the_matching_policy() {
191        let limits =
192            RemoteQueryLimits::new(100, 8 << 20, 1_000, 1_000, 1_000, 1_000).deadline_ms(30_000);
193        let resources = QueryExecutionResourceLimits::from(limits);
194        assert_eq!(
195            resources,
196            QueryExecutionResourceLimits {
197                timeout_milliseconds: 30_000,
198                items: 100,
199                bytes: 8 << 20,
200                graph_nodes: 1_000,
201                attribute_values: 1_000,
202                collection_members: 1_000,
203                role_players: 1_000,
204                statements: DEFAULT_PROXY_QUERY_STATEMENTS,
205            }
206        );
207
208        let generated = RemoteConnectionOptions::generated(limits, Offline);
209        let expected = ProxyConnectionPolicy::with_exchange(Offline).answer_limits(resources);
210        assert_eq!(fields(&generated), fields(&expected));
211        assert!(matches!(
212            generated.route,
213            super::super::ProxyRoute::Exchange(_)
214        ));
215
216        let scoped = RemoteConnectionOptions::new("scope", "profile", limits, Offline);
217        let expected = ProxyConnectionPolicy::with_exchange(Offline)
218            .answer_limits(resources)
219            .authority("scope", "profile");
220        assert_eq!(fields(&scoped), fields(&expected));
221        assert_eq!(fields(&scoped).0, Some("scope"));
222        assert_eq!(fields(&scoped).1, Some("profile"));
223    }
224}