Skip to main content

flwrs_plugin/plugin/
core.rs

1use crate::schema::schema::{
2    field_type::Enum as FieldType, FieldDefinition as PbFieldDefinition,
3    SchemaDefinition as PbSchemaDefinition,
4};
5use crate::schema::sink::Initialize as SinkInitialize;
6use crate::schema::source::Initialize as SourceInitialize;
7use crate::schema::transform::Initialize as TransformInitialize;
8use prost::alloc::boxed::Box as PbBox;
9use prost::alloc::string::String;
10
11pub struct InitializeRequest {
12    id: String,
13    version: String,
14    in_schema: SchemaDefinition,
15    out_schema: SchemaDefinition,
16}
17
18impl InitializeRequest {
19    pub fn new() -> Self {
20        Self {
21            id: String::new(),
22            version: String::new(),
23            in_schema: SchemaDefinition::new(),
24            out_schema: SchemaDefinition::new(),
25        }
26    }
27
28    pub fn with_id(mut self, id: String) -> Self {
29        self.id = id;
30        self
31    }
32
33    pub fn with_version(mut self, version: String) -> Self {
34        self.version = version;
35        self
36    }
37
38    pub fn with_schema(mut self, schema: SchemaDefinition) -> Self {
39        self.in_schema = schema;
40        self
41    }
42}
43
44impl Into<SinkInitialize> for InitializeRequest {
45    fn into(self) -> SinkInitialize {
46        SinkInitialize {
47            plugin_id: self.id,
48            plugin_version: self.version,
49            schema: Some(self.in_schema.into()),
50        }
51    }
52}
53
54impl Into<SourceInitialize> for InitializeRequest {
55    fn into(self) -> SourceInitialize {
56        SourceInitialize {
57            plugin_id: self.id,
58            plugin_version: self.version,
59            schema: Some(self.out_schema.into()),
60        }
61    }
62}
63
64impl Into<TransformInitialize> for InitializeRequest {
65    fn into(self) -> TransformInitialize {
66        TransformInitialize {
67            plugin_id: self.id,
68            plugin_version: self.version,
69            in_schema: Some(self.in_schema.into()),
70            out_schema: Some(self.out_schema.into()),
71        }
72    }
73}
74
75pub struct SchemaDefinition {
76    fields: Vec<FieldDefinition>,
77}
78
79impl SchemaDefinition {
80    pub fn new() -> Self {
81        Self { fields: Vec::new() }
82    }
83
84    pub fn add_field(mut self, field: FieldDefinition) -> Self {
85        self.fields.push(field);
86        self
87    }
88
89    pub fn with_fields(mut self, fields: Vec<FieldDefinition>) -> Self {
90        self.fields = fields;
91        self
92    }
93
94    pub fn remove_field(mut self, idx: usize) -> Self {
95        self.fields.remove(idx);
96        self
97    }
98}
99
100impl Into<PbSchemaDefinition> for SchemaDefinition {
101    fn into(self) -> PbSchemaDefinition {
102        PbSchemaDefinition {
103            fields: self.fields.into_iter().map(|field| field.into()).collect(),
104        }
105    }
106}
107
108#[derive(Clone)]
109pub struct FieldDefinition {
110    key: String,
111    description: Option<String>,
112    type_: FieldType,
113    nested_type_definition: Option<Box<FieldDefinition>>,
114    object_fields: Option<Vec<FieldDefinition>>,
115}
116
117impl FieldDefinition {
118    pub fn new() -> Self {
119        Self {
120            key: String::new(),
121            description: None,
122            type_: FieldType::String,
123            nested_type_definition: None,
124            object_fields: None,
125        }
126    }
127
128    pub fn with_key(mut self, key: String) -> Self {
129        self.key = key;
130        self
131    }
132
133    pub fn with_description(mut self, description: String) -> Self {
134        self.description = Some(description);
135        self
136    }
137
138    pub fn with_type(mut self, type_: FieldType) -> Self {
139        self.type_ = type_;
140        self
141    }
142
143    pub fn with_nested_type_definition(mut self, definition: FieldDefinition) -> Self {
144        self.nested_type_definition = Some(Box::new(definition));
145        self
146    }
147
148    pub fn with_object_fields(mut self, fields: Vec<FieldDefinition>) -> Self {
149        self.object_fields = Some(fields);
150        self
151    }
152}
153
154impl Into<PbFieldDefinition> for FieldDefinition {
155    fn into(self) -> PbFieldDefinition {
156        PbFieldDefinition {
157            key: self.key,
158            r#type: self.type_ as i32,
159            description: match self.description {
160                None => String::new(),
161                Some(str) => String::from(str),
162            },
163            nested_type_definition: match self.nested_type_definition {
164                None => None,
165                Some(t) => Some(PbBox::new(t.into())),
166            },
167            object_fields: match self.object_fields {
168                None => Vec::new(),
169                Some(fields) => fields.into_iter().map(|field| field.into()).collect(),
170            },
171        }
172    }
173}
174
175impl Into<PbFieldDefinition> for Box<FieldDefinition> {
176    fn into(self) -> PbFieldDefinition {
177        PbFieldDefinition {
178            key: self.key,
179            r#type: self.type_ as i32,
180            description: match self.description {
181                None => String::new(),
182                Some(str) => String::from(str),
183            },
184            nested_type_definition: match self.nested_type_definition {
185                None => None,
186                Some(t) => Some(PbBox::new(t.into())),
187            },
188            object_fields: match self.object_fields {
189                None => Vec::new(),
190                Some(fields) => fields.into_iter().map(|field| field.into()).collect(),
191            },
192        }
193    }
194}
195
196pub struct ConnectionConfig {
197    pub host: String,
198    pub port: u16,
199}