copybook-core 0.4.3

Core COBOL copybook parser, schema, and validation primitives.
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Field projection for copybook schemas
//!
//! Provides functionality to create subset schemas by selecting specific fields
//! from a copybook, with automatic dependency resolution for ODO counters and
//! parent groups.

use crate::schema::{Field, Occurs, Schema};
use crate::{Error, ErrorCode, Result};
use std::collections::HashSet;

/// Project schema to include only selected fields plus dependencies
///
/// This function creates a new schema containing only the selected fields
/// and their required dependencies:
/// - ODO counter fields (DEPENDING ON) are automatically included
/// - Parent groups are included to maintain structure
/// - RENAMES aliases are resolved using `Schema::resolve_alias_to_target()`
/// - Group selection includes all child fields
///
/// # Arguments
/// * `schema` - Source schema to project from
/// * `selections` - List of field names or paths to include
///
/// # Returns
/// A new `Schema` with the projected subset of fields
///
/// # Errors
/// - `CBKS701_PROJECTION_INVALID_ODO`: ODO array selected but counter not accessible
/// - `CBKS702_PROJECTION_UNRESOLVED_ALIAS`: RENAMES alias spans unselected fields
/// - `CBKS703_PROJECTION_FIELD_NOT_FOUND`: Selected field doesn't exist in schema
///
/// # Examples
///
/// ```
/// use copybook_core::{parse_copybook, project_schema};
///
/// let schema = parse_copybook(
///     "01 CUSTOMER.\n   05 ID PIC 9(6).\n   05 NAME PIC X(30)."
/// ).unwrap();
/// let projected = project_schema(&schema, &["ID".to_string()]).unwrap();
/// // Projected schema includes ID and parent group CUSTOMER
/// assert_eq!(projected.fields[0].name, "CUSTOMER");
/// assert_eq!(projected.fields[0].children.len(), 1);
/// assert_eq!(projected.fields[0].children[0].name, "ID");
/// ```
pub fn project_schema(schema: &Schema, selections: &[String]) -> Result<Schema> {
    if selections.is_empty() {
        return Ok(Schema::from_fields(Vec::new()));
    }

    // Step 1: Normalize and resolve field selections
    let mut selected_paths = HashSet::new();

    for selection in selections {
        let normalized = selection.trim();

        // Try to find the field by path or name
        let field = find_field_by_name_or_path(schema, normalized).ok_or_else(|| {
            Error::new(
                ErrorCode::CBKS703_PROJECTION_FIELD_NOT_FOUND,
                format!("Field '{}' not found in schema", normalized),
            )
        })?;

        // If it's a level-66 RENAMES alias, expand to target fields
        if field.level == 66 {
            if let Some(ref resolved) = field.resolved_renames {
                // Add all member fields from the alias
                for member_path in &resolved.members {
                    selected_paths.insert(member_path.clone());

                    if let Some(member_field) = schema.find_field(member_path) {
                        // If member is a group, collect all its children
                        if member_field.is_group() {
                            collect_group_fields(member_field, &mut selected_paths);
                        }
                        // Always include level-88 condition children when present
                        collect_level88_children(member_field, &mut selected_paths);
                    }
                }
            } else {
                // RENAMES field without resolved members - this shouldn't happen
                return Err(Error::new(
                    ErrorCode::CBKS702_PROJECTION_UNRESOLVED_ALIAS,
                    format!("RENAMES alias '{}' has no resolved members", field.name),
                ));
            }
        } else {
            // Regular field or group
            selected_paths.insert(field.path.clone());

            // If it's a group, include all children
            if field.is_group() {
                collect_group_fields(field, &mut selected_paths);
            }
            // Always include level-88 condition children when present
            collect_level88_children(field, &mut selected_paths);
        }
    }

    // Step 2: Add parent groups first so ODO detection sees ancestor arrays
    add_parent_groups(schema, &mut selected_paths);

    // Step 3: Find and add ODO counter dependencies (after parents are present)
    let odo_counters = find_odo_counters(schema, &schema.fields, &selected_paths);
    if !odo_counters.is_empty() {
        selected_paths.extend(odo_counters);
        // Ensure newly added counters bring their parent groups along
        add_parent_groups(schema, &mut selected_paths);
    }

    // Step 4: Validate projection has no structural errors
    validate_projection(schema, &selected_paths)?;

    // Step 5: Build new schema with filtered field tree
    let projected_fields = filter_fields(&schema.fields, &selected_paths);

    // Step 6: Create new schema with recalculated properties
    let mut projected_schema = Schema::from_fields(projected_fields);

    // Update tail_odo if the ODO array is included
    if let Some(ref tail_odo) = schema.tail_odo
        && let Some(array_field) = find_field_by_name_or_path(schema, &tail_odo.array_path)
        && selected_paths.contains(&array_field.path)
    {
        projected_schema.tail_odo = Some(tail_odo.clone());
    }

    // Preserve the physical record contract regardless of projection contents
    projected_schema.lrecl_fixed = schema.lrecl_fixed;

    // Recalculate fingerprint for the new schema
    projected_schema.calculate_fingerprint();

    Ok(projected_schema)
}

