dcbor_pattern/pattern/structure/
structure_pattern.rs1use super::{ArrayPattern, MapPattern, TaggedPattern};
2use crate::pattern::{Matcher, Path, Pattern, vm::Instr};
3use dcbor::prelude::*;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum StructurePattern {
7 Array(ArrayPattern),
8 Map(MapPattern),
9 Tagged(TaggedPattern),
10}
11
12impl Matcher for StructurePattern {
13 fn paths(&self, haystack: &CBOR) -> Vec<Path> {
14 match self {
15 StructurePattern::Array(pattern) => pattern.paths(haystack),
16 StructurePattern::Map(pattern) => pattern.paths(haystack),
17 StructurePattern::Tagged(pattern) => pattern.paths(haystack),
18 }
19 }
20
21 fn compile(
22 &self,
23 code: &mut Vec<Instr>,
24 literals: &mut Vec<Pattern>,
25 captures: &mut Vec<String>,
26 ) {
27 match self {
28 StructurePattern::Array(pattern) => {
29 pattern.compile(code, literals, captures)
30 }
31 StructurePattern::Map(pattern) => {
32 pattern.compile(code, literals, captures)
33 }
34 StructurePattern::Tagged(pattern) => {
35 pattern.compile(code, literals, captures)
36 }
37 }
38 }
39
40 fn collect_capture_names(&self, names: &mut Vec<String>) {
41 match self {
42 StructurePattern::Array(pattern) => {
43 pattern.collect_capture_names(names)
44 }
45 StructurePattern::Map(pattern) => {
46 pattern.collect_capture_names(names)
47 }
48 StructurePattern::Tagged(pattern) => {
49 pattern.collect_capture_names(names)
50 }
51 }
52 }
53
54 fn paths_with_captures(
55 &self,
56 haystack: &CBOR,
57 ) -> (Vec<Path>, std::collections::HashMap<String, Vec<Path>>) {
58 match self {
59 StructurePattern::Array(pattern) => {
60 pattern.paths_with_captures(haystack)
61 }
62 StructurePattern::Map(pattern) => pattern.paths_with_captures(haystack),
63 StructurePattern::Tagged(pattern) => {
64 pattern.paths_with_captures(haystack)
65 }
66 }
67 }
68}
69
70impl std::fmt::Display for StructurePattern {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 match self {
73 StructurePattern::Array(pattern) => write!(f, "{}", pattern),
74 StructurePattern::Map(pattern) => write!(f, "{}", pattern),
75 StructurePattern::Tagged(pattern) => write!(f, "{}", pattern),
76 }
77 }
78}