selfware 0.2.2

Your personal AI workshop — software you own, software that lasts
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
//! AST -- Abstract Syntax Tree node definitions for the workflow DSL.

#![allow(dead_code, unused_imports, unused_variables)]

/// AST Node types
#[derive(Debug, Clone)]
pub enum AstNode {
    /// Workflow definition
    Workflow {
        name: String,
        body: Vec<AstNode>,
    },

    /// Step definition
    Step {
        name: String,
        command: Box<AstNode>,
    },

    /// Parallel execution block
    Parallel {
        body: Vec<AstNode>,
    },

    /// Sequence block
    Sequence {
        body: Vec<AstNode>,
    },

    /// If statement
    If {
        condition: Box<AstNode>,
        then_branch: Vec<AstNode>,
        else_branch: Option<Vec<AstNode>>,
    },

    /// For loop
    For {
        variable: String,
        iterable: Box<AstNode>,
        body: Vec<AstNode>,
    },

    /// While loop
    While {
        condition: Box<AstNode>,
        body: Vec<AstNode>,
    },

    /// Variable assignment
    Let {
        name: String,
        value: Box<AstNode>,
    },

    /// Function definition
    FnDef {
        name: String,
        params: Vec<String>,
        body: Vec<AstNode>,
    },

    /// Function call
    Call {
        name: String,
        args: Vec<AstNode>,
    },

    /// Binary expression
    Binary {
        left: Box<AstNode>,
        operator: String,
        right: Box<AstNode>,
    },

    /// Unary expression
    Unary {
        operator: String,
        operand: Box<AstNode>,
    },

    /// Property access
    Property {
        object: Box<AstNode>,
        property: String,
    },

    /// Pipeline
    Pipeline {
        stages: Vec<AstNode>,
    },

    /// Return statement
    Return {
        value: Option<Box<AstNode>>,
    },

    /// Error handler
    OnError {
        handler: Box<AstNode>,
    },

    /// Literals
    Identifier(String),
    StringLit(String),
    IntegerLit(i64),
    FloatLit(f64),
    BooleanLit(bool),
    ArrayLit(Vec<AstNode>),

    /// Command (for shell execution)
    Command(String),
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_literal_nodes_construction() {
        let id = AstNode::Identifier("my_var".into());
        let s = AstNode::StringLit("hello".into());
        let i = AstNode::IntegerLit(42);
        let f = AstNode::FloatLit(1.23);
        let b = AstNode::BooleanLit(true);
        let cmd = AstNode::Command("echo hello".into());

        // Verify Debug output contains expected values
        assert!(format!("{:?}", id).contains("my_var"));
        assert!(format!("{:?}", s).contains("hello"));
        assert!(format!("{:?}", i).contains("42"));
        assert!(format!("{:?}", f).contains("1.23"));
        assert!(format!("{:?}", b).contains("true"));
        assert!(format!("{:?}", cmd).contains("echo hello"));
    }

    #[test]
    fn test_workflow_node() {
        let wf = AstNode::Workflow {
            name: "build".into(),
            body: vec![AstNode::Step {
                name: "check".into(),
                command: Box::new(AstNode::Command("cargo check".into())),
            }],
        };

        if let AstNode::Workflow { name, body } = &wf {
            assert_eq!(name, "build");
            assert_eq!(body.len(), 1);
        } else {
            panic!("Expected Workflow node");
        }
    }

    #[test]
    fn test_step_node() {
        let step = AstNode::Step {
            name: "test".into(),
            command: Box::new(AstNode::Command("cargo test".into())),
        };

        if let AstNode::Step { name, command } = &step {
            assert_eq!(name, "test");
            assert!(matches!(command.as_ref(), AstNode::Command(c) if c == "cargo test"));
        } else {
            panic!("Expected Step node");
        }
    }

