mz_storage_types/connections/
inline.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Contains traits and types to support inlining connection details.
11//!
12//! Because we ultimately want to support the ability to alter the details of a
13//! connection, we cannot simply inline their state into structs--we instead
14//! need a means of understanding which connection we're referring to and inline
15//! its current state when sending the struct off to be used to handle a
16//! connection to an external service.
17
18use std::fmt::Debug;
19use std::hash::Hash;
20
21use mz_repr::CatalogItemId;
22use proptest::prelude::Arbitrary;
23use proptest_derive::Arbitrary;
24use serde::{Deserialize, Serialize};
25
26use crate::AlterCompatible;
27
28use super::Connection;
29
30/// Permits any struct to take a [`CatalogItemId`] into an inlined connection.
31///
32/// It is safe to assume that if this `id` does not refer to a catalog
33/// connection, this function will panic.
34pub trait ConnectionResolver {
35    fn resolve_connection(&self, id: CatalogItemId) -> Connection<InlinedConnection>;
36}
37
38impl<R: ConnectionResolver + ?Sized> ConnectionResolver for &R {
39    fn resolve_connection(&self, id: CatalogItemId) -> Connection<InlinedConnection> {
40        (*self).resolve_connection(id)
41    }
42}
43
44/// Takes ~`T<ReferencedConnection>` to ~`T<InlinedConnection>` by recursively
45/// inlining connections and resolving any referenced connections into their
46/// inlined version.
47///
48/// Note that this trait is overly generic.
49// TODO: this trait could be derived for types that are generic over a type that
50// implements `ConnectionAccess`, e.g. `derive(IntoInlineConnection)`.
51pub trait IntoInlineConnection<T, R: ConnectionResolver + ?Sized> {
52    fn into_inline_connection(self, connection_resolver: R) -> T;
53}
54
55/// Expresses how a struct/enum can access details about any connections it
56/// uses. Meant to be used as a type constraint on structs that use connections.
57pub trait ConnectionAccess:
58    Arbitrary + Clone + Debug + Eq + PartialEq + Serialize + 'static
59{
60    type Kafka: Arbitrary
61        + Clone
62        + Debug
63        + Eq
64        + PartialEq
65        + Hash
66        + Serialize
67        + for<'a> Deserialize<'a>
68        + AlterCompatible;
69    type Pg: Arbitrary
70        + Clone
71        + Debug
72        + Eq
73        + PartialEq
74        + Hash
75        + Serialize
76        + for<'a> Deserialize<'a>
77        + AlterCompatible;
78    type Aws: Arbitrary
79        + Clone
80        + Debug
81        + Eq
82        + PartialEq
83        + Hash
84        + Serialize
85        + for<'a> Deserialize<'a>
86        + AlterCompatible;
87    type Ssh: Arbitrary
88        + Clone
89        + Debug
90        + Eq
91        + PartialEq
92        + Hash
93        + Serialize
94        + for<'a> Deserialize<'a>
95        + AlterCompatible;
96    type Csr: Arbitrary
97        + Clone
98        + Debug
99        + Eq
100        + PartialEq
101        + Hash
102        + Serialize
103        + for<'a> Deserialize<'a>
104        + AlterCompatible;
105    type MySql: Arbitrary
106        + Clone
107        + Debug
108        + Eq
109        + PartialEq
110        + Hash
111        + Serialize
112        + for<'a> Deserialize<'a>
113        + AlterCompatible;
114    type SqlServer: Arbitrary
115        + Clone
116        + Debug
117        + Eq
118        + PartialEq
119        + Hash
120        + Serialize
121        + for<'a> Deserialize<'a>
122        + AlterCompatible;
123}
124
125/// Expresses that the struct contains references to connections. Use a
126/// combination of [`IntoInlineConnection`] and [`ConnectionResolver`] to take
127/// this into [`InlinedConnection`].
128#[derive(Arbitrary, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
129pub struct ReferencedConnection;
130
131impl ConnectionAccess for ReferencedConnection {
132    type Kafka = CatalogItemId;
133    type Pg = CatalogItemId;
134    type Aws = CatalogItemId;
135    type Ssh = CatalogItemId;
136    type Csr = CatalogItemId;
137    type MySql = CatalogItemId;
138    type SqlServer = CatalogItemId;
139}
140
141/// Expresses that the struct contains an inlined definition of a connection.
142#[derive(Arbitrary, Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
143pub struct InlinedConnection;
144
145impl ConnectionAccess for InlinedConnection {
146    type Kafka = super::KafkaConnection;
147    type Pg = super::PostgresConnection;
148    type Aws = super::aws::AwsConnection;
149    type Ssh = super::SshConnection;
150    type Csr = super::CsrConnection;
151    type MySql = super::MySqlConnection;
152    type SqlServer = super::SqlServerConnectionDetails;
153}