/// Find a field by name or full path
///
/// First tries exact path match, then searches by field name recursively
fn find_field_by_name_or_path<'a>(schema: &'a Schema, name_or_path: &str) -> Option<&'a Field> {
    // Try exact path match first
    if let Some(field) = schema.find_field(name_or_path) {
        return Some(field);
    }

    // Try alias lookup (level-66)
    if let Some(field) = schema.find_field_or_alias(name_or_path) {
        return Some(field);
    }

    // Search by name in all fields
    find_field_by_name_recursive(&schema.fields, name_or_path)
}

/// Recursively search for a field by name (case-insensitive)
fn find_field_by_name_recursive<'a>(fields: &'a [Field], name: &str) -> Option<&'a Field> {
    for field in fields {
        // Check if this field's name matches (case-insensitive)
        if field.name.eq_ignore_ascii_case(name) {
            return Some(field);
        }
        // Recurse into children
        if let Some(found) = find_field_by_name_recursive(&field.children, name) {
            return Some(found);
        }
    }
    None
}

/// Collect all fields under a group recursively
fn collect_group_fields(field: &Field, collected: &mut HashSet<String>) {
    for child in &field.children {
        collected.insert(child.path.clone());
        if child.is_group() {
            collect_group_fields(child, collected);
        }
    }
}

/// Collect level-88 condition children for a field
fn collect_level88_children(field: &Field, collected: &mut HashSet<String>) {
    for child in &field.children {
        if child.level == 88 {
            collected.insert(child.path.clone());
        }
    }
}

/// Find ODO counter dependencies in selected fields
///
/// Returns full paths to counter fields that need to be included
fn find_odo_counters(
    schema: &Schema,
    fields: &[Field],
    selected: &HashSet<String>,
) -> HashSet<String> {
    fn scan_fields(
        schema: &Schema,
        fields: &[Field],
        selected: &HashSet<String>,
        counters: &mut HashSet<String>,
    ) {
        for field in fields {
            // Check if this field is selected and has ODO
            if selected.contains(&field.path)
                && let Some(Occurs::ODO { counter_path, .. }) = &field.occurs
                && let Some(counter_field) = find_field_by_name_or_path(schema, counter_path)
            {
                counters.insert(counter_field.path.clone());
            }

            // Recurse into children
            scan_fields(schema, &field.children, selected, counters);
        }
    }

    let mut counters = HashSet::new();
    scan_fields(schema, fields, selected, &mut counters);
    counters
}

/// Add parent groups to maintain structural integrity
fn add_parent_groups(schema: &Schema, selected: &mut HashSet<String>) {
    let paths_to_check: Vec<String> = selected.iter().cloned().collect();

    for path in paths_to_check {
        // Walk up the path hierarchy
        let mut current_path = path.as_str();

        while let Some(parent_path) = get_parent_path(current_path) {
            if selected.insert(parent_path.to_string()) {
                // If we added a new parent, check if there's a field for it
                if let Some(_parent_field) = schema.find_field(parent_path) {
                    // Parent exists and was added to selected set
                }
            }
            current_path = parent_path;
        }
    }
}