    #[test]
    fn test_parallel_and_sequence_blocks() {
        let par = AstNode::Parallel {
            body: vec![
                AstNode::Command("cargo clippy".into()),
                AstNode::Command("cargo fmt".into()),
            ],
        };

        if let AstNode::Parallel { body } = &par {
            assert_eq!(body.len(), 2);
        } else {
            panic!("Expected Parallel node");
        }

        let seq = AstNode::Sequence {
            body: vec![
                AstNode::Command("cargo build".into()),
                AstNode::Command("cargo test".into()),
            ],
        };

        if let AstNode::Sequence { body } = &seq {
            assert_eq!(body.len(), 2);
        } else {
            panic!("Expected Sequence node");
        }
    }

    #[test]
    fn test_if_node_with_else() {
        let if_node = AstNode::If {
            condition: Box::new(AstNode::BooleanLit(true)),
            then_branch: vec![AstNode::Command("echo yes".into())],
            else_branch: Some(vec![AstNode::Command("echo no".into())]),
        };

        if let AstNode::If {
            condition,
            then_branch,
            else_branch,
        } = &if_node
        {
            assert!(matches!(condition.as_ref(), AstNode::BooleanLit(true)));
            assert_eq!(then_branch.len(), 1);
            assert!(else_branch.is_some());
            assert_eq!(else_branch.as_ref().unwrap().len(), 1);
        } else {
            panic!("Expected If node");
        }
    }

    #[test]
    fn test_if_node_without_else() {
        let if_node = AstNode::If {
            condition: Box::new(AstNode::BooleanLit(false)),
            then_branch: vec![AstNode::Command("echo yes".into())],
            else_branch: None,
        };

        if let AstNode::If { else_branch, .. } = &if_node {
            assert!(else_branch.is_none());
        } else {
            panic!("Expected If node");
        }
    }

    #[test]
    fn test_for_loop_node() {
        let for_node = AstNode::For {
            variable: "item".into(),
            iterable: Box::new(AstNode::ArrayLit(vec![
                AstNode::IntegerLit(1),
                AstNode::IntegerLit(2),
                AstNode::IntegerLit(3),
            ])),
            body: vec![AstNode::Call {
                name: "process".into(),
                args: vec![AstNode::Identifier("item".into())],
            }],
        };

        if let AstNode::For {
            variable,
            iterable,
            body,
        } = &for_node
        {
            assert_eq!(variable, "item");
            assert!(matches!(iterable.as_ref(), AstNode::ArrayLit(items) if items.len() == 3));
            assert_eq!(body.len(), 1);
        } else {
            panic!("Expected For node");
        }
    }

    #[test]
    fn test_binary_and_unary_expressions() {
        let binary = AstNode::Binary {
            left: Box::new(AstNode::IntegerLit(1)),
            operator: "+".into(),
            right: Box::new(AstNode::IntegerLit(2)),
        };

        if let AstNode::Binary { operator, .. } = &binary {
            assert_eq!(operator, "+");
        } else {
            panic!("Expected Binary node");
        }

        let unary = AstNode::Unary {
            operator: "!".into(),
            operand: Box::new(AstNode::BooleanLit(false)),
        };

        if let AstNode::Unary { operator, .. } = &unary {
            assert_eq!(operator, "!");
        } else {
            panic!("Expected Unary node");
        }
    }

    #[test]
    fn test_let_and_fn_def_nodes() {
        let let_node = AstNode::Let {
            name: "x".into(),
            value: Box::new(AstNode::IntegerLit(42)),
        };

        if let AstNode::Let { name, .. } = &let_node {
            assert_eq!(name, "x");
        } else {
            panic!("Expected Let node");
        }

        let fn_node = AstNode::FnDef {
            name: "greet".into(),
            params: vec!["name".into()],
            body: vec![AstNode::Return {
                value: Some(Box::new(AstNode::StringLit("hello".into()))),
            }],
        };

        if let AstNode::FnDef { name, params, body } = &fn_node {
            assert_eq!(name, "greet");
            assert_eq!(params, &["name"]);
            assert_eq!(body.len(), 1);
        } else {
            panic!("Expected FnDef node");
        }
    }

