pipederive 0.2.1

Proc macros for data integration app using pipebase framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use super::{Expr, VisitContextStoreMeta, VisitErrorHandlerMeta, VisitPipeMeta};

use std::collections::{HashMap, HashSet};
use std::iter::FromIterator;
use syn::Attribute;

use crate::constants::{
    BOOTSTRAP_PIPE_CHANNEL_BUFFER, BOOTSTRAP_PIPE_CHANNEL_DEFAULT_BUFFER,
    BOOTSTRAP_PIPE_CONFIG_EMPTY_PATH, BOOTSTRAP_PIPE_CONFIG_PATH, BOOTSTRAP_PIPE_CONFIG_TYPE,
    BOOTSTRAP_PIPE_IDENT_SUFFIX, BOOTSTRAP_PIPE_NAME, BOOTSTRAP_PIPE_OUTPUT, BOOTSTRAP_PIPE_TYPE,
    BOOTSTRAP_PIPE_UPSTREAM, BOOTSTRAP_PIPE_UPSTREAM_NAME_SEP, CONTEXT_STORE_CONFIG_EMPTY_PATH,
    CONTEXT_STORE_CONFIG_PATH, CONTEXT_STORE_CONFIG_TYPE, CONTEXT_STORE_IDENT_SUFFIX,
    CONTEXT_STORE_NAME, ERROR_HANDLER_CHANNEL_BUFFER, ERROR_HANDLER_CHANNEL_DEFAULT_BUFFER,
    ERROR_HANDLER_CONFIG_PATH, ERROR_HANDLER_CONFIG_TYPE,
};
use crate::utils::{
    get_meta, get_meta_number_value_by_meta_path, get_meta_string_value_by_meta_path,
};

/// Pipe configuration type name and path
#[derive(Clone)]
pub struct PipeConfigMeta {
    pub ty: String,
    pub path: Option<String>,
}

impl PipeConfigMeta {
    pub fn get_ty(&self) -> String {
        self.ty.to_owned()
    }

    pub fn get_path(&self) -> String {
        match self.path.to_owned() {
            Some(path) => path,
            None => BOOTSTRAP_PIPE_CONFIG_EMPTY_PATH.to_owned(),
        }
    }
}

/// Pipe metadata
#[derive(Clone)]
pub struct PipeMeta {
    pub name: String,
    pub ident: String,
    pub ty: String,
    pub config_meta: PipeConfigMeta,
    pub output_type_name: Option<String>,
    pub buffer: usize,
    pub upstream_names: Vec<String>,
    pub upstream_output_type_name: Option<String>,
    pub downstream_names: Vec<String>,
}

impl PipeMeta {
    pub fn accept<V: VisitPipeMeta>(&self, visitor: &mut V) {
        visitor.visit(self);
    }

    pub fn get_name(&self) -> &String {
        &self.name
    }

    pub fn get_ident(&self) -> &String {
        &self.ident
    }

    pub fn get_ty(&self) -> &String {
        &self.ty
    }

    pub fn get_config_meta(&self) -> &PipeConfigMeta {
        &self.config_meta
    }

    pub fn get_output_type_name(&self) -> Option<&String> {
        self.output_type_name.as_ref()
    }

    pub fn get_upstream_output_type_name(&self) -> Option<String> {
        self.upstream_output_type_name.to_owned()
    }

    pub fn get_channel_buffer(&self) -> usize {
        self.buffer
    }

    pub fn get_upstream_names(&self) -> &Vec<String> {
        &self.upstream_names
    }

    pub fn set_upstream_output_type_name(&mut self, upstream_output_type_name: String) {
        // upstream pipes should have identical output meta
        match self.upstream_output_type_name {
            Some(ref local_upstream_output_type_name) => {
                assert!(
                    local_upstream_output_type_name.eq(&upstream_output_type_name),
                    "upstream output conflict, found {} != {}",
                    local_upstream_output_type_name,
                    upstream_output_type_name
                );
            }
            None => self.upstream_output_type_name = Some(upstream_output_type_name),
        }
    }

    pub fn add_downstream_names(&mut self, downstream_names: Vec<String>) {
        self.downstream_names.extend(downstream_names)
    }

    pub fn get_downstream_names(&self) -> &Vec<String> {
        &self.downstream_names
    }

