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
|
// Copyright (C) 2021-2022 Soni L.
// SPDX-License-Identifier: MIT OR Apache-2.0
use serde_json::Deserializer as JsonDer;
use serde::Deserialize;
//use datafu::Predicate;
//#[test]
//fn test_basic() {
// let mut der = JsonDer::from_str(r#"{"foo": 1, "bar": {"baz": 2}}"#);
// let pat = datafu::PatternBuilder::for_pattern("->[x]:?map->[yk]y").compile().unwrap();
// #[derive(Deserialize)]
// struct Values {
// x: String,
// yk: String,
// y: usize,
// }
// let matches: Values = pat.deserialize(&mut der).unwrap().collect().unwrap();
// assert_eq!(matches.x, "bar");
// assert_eq!(matches.yk, "baz");
// assert_eq!(matches.y, 2);
// assert!(der.end().is_ok());
//}
#[test]
fn test_real_use_case() {
let pat = datafu::PatternBuilder::for_pattern(
"
:map
->['projects'?]:map
->[commit:?str]:?map
->[url:?str]:?map
->[branch:?str]:?map
(->['active'?]active:?bool)?
(->['federate'?]?federate:?bool)?
(->['pinned'?]?pinned:?bool)?
"
).compile().unwrap();
let data = r#"
{"base_url": "https://ganarchy.autistic.space", "repo_list_srcs": {"https://ganarchy.autistic.space/index.toml": {"active": false}}, "projects": {"385e734a52e13949a7a5c71827f6de920dbfea43": {"https://github.com/ganarchy/GAnarchy": {"HEAD": {"active": true}}, "https://soniex2.autistic.space/git-repos/ganarchy.git": {"HEAD": {"active": true, "pinned": true}}}, "a8fb5087f79eafe312db270082c052c427b208c2": {"https://soniex2.autistic.space/git-repos/mmorfc.git": {"HEAD": {"active": true, "pinned": true}}}, "2d0b363fe3179087de59d9ef4a2d14af21d89071": {"https://soniex2.autistic.space/git-repos/chewstuff.git": {"HEAD": {"active": true, "pinned": true}}}}}
"#;
let mut der = JsonDer::from_str(data);
fn always_true() -> bool { true }
#[derive(Deserialize, PartialEq, Eq, Debug)]
struct Values {
commit: String,
url: String,
branch: String,
active: bool,
#[serde(default="always_true")]
federate: bool,
#[serde(default)]
pinned: bool,
}
let graph = pat.deserialize(&mut der).unwrap();
let matches: Vec<Values> = graph.collect().unwrap();
assert!(der.end().is_ok());
assert_eq!(matches.len(), 4);
assert_eq!(&*matches, &[
Values {
commit: "385e734a52e13949a7a5c71827f6de920dbfea43".into(),
url: "https://github.com/ganarchy/GAnarchy".into(),
branch: "HEAD".into(),
active: true,
federate: true,
pinned: false,
},
Values {
commit: "385e734a52e13949a7a5c71827f6de920dbfea43".into(),
url: "https://soniex2.autistic.space/git-repos/ganarchy.git".into(),
branch: "HEAD".into(),
active: true,
federate: true,
pinned: true,
},
Values {
commit: "a8fb5087f79eafe312db270082c052c427b208c2".into(),
url: "https://soniex2.autistic.space/git-repos/mmorfc.git".into(),
branch: "HEAD".into(),
active: true,
federate: true,
pinned: true,
},
Values {
commit: "2d0b363fe3179087de59d9ef4a2d14af21d89071".into(),
url: "https://soniex2.autistic.space/git-repos/chewstuff.git".into(),
branch: "HEAD".into(),
active: true,
federate: true,
pinned: true,
},
]);
}
//#[test]
//fn test_str() {
// //let tree = Value::M(vec![
// // ("foo".into(), Value::U(1)),
// // ("bar".into(), Value::M(vec![
// // ("baz".into(), Value::U(2)),
// // ].into_iter().collect())),
// //].into_iter().collect());
// let der = JsonDer::from_str(r#"{"foo": 1, "bar": {"baz": 2}}"#)
// let preds = vec![("dict", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::M(_)))) as Box<datafu::Predicate<Value>>)].into_iter().collect();
// let pat = datafu::Pattern::<Value>::compile::<&str, &str>("->X'bar'->Y", Some(preds), None).unwrap();
// let mut matcher = pat.attempt_match(&tree);
// let m = matcher.next().unwrap().unwrap();
// assert_eq!(m["X"].0, RefOwn::Ref(&Value::from("bar")));
// assert_eq!(m["Y"].0, RefOwn::Ref(&Value::from("baz")));
// assert_eq!(m["Y"].1, RefOwn::Ref(&Value::U(2)));
// assert!(matcher.next().is_none());
//}
//
//#[test]
//fn test_basic_2() {
// let tree = Value::M(vec![
// ("projects".into(), Value::M(vec![
// ("385e734a52e13949a7a5c71827f6de920dbfea43".into(), Value::M(vec![
// ("https://soniex2.autistic.space/git-repos/ganarchy.git".into(), Value::M(vec![
// ("HEAD".into(), Value::M(vec![
// ("active".into(), Value::B(true)),
// ].into_iter().collect())),
// ].into_iter().collect())),
// ].into_iter().collect())),
// ].into_iter().collect())),
// ].into_iter().collect());
// let preds = vec![("d", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::M(_)))) as Box<datafu::Predicate<Value>>)].into_iter().collect();
// let pat = datafu::Pattern::<Value>::compile::<&str, &str>("->'projects':?$d->P/[0-9a-fA-F]{40}|[0-9a-fA-F]{64}/?:?$d->U:?$d->B", Some(preds), None).unwrap();
// let mut matcher = pat.attempt_match(&tree);
// let m = matcher.next().unwrap().unwrap();
// assert_eq!(m["P"].0, RefOwn::Ref(&Value::from("385e734a52e13949a7a5c71827f6de920dbfea43")));
// assert_eq!(m["U"].0, RefOwn::Ref(&Value::from("https://soniex2.autistic.space/git-repos/ganarchy.git")));
// assert_eq!(m["B"].0, RefOwn::Ref(&Value::from("HEAD")));
// assert_eq!(m["B"].1, RefOwn::Ref(&Value::M(vec![(Value::from("active"), Value::B(true))].into_iter().collect())));
// assert!(matcher.next().is_none());
//}
//
//#[test]
//fn test_spaces() {
// let tree = Value::M(vec![
// ("projects".into(), Value::M(vec![
// ("385e734a52e13949a7a5c71827f6de920dbfea43".into(), Value::M(vec![
// ("https://soniex2.autistic.space/git-repos/ganarchy.git".into(), Value::M(vec![
// ("HEAD".into(), Value::M(vec![
// ("active".into(), Value::B(true)),
// ].into_iter().collect())),
// ].into_iter().collect())),
// ].into_iter().collect())),
// ].into_iter().collect())),
// ].into_iter().collect());
// let preds = vec![("dict", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::M(_)))) as Box<datafu::Predicate<Value>>)].into_iter().collect();
// let pat = datafu::Pattern::<Value>::compile::<_, &str>("
// -> 'projects'?
// -> commit /[0-9a-fA-F]{40}|[0-9a-fA-F]{64}/? :?$dict
// -> url :?$dict
// -> branch :?$dict",
// Some(preds),
// None,
// ).unwrap();
// let mut matcher = pat.attempt_match(&tree);
// let m = matcher.next().unwrap().unwrap();
// assert_eq!(m["commit"].0, RefOwn::Ref(&Value::from("385e734a52e13949a7a5c71827f6de920dbfea43")));
// assert_eq!(m["url"].0, RefOwn::Ref(&Value::from("https://soniex2.autistic.space/git-repos/ganarchy.git")));
// assert_eq!(m["branch"].0, RefOwn::Ref(&Value::from("HEAD")));
// assert_eq!(m["branch"].1, RefOwn::Ref(&Value::M(vec![(Value::from("active"), Value::B(true))].into_iter().collect())));
// assert!(matcher.next().is_none());
//}
//
//#[test]
//fn test_harder() {
// let tree = Value::M(vec![
// ("projects".into(), Value::M(vec![
// ("385e734a52e13949a7a5c71827f6de920dbfea43".into(), Value::M(vec![
// ("https://soniex2.autistic.space/git-repos/ganarchy.git".into(), Value::M(vec![
// ("HEAD".into(), Value::M(vec![
// ("active".into(), Value::B(true)),
// ].into_iter().collect())),
// ].into_iter().collect())),
// ].into_iter().collect())),
// ].into_iter().collect())),
// ].into_iter().collect());
// let preds = vec![
// ("dict", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::M(_)))) as Box<datafu::Predicate<Value>>),
// ("str", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::S(_)))) as Box<datafu::Predicate<Value>>),
// ("bool", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::B(_)))) as Box<datafu::Predicate<Value>>),
// // stubs, we don't particularly need to test these
// ("commit", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::S(_)))) as Box<datafu::Predicate<Value>>),
// ("uri", Box::new(|v: RefOwn<'_, _, _>| matches!(v, RefOwn::Ref(&Value::S(_)))) as Box<datafu::Predicate<Value>>),
// ].into_iter().collect();
// let pat = datafu::Pattern::<Value>::compile::<_, &str>("
// ->'projects':$dict
// ->commit[:?$str:?$commit]:?$dict
// ->url[:?$str:?$uri]:?$dict
// ->branch:?$dict
// (->active'active'?:?$bool)
// (->federate'federate'?:?$bool)?",
// Some(preds),
// None,
// ).unwrap();
// let mut matcher = pat.attempt_match(&tree);
// let m = matcher.next().unwrap().unwrap();
// assert_eq!(m["commit"].0, RefOwn::Ref(&Value::from("385e734a52e13949a7a5c71827f6de920dbfea43")));
// assert_eq!(m["url"].0, RefOwn::Ref(&Value::from("https://soniex2.autistic.space/git-repos/ganarchy.git")));
// assert_eq!(m["branch"].0, RefOwn::Ref(&Value::from("HEAD")));
// assert_eq!(m["active"].1, RefOwn::Ref(&Value::B(true)));
// assert_eq!(m.get("federate"), None);
// assert!(matcher.next().is_none());
//}
|