    #[test]
    fn test_property_access_node() {
        let prop = AstNode::Property {
            object: Box::new(AstNode::Identifier("step1".into())),
            property: "success".into(),
        };

        if let AstNode::Property { object, property } = &prop {
            assert!(matches!(object.as_ref(), AstNode::Identifier(id) if id == "step1"));
            assert_eq!(property, "success");
        } else {
            panic!("Expected Property node");
        }
    }

    #[test]
    fn test_pipeline_node() {
        let pipe = AstNode::Pipeline {
            stages: vec![
                AstNode::Command("cargo build".into()),
                AstNode::Command("cargo test".into()),
                AstNode::Command("cargo clippy".into()),
            ],
        };

        if let AstNode::Pipeline { stages } = &pipe {
            assert_eq!(stages.len(), 3);
        } else {
            panic!("Expected Pipeline node");
        }
    }

    #[test]
    fn test_return_node_with_and_without_value() {
        let ret_with = AstNode::Return {
            value: Some(Box::new(AstNode::IntegerLit(0))),
        };
        if let AstNode::Return { value } = &ret_with {
            assert!(value.is_some());
        } else {
            panic!("Expected Return node");
        }

        let ret_without = AstNode::Return { value: None };
        if let AstNode::Return { value } = &ret_without {
            assert!(value.is_none());
        } else {
            panic!("Expected Return node");
        }
    }

    #[test]
    fn test_on_error_node() {
        let on_err = AstNode::OnError {
            handler: Box::new(AstNode::Call {
                name: "rollback".into(),
                args: vec![],
            }),
        };

        if let AstNode::OnError { handler } = &on_err {
            assert!(matches!(handler.as_ref(), AstNode::Call { name, .. } if name == "rollback"));
        } else {
            panic!("Expected OnError node");
        }
    }

    #[test]
    fn test_array_literal_node() {
        let arr = AstNode::ArrayLit(vec![
            AstNode::StringLit("a".into()),
            AstNode::StringLit("b".into()),
        ]);

        if let AstNode::ArrayLit(items) = &arr {
            assert_eq!(items.len(), 2);
        } else {
            panic!("Expected ArrayLit node");
        }

        // Empty array
        let empty = AstNode::ArrayLit(vec![]);
        if let AstNode::ArrayLit(items) = &empty {
            assert!(items.is_empty());
        } else {
            panic!("Expected ArrayLit node");
        }
    }

    #[test]
    fn test_clone_preserves_deep_structure() {
        let original = AstNode::Workflow {
            name: "test".into(),
            body: vec![AstNode::If {
                condition: Box::new(AstNode::Binary {
                    left: Box::new(AstNode::Identifier("x".into())),
                    operator: ">".into(),
                    right: Box::new(AstNode::IntegerLit(0)),
                }),
                then_branch: vec![AstNode::Command("echo positive".into())],
                else_branch: Some(vec![AstNode::Command("echo non-positive".into())]),
            }],
        };

        let cloned = original.clone();

        // Verify structural equivalence via Debug output
        assert_eq!(format!("{:?}", original), format!("{:?}", cloned));
    }

    #[test]
    fn test_while_loop_node() {
        let while_node = AstNode::While {
            condition: Box::new(AstNode::BooleanLit(true)),
            body: vec![AstNode::Command("do_work".into())],
        };

        if let AstNode::While { condition, body } = &while_node {
            assert!(matches!(condition.as_ref(), AstNode::BooleanLit(true)));
            assert_eq!(body.len(), 1);
        } else {
            panic!("Expected While node");
        }
    }

    #[test]
    fn test_call_node_with_args() {
        let call = AstNode::Call {
            name: "build_project".into(),
            args: vec![
                AstNode::StringLit("release".into()),
                AstNode::BooleanLit(true),
            ],
        };

        if let AstNode::Call { name, args } = &call {
            assert_eq!(name, "build_project");
            assert_eq!(args.len(), 2);
        } else {
            panic!("Expected Call node");
        }
    }
}