    pub fn parse(attribute: &Attribute, ident_location: &str) -> Self {
        let name = Self::parse_name(attribute, ident_location);
        let ident = Self::ident(&name);
        PipeMeta {
            name,
            ident,
            ty: Self::parse_ty(attribute, ident_location),
            config_meta: Self::parse_config_meta(attribute, ident_location),
            output_type_name: Self::parse_output_meta(attribute),
            upstream_names: Self::parse_upstream_names(attribute),
            buffer: Self::parse_channel_buffer(attribute)
                .unwrap_or(BOOTSTRAP_PIPE_CHANNEL_DEFAULT_BUFFER),
            upstream_output_type_name: None,
            downstream_names: vec![],
        }
    }

    fn parse_name(attribute: &Attribute, ident_location: &str) -> String {
        get_meta_string_value_by_meta_path(
            BOOTSTRAP_PIPE_NAME,
            &get_meta(attribute),
            true,
            ident_location,
        )
        .unwrap()
    }

    fn parse_ty(attribute: &Attribute, ident_location: &str) -> String {
        get_meta_string_value_by_meta_path(
            BOOTSTRAP_PIPE_TYPE,
            &get_meta(attribute),
            true,
            ident_location,
        )
        .unwrap()
    }

    fn parse_upstream_names(attribute: &Attribute) -> Vec<String> {
        match get_meta_string_value_by_meta_path(
            BOOTSTRAP_PIPE_UPSTREAM,
            &get_meta(attribute),
            false,
            "",
        ) {
            Some(upstream_names) => {
                // split into vector of upstreams
                upstream_names
                    .split(BOOTSTRAP_PIPE_UPSTREAM_NAME_SEP)
                    .map(|n| {
                        let mut n = n.to_owned();
                        // clean whitespace after split
                        n.retain(|c| !c.is_whitespace());
                        n
                    })
                    .collect()
            }
            None => vec![],
        }
    }

    fn parse_config_meta(attribute: &Attribute, ident_location: &str) -> PipeConfigMeta {
        let ty = get_meta_string_value_by_meta_path(
            BOOTSTRAP_PIPE_CONFIG_TYPE,
            &get_meta(attribute),
            true,
            ident_location,
        )
        .unwrap();
        let path = get_meta_string_value_by_meta_path(
            BOOTSTRAP_PIPE_CONFIG_PATH,
            &get_meta(attribute),
            false,
            ident_location,
        );
        PipeConfigMeta { ty, path }
    }

    fn parse_output_meta(attribute: &Attribute) -> Option<String> {
        get_meta_string_value_by_meta_path(BOOTSTRAP_PIPE_OUTPUT, &get_meta(attribute), false, "")
    }

    fn parse_channel_buffer(attribute: &Attribute) -> Option<usize> {
        let buffer = get_meta_number_value_by_meta_path(
            BOOTSTRAP_PIPE_CHANNEL_BUFFER,
            &get_meta(attribute),
            false,
            "",
        );
        buffer.map(|b| b.parse().unwrap())
    }

    pub fn ident(name: &str) -> String {
        format!("{}{}", name, BOOTSTRAP_PIPE_IDENT_SUFFIX)
    }

    pub fn generate_pipe_meta_expr<T: VisitPipeMeta + Expr>(&self) -> Option<String> {
        let mut visitor = T::default();
        self.accept(&mut visitor);
        visitor.to_expr()
    }
}

#[derive(Default)]
pub struct PipeMetas {
    pub pipe_metas: HashMap<String, PipeMeta>,
}