/// Get parent path from a field path (e.g., "A.B.C" -> "A.B")
fn get_parent_path(path: &str) -> Option<&str> {
    path.rfind('.').map(|idx| &path[..idx])
}

/// Validate projection has no structural errors
fn validate_projection(schema: &Schema, selected: &HashSet<String>) -> Result<()> {
    // Check all selected ODO fields have accessible counters
    for path in selected {
        if let Some(field) = schema.find_field(path)
            && let Some(Occurs::ODO { counter_path, .. }) = &field.occurs
        {
            // Resolve counter_path to full path
            if let Some(counter_field) = find_field_by_name_or_path(schema, counter_path) {
                if !selected.contains(&counter_field.path) {
                    return Err(Error::new(
                        ErrorCode::CBKS701_PROJECTION_INVALID_ODO,
                        format!(
                            "ODO array '{}' requires counter '{}' which is not selected",
                            field.path, counter_path
                        ),
                    ));
                }
            } else {
                // Counter not found in schema - this is also an error
                return Err(Error::new(
                    ErrorCode::CBKS701_PROJECTION_INVALID_ODO,
                    format!(
                        "ODO array '{}' references non-existent counter '{}'",
                        field.path, counter_path
                    ),
                ));
            }
        }
    }

    Ok(())
}