impl PipeMetas {
    pub fn parse(attributes: &[Attribute], ident_location: &str) -> Self {
        let mut pipe_metas: HashMap<String, PipeMeta> = HashMap::new();
        let mut pipe_names: HashSet<String> = HashSet::new();
        let mut pipe_output_type_names: HashMap<String, Option<String>> = HashMap::new();
        let mut downstream_pipe_names: HashMap<String, Vec<String>> = HashMap::new();
        let mut upstream_pipe_names: HashMap<String, HashSet<String>> = HashMap::new();
        for attribute in attributes {
            let pipe_meta = &PipeMeta::parse(attribute, ident_location);
            let pipe_name = pipe_meta.get_name();
            assert!(
                pipe_names.insert(pipe_name.to_owned()),
                "duplicated pipe name {}",
                pipe_name
            );
            pipe_metas.insert(pipe_name.to_owned(), pipe_meta.to_owned());
            // collect output type per pipe - channel ty
            pipe_output_type_names.insert(
                pipe_name.to_owned(),
                pipe_meta.get_output_type_name().cloned(),
            );
            // collect upstream pipe for input lookup - channel rx
            upstream_pipe_names.insert(
                pipe_name.to_owned(),
                HashSet::from_iter(pipe_meta.get_upstream_names().to_owned()),
            );
            // collect downstream pipe - channel tx
            for upstream_pipe_name in pipe_meta.get_upstream_names() {
                let ds = downstream_pipe_names
                    .entry(upstream_pipe_name.to_owned())
                    .or_insert_with(Vec::new);
                ds.push(pipe_name.to_owned());
            }
        }
        for pipe_name in &pipe_names {
            let pipe_meta = pipe_metas.get_mut(pipe_name).expect("pipe meta");
            // connect downstream pipe
            pipe_meta.add_downstream_names(
                downstream_pipe_names
                    .get(pipe_name)
                    .cloned()
                    .unwrap_or_default(),
            );
            // setup upstream output as input type for channel
            for upstream_pipe_name in upstream_pipe_names.get(pipe_name).expect("upstreams") {
                let upstream_output_type_name = pipe_output_type_names
                    .get(upstream_pipe_name)
                    .unwrap_or_else(|| {
                        panic!("upstream pipe {} does not exists", upstream_pipe_name)
                    })
                    .to_owned()
                    .unwrap_or_else(|| {
                        panic!(
                            "output type not found in upstream pipe {}",
                            upstream_pipe_name
                        )
                    });
                pipe_meta.set_upstream_output_type_name(upstream_output_type_name);
            }
        }
        PipeMetas { pipe_metas }
    }

    pub fn list_pipe_ident(&self) -> Vec<String> {
        self.pipe_metas
            .values()
            .into_iter()
            .map(|k| k.get_ident().to_owned())
            .collect()
    }

    // generate expr per pipe meta
    pub fn generate_pipe_meta_exprs<T: VisitPipeMeta + Expr>(&self) -> Vec<String> {
        self.pipe_metas
            .values()
            .into_iter()
            .filter_map(|meta| meta.generate_pipe_meta_expr::<T>())
            .collect()
    }

    pub fn accept<T: VisitPipeMeta>(&self, visitor: &mut T) {
        for pipe_meta in self.pipe_metas.values() {
            pipe_meta.accept(visitor)
        }
    }
}

pub struct ContextStoreConfigMeta {
    ty: String,
    path: Option<String>,
}

impl ContextStoreConfigMeta {
    pub fn get_ty(&self) -> String {
        self.ty.to_owned()
    }

    pub fn get_path(&self) -> String {
        match self.path.to_owned() {
            Some(path) => path,
            None => CONTEXT_STORE_CONFIG_EMPTY_PATH.to_owned(),
        }
    }
}

pub struct ContextStoreMeta {
    name: String,
    ident: String,
    config_meta: ContextStoreConfigMeta,
    pipe_idents: Vec<String>,
}

impl ContextStoreMeta {
    pub fn accept<V: VisitContextStoreMeta>(&self, visitor: &mut V) {
        visitor.visit(self)
    }

    pub fn get_name(&self) -> &String {
        &self.name
    }

    pub fn get_ident(&self) -> &String {
        &self.ident
    }

    pub fn set_pipes(&mut self, pipe_idents: Vec<String>) {
        self.pipe_idents = pipe_idents
    }

    pub fn get_pipes(&self) -> Vec<String> {
        self.pipe_idents.to_owned()
    }

    pub fn get_config_meta(&self) -> &ContextStoreConfigMeta {
        &self.config_meta
    }

    pub fn parse(attribute: &Attribute, ident_location: &str) -> Self {
        let name = Self::parse_name(attribute, ident_location);
        let ident = format!("{}{}", name, CONTEXT_STORE_IDENT_SUFFIX);
        ContextStoreMeta {
            name,
            ident,
            config_meta: Self::parse_config_meta(attribute, ident_location),
            pipe_idents: Vec::new(),
        }
    }

    fn parse_name(attribute: &Attribute, ident_location: &str) -> String {
        get_meta_string_value_by_meta_path(
            CONTEXT_STORE_NAME,
            &get_meta(attribute),
            true,
            ident_location,
        )
        .unwrap()
    }

    fn parse_config_meta(attribute: &Attribute, ident_location: &str) -> ContextStoreConfigMeta {
        let ty = get_meta_string_value_by_meta_path(
            CONTEXT_STORE_CONFIG_TYPE,
            &get_meta(attribute),
            true,
            ident_location,
        )
        .unwrap();
        let path = get_meta_string_value_by_meta_path(
            CONTEXT_STORE_CONFIG_PATH,
            &get_meta(attribute),
            false,
            "",
        );
        ContextStoreConfigMeta { ty, path }
    }

    pub fn generate_cstore_meta_expr<V: VisitContextStoreMeta + Expr>(&self) -> Option<String> {
        let mut visitor = V::default();
        self.accept(&mut visitor);
        visitor.to_expr()
    }
}

pub struct ContextStoreMetas {
    metas: Vec<ContextStoreMeta>,
}

impl ContextStoreMetas {
    pub fn parse(attributes: &[Attribute], ident_location: &str) -> Self {
        let metas: Vec<ContextStoreMeta> = attributes
            .iter()
            .map(|attribute| ContextStoreMeta::parse(attribute, ident_location))
            .collect();
        ContextStoreMetas { metas }
    }

    pub fn add_pipes(&mut self, pipe_idents: Vec<String>) {
        for meta in &mut self.metas {
            meta.set_pipes(pipe_idents.to_owned())
        }
    }

    pub fn generate_cstore_meta_exprs<V: VisitContextStoreMeta + Expr>(&self) -> Vec<String> {
        self.metas
            .iter()
            .filter_map(|meta| meta.generate_cstore_meta_expr::<V>())
            .collect()
    }

    pub fn accept<V: VisitContextStoreMeta>(&self, visitor: &mut V) {
        for meta in &self.metas {
            visitor.visit(meta)
        }
    }
}

pub struct ErrorHandlerConfigMeta {
    ty: String,
    path: Option<String>,
}

impl ErrorHandlerConfigMeta {
    pub fn get_ty(&self) -> String {
        self.ty.to_owned()
    }

    pub fn get_path(&self) -> String {
        match self.path.to_owned() {
            Some(path) => path,
            None => CONTEXT_STORE_CONFIG_EMPTY_PATH.to_owned(),
        }
    }
}

pub struct ErrorHandlerMeta {
    config_meta: ErrorHandlerConfigMeta,
    buffer: usize,
    pipe_idents: Vec<String>,
}

impl ErrorHandlerMeta {
    pub fn accept<V: VisitErrorHandlerMeta>(&self, visitor: &mut V) {
        visitor.visit(self)
    }

    pub fn set_pipes(&mut self, pipe_idents: Vec<String>) {
        self.pipe_idents = pipe_idents;
    }

    pub fn get_pipes(&self) -> Vec<String> {
        self.pipe_idents.to_owned()
    }

    pub fn get_config_meta(&self) -> &ErrorHandlerConfigMeta {
        &self.config_meta
    }

    pub fn get_channel_buffer(&self) -> usize {
        self.buffer
    }

    pub fn parse(attribute: Option<&Attribute>, ident_location: &str) -> Option<Self> {
        let attribute = match attribute {
            Some(attribute) => attribute,
            None => return None,
        };
        let config_meta = Self::parse_config_meta(attribute, ident_location);
        let buffer =
            Self::parse_channel_buffer(attribute).unwrap_or(ERROR_HANDLER_CHANNEL_DEFAULT_BUFFER);
        Some(ErrorHandlerMeta {
            config_meta,
            buffer,
            pipe_idents: Vec::new(),
        })
    }

    fn parse_config_meta(attribute: &Attribute, ident_location: &str) -> ErrorHandlerConfigMeta {
        let ty = get_meta_string_value_by_meta_path(
            ERROR_HANDLER_CONFIG_TYPE,
            &get_meta(attribute),
            true,
            ident_location,
        )
        .unwrap();
        let path = get_meta_string_value_by_meta_path(
            ERROR_HANDLER_CONFIG_PATH,
            &get_meta(attribute),
            false,
            "",
        );
        ErrorHandlerConfigMeta { ty, path }
    }

    fn parse_channel_buffer(attribute: &Attribute) -> Option<usize> {
        let buffer = get_meta_number_value_by_meta_path(
            ERROR_HANDLER_CHANNEL_BUFFER,
            &get_meta(attribute),
            false,
            "",
        );
        buffer.map(|b| b.parse().unwrap())
    }

    pub fn generate_error_handler_meta_expr<V: VisitErrorHandlerMeta + Expr>(
        &self,
    ) -> Option<String> {
        let mut visitor = V::default();
        self.accept(&mut visitor);
        visitor.to_expr()
    }
}