/// Filter fields to include only selected paths
fn filter_fields(fields: &[Field], selected: &HashSet<String>) -> Vec<Field> {
    let mut result = Vec::new();

    for field in fields {
        if selected.contains(&field.path) {
            // Clone the field and recursively filter its children
            let mut filtered_field = field.clone();
            filtered_field.children = filter_fields(&field.children, selected);
            result.push(filtered_field);
        }
    }

    result
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;
    use crate::schema::{FieldKind, ResolvedRenames, TailODO};

    fn create_simple_schema() -> Schema {
        let mut root = Field::new(1, "ROOT".to_string());
        root.path = "ROOT".to_string();
        root.kind = FieldKind::Group;

        let mut field1 = Field::new(5, "FIELD1".to_string());
        field1.path = "ROOT.FIELD1".to_string();
        field1.kind = FieldKind::Alphanum { len: 10 };
        field1.len = 10;

        let mut field2 = Field::new(5, "FIELD2".to_string());
        field2.path = "ROOT.FIELD2".to_string();
        field2.kind = FieldKind::ZonedDecimal {
            digits: 5,
            scale: 0,
            signed: false,
            sign_separate: None,
        };
        field2.len = 5;

        root.children = vec![field1, field2];

        Schema::from_fields(vec![root])
    }

    #[test]
    fn test_simple_field_selection() {
        let schema = create_simple_schema();
        let projected = project_schema(&schema, &["FIELD1".to_string()]).unwrap();

        // Should include ROOT (parent), ROOT.FIELD1
        assert_eq!(projected.fields.len(), 1);
        assert_eq!(projected.fields[0].name, "ROOT");
        assert_eq!(projected.fields[0].children.len(), 1);
        assert_eq!(projected.fields[0].children[0].name, "FIELD1");
    }

    #[test]
    fn test_nonexistent_field() {
        let schema = create_simple_schema();
        let result = project_schema(&schema, &["NONEXISTENT".to_string()]);

        assert!(result.is_err());
        if let Err(err) = result {
            assert_eq!(err.code, ErrorCode::CBKS703_PROJECTION_FIELD_NOT_FOUND);
        }
    }

    #[test]
    fn test_group_selection_includes_children() {
        let schema = create_simple_schema();
        let projected = project_schema(&schema, &["ROOT".to_string()]).unwrap();

        // Selecting ROOT should include all children
        assert_eq!(projected.fields.len(), 1);
        assert_eq!(projected.fields[0].name, "ROOT");
        assert_eq!(projected.fields[0].children.len(), 2);
    }

    #[test]
    fn test_odo_counter_auto_included() {
        let mut root = Field::new(1, "ROOT".to_string());
        root.path = "ROOT".to_string();
        root.kind = FieldKind::Group;

        let mut counter = Field::new(5, "COUNTER".to_string());
        counter.path = "ROOT.COUNTER".to_string();
        counter.kind = FieldKind::ZonedDecimal {
            digits: 3,
            scale: 0,
            signed: false,
            sign_separate: None,
        };
        counter.len = 3;

        let mut odo_array = Field::new(5, "ITEMS".to_string());
        odo_array.path = "ROOT.ITEMS".to_string();
        odo_array.kind = FieldKind::Group;
        odo_array.occurs = Some(Occurs::ODO {
            min: 1,
            max: 10,
            counter_path: "ROOT.COUNTER".to_string(),
        });

        let mut item_field = Field::new(10, "ITEM_ID".to_string());
        item_field.path = "ROOT.ITEMS.ITEM_ID".to_string();
        item_field.kind = FieldKind::Alphanum { len: 5 };
        item_field.len = 5;

        odo_array.children = vec![item_field];
        root.children = vec![counter, odo_array];

        let schema = Schema::from_fields(vec![root]);

        // Select only ITEMS - counter should be auto-included
        let projected = project_schema(&schema, &["ITEMS".to_string()]).unwrap();

        // Should have ROOT with both COUNTER and ITEMS
        assert_eq!(projected.fields.len(), 1);
        assert_eq!(projected.fields[0].children.len(), 2);

        let child_names: Vec<&str> = projected.fields[0]
            .children
            .iter()
            .map(|f| f.name.as_str())
            .collect();
        assert!(child_names.contains(&"COUNTER"));
        assert!(child_names.contains(&"ITEMS"));
    }

    #[test]
    fn test_odo_counter_added_when_selecting_leaf() {
        let mut root = Field::new(1, "ROOT".to_string());
        root.path = "ROOT".to_string();
        root.kind = FieldKind::Group;

        let mut counter = Field::new(5, "CTR".to_string());
        counter.path = "ROOT.CTR".to_string();
        counter.kind = FieldKind::ZonedDecimal {
            digits: 2,
            scale: 0,
            signed: false,
            sign_separate: None,
        };
        counter.len = 2;

        let mut odo_array = Field::new(5, "ITEMS".to_string());
        odo_array.path = "ROOT.ITEMS".to_string();
        odo_array.kind = FieldKind::Group;
        odo_array.occurs = Some(Occurs::ODO {
            min: 0,
            max: 5,
            counter_path: "ROOT.CTR".to_string(),
        });

        let mut item_field = Field::new(10, "ITEM_ID".to_string());
        item_field.path = "ROOT.ITEMS.ITEM_ID".to_string();
        item_field.kind = FieldKind::Alphanum { len: 3 };
        item_field.len = 3;

        odo_array.children = vec![item_field];
        root.children = vec![counter, odo_array];

        let schema = Schema::from_fields(vec![root]);

        // Select only the leaf inside the ODO array; counter should still be added
        let projected = project_schema(&schema, &["ITEM_ID".to_string()]).unwrap();
        let root_children = &projected.fields[0].children;
        assert_eq!(root_children.len(), 2);
        assert!(root_children.iter().any(|f| f.name == "CTR"));
        assert!(root_children.iter().any(|f| f.name == "ITEMS"));
    }

    #[test]
    fn test_renames_alias_expansion() {
        let mut root = Field::new(1, "ROOT".to_string());
        root.path = "ROOT".to_string();
        root.kind = FieldKind::Group;

        let mut field1 = Field::new(5, "FIELD1".to_string());
        field1.path = "ROOT.FIELD1".to_string();
        field1.kind = FieldKind::Alphanum { len: 10 };
        field1.len = 10;

        let mut field2 = Field::new(5, "FIELD2".to_string());
        field2.path = "ROOT.FIELD2".to_string();
        field2.kind = FieldKind::Alphanum { len: 10 };
        field2.len = 10;

        // Create level-66 RENAMES alias
        let mut alias = Field::new(66, "ALIAS".to_string());
        alias.path = "ROOT.ALIAS".to_string();
        alias.level = 66;
        alias.kind = FieldKind::Renames {
            from_field: "FIELD1".to_string(),
            thru_field: "FIELD2".to_string(),
        };
        alias.resolved_renames = Some(ResolvedRenames {
            offset: 0,
            length: 20,
            members: vec!["ROOT.FIELD1".to_string(), "ROOT.FIELD2".to_string()],
        });

        root.children = vec![field1, field2, alias];

        let schema = Schema::from_fields(vec![root]);

        // Select ALIAS - should expand to FIELD1 and FIELD2
        let projected = project_schema(&schema, &["ALIAS".to_string()]).unwrap();

        // Should have ROOT with FIELD1 and FIELD2 (not the alias itself)
        assert_eq!(projected.fields.len(), 1);
        assert_eq!(projected.fields[0].children.len(), 2);

        let child_names: Vec<&str> = projected.fields[0]
            .children
            .iter()
            .map(|f| f.name.as_str())
            .collect();
        assert!(child_names.contains(&"FIELD1"));
        assert!(child_names.contains(&"FIELD2"));
    }

    #[test]
    fn test_empty_selection() {
        let schema = create_simple_schema();
        let projected = project_schema(&schema, &[]).unwrap();

        assert_eq!(projected.fields.len(), 0);
    }

    #[test]
    fn test_collect_group_fields() {
        let mut group = Field::new(5, "GROUP".to_string());
        group.path = "GROUP".to_string();
        group.kind = FieldKind::Group;

        let mut child1 = Field::new(10, "CHILD1".to_string());
        child1.path = "GROUP.CHILD1".to_string();
        child1.kind = FieldKind::Alphanum { len: 5 };

        let mut child2 = Field::new(10, "CHILD2".to_string());
        child2.path = "GROUP.CHILD2".to_string();
        child2.kind = FieldKind::Alphanum { len: 5 };

        group.children = vec![child1, child2];

        let mut collected = HashSet::new();
        collect_group_fields(&group, &mut collected);

        assert_eq!(collected.len(), 2);
        assert!(collected.contains("GROUP.CHILD1"));
        assert!(collected.contains("GROUP.CHILD2"));
    }

    #[test]
    fn test_lrecl_preserved_even_with_tail_odo() {
        let mut root = Field::new(1, "ROOT".to_string());
        root.path = "ROOT".to_string();
        root.kind = FieldKind::Group;

        let mut counter = Field::new(5, "CTR".to_string());
        counter.path = "ROOT.CTR".to_string();
        counter.kind = FieldKind::ZonedDecimal {
            digits: 2,
            scale: 0,
            signed: false,
            sign_separate: None,
        };
        counter.len = 2;

        let mut odo_array = Field::new(5, "ITEMS".to_string());
        odo_array.path = "ROOT.ITEMS".to_string();
        odo_array.kind = FieldKind::Group;
        odo_array.occurs = Some(Occurs::ODO {
            min: 0,
            max: 5,
            counter_path: "ROOT.CTR".to_string(),
        });

        let mut item_field = Field::new(10, "ITEM_ID".to_string());
        item_field.path = "ROOT.ITEMS.ITEM_ID".to_string();
        item_field.kind = FieldKind::Alphanum { len: 3 };
        item_field.len = 3;

        odo_array.children = vec![item_field];
        root.children = vec![counter, odo_array];

        let mut schema = Schema::from_fields(vec![root]);
        schema.lrecl_fixed = Some(32);
        schema.tail_odo = Some(TailODO {
            counter_path: "ROOT.CTR".to_string(),
            min_count: 0,
            max_count: 5,
            array_path: "ROOT.ITEMS".to_string(),
        });

        let projected = project_schema(&schema, &["CTR".to_string()]).unwrap();
        assert_eq!(projected.lrecl_fixed, Some(32));
        assert!(projected.tail_odo.is_none());
    }
}