this repo has no description

chore: Adding value context to at-uri compose errors

Signed-off-by: Nick Gerakines <12125+ngerakines@users.noreply.github.com>

+107 -26
+107 -26
src/matcher.rs
··· 152 .collect::<Vec<String>>(); 153 154 if string_nodes.iter().any(|value| value == &self.expected) { 155 - let aturi = extract_aturi(self.aturi_path.as_ref(), value) 156 - .ok_or(anyhow!("matcher matched but could not create at-uri"))?; 157 Ok(MatcherResult { 158 matched: true, 159 aturi, ··· 207 .iter() 208 .any(|value| value.starts_with(&self.prefix)); 209 if found { 210 - let aturi = extract_aturi(self.aturi_path.as_ref(), value) 211 - .ok_or(anyhow!("matcher matched but could not create at-uri"))?; 212 Ok(MatcherResult { 213 matched: true, 214 aturi, ··· 278 } 279 280 if last_found != -1 && found_index == self.expected.len() - 1 { 281 - let aturi = extract_aturi(self.aturi_path.as_ref(), value) 282 - .ok_or(anyhow!("matcher matched but could not create at-uri"))?; 283 return Ok(MatcherResult { 284 matched: true, 285 aturi, ··· 325 let rkey = commit.get("rkey").and_then(|did| did.as_str())?; 326 let uri = format!("at://{}/{}/{}", did, collection, rkey); 327 return Some(uri); 328 } 329 330 None ··· 390 println!("{event:?}"); 391 let event = event.as_map_ref().map_err(|err| anyhow!(err))?; 392 393 - let commit = event.get("commit").ok_or(anyhow!("no commit on event"))?.as_map_ref().map_err(|err| anyhow!(err))?; 394 - let record = commit.get("record").ok_or(anyhow!("no record on event commit"))?.as_map_ref().map_err(|err| anyhow!(err))?; 395 396 - let rtype = record.get("$type").ok_or(anyhow!("no $type on event commit record"))?.as_immutable_string_ref().map_err(|err| anyhow!(err))?; 397 398 - if rtype.as_str() == "app.bsky.feed.post" { 399 - let did = event.get("did").ok_or(anyhow!("no did on event"))?.as_immutable_string_ref().map_err(|err| anyhow!(err))?; 400 - let collection = commit.get("collection").ok_or(anyhow!("no collection on event"))?.as_immutable_string_ref().map_err(|err| anyhow!(err))?; 401 - let rkey = commit.get("rkey").ok_or(anyhow!("no rkey on event commit"))?.as_immutable_string_ref().map_err(|err| anyhow!(err))?; 402 403 - return Ok(format!("at://{}/{}/{}", did.as_str(), collection.as_str(), rkey.as_str())); 404 } 405 - 406 - 407 - Err(anyhow!("no aturi for event")) 408 } 409 410 fn build_aturi(event: Dynamic) -> String { ··· 610 ( 611 "post1.json", 612 [ 613 - ("rhai_match_everything.rhai", true, "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadb7behk25"), 614 - ("rhai_match_type.rhai", true, "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadb7behk25"), 615 - ("rhai_match_poster.rhai", true, "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadb7behk25"), 616 ("rhai_match_reply_root.rhai", false, ""), 617 ], 618 ), 619 ( 620 "post2.json", 621 [ 622 - ("rhai_match_everything.rhai", true, "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadftr72k25"), 623 - ("rhai_match_type.rhai", true, "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadftr72k25"), 624 - ("rhai_match_poster.rhai", true, "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadftr72k25"), 625 - ("rhai_match_reply_root.rhai", true, "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadftr72k25"), 626 ], 627 ), 628 ]; ··· 640 let matcher = RhaiMatcher::new(&matcher_path.to_string_lossy()) 641 .context("could not construct matcher")?; 642 let result = matcher.matches(&value)?; 643 - assert_eq!(result.matched, matched, "matched {}: {}", input_json, matcher_file_name); 644 - assert_eq!(result.aturi, aturi, "aturi {}: {}", input_json, matcher_file_name); 645 } 646 } 647
··· 152 .collect::<Vec<String>>(); 153 154 if string_nodes.iter().any(|value| value == &self.expected) { 155 + let aturi = extract_aturi(self.aturi_path.as_ref(), value).ok_or(anyhow!( 156 + "matcher matched but could not create at-uri: {:?}", 157 + value 158 + ))?; 159 Ok(MatcherResult { 160 matched: true, 161 aturi, ··· 209 .iter() 210 .any(|value| value.starts_with(&self.prefix)); 211 if found { 212 + let aturi = extract_aturi(self.aturi_path.as_ref(), value).ok_or(anyhow!( 213 + "matcher matched but could not create at-uri: {:?}", 214 + value 215 + ))?; 216 Ok(MatcherResult { 217 matched: true, 218 aturi, ··· 282 } 283 284 if last_found != -1 && found_index == self.expected.len() - 1 { 285 + let aturi = extract_aturi(self.aturi_path.as_ref(), value).ok_or(anyhow!( 286 + "matcher matched but could not create at-uri: {:?}", 287 + value 288 + ))?; 289 return Ok(MatcherResult { 290 matched: true, 291 aturi, ··· 331 let rkey = commit.get("rkey").and_then(|did| did.as_str())?; 332 let uri = format!("at://{}/{}/{}", did, collection, rkey); 333 return Some(uri); 334 + } 335 + 336 + if Some("app.bsky.feed.like") == rtype { 337 + return event_value 338 + .get("commit") 339 + .and_then(|value| value.get("record")) 340 + .and_then(|value| value.get("subject")) 341 + .and_then(|value| value.get("uri")) 342 + .and_then(|value| value.as_str()) 343 + .map(|value| value.to_string()); 344 } 345 346 None ··· 406 println!("{event:?}"); 407 let event = event.as_map_ref().map_err(|err| anyhow!(err))?; 408 409 + let commit = event 410 + .get("commit") 411 + .ok_or(anyhow!("no commit on event"))? 412 + .as_map_ref() 413 + .map_err(|err| anyhow!(err))?; 414 + let record = commit 415 + .get("record") 416 + .ok_or(anyhow!("no record on event commit"))? 417 + .as_map_ref() 418 + .map_err(|err| anyhow!(err))?; 419 420 + let rtype = record 421 + .get("$type") 422 + .ok_or(anyhow!("no $type on event commit record"))? 423 + .as_immutable_string_ref() 424 + .map_err(|err| anyhow!(err))?; 425 426 + match rtype.as_str() { 427 + "app.bsky.feed.post" => { 428 + let did = event 429 + .get("did") 430 + .ok_or(anyhow!("no did on event"))? 431 + .as_immutable_string_ref() 432 + .map_err(|err| anyhow!(err))?; 433 + let collection = commit 434 + .get("collection") 435 + .ok_or(anyhow!("no collection on event"))? 436 + .as_immutable_string_ref() 437 + .map_err(|err| anyhow!(err))?; 438 + let rkey = commit 439 + .get("rkey") 440 + .ok_or(anyhow!("no rkey on event commit"))? 441 + .as_immutable_string_ref() 442 + .map_err(|err| anyhow!(err))?; 443 444 + Ok(format!( 445 + "at://{}/{}/{}", 446 + did.as_str(), 447 + collection.as_str(), 448 + rkey.as_str() 449 + )) 450 + } 451 + _ => Err(anyhow!("no aturi for event")), 452 } 453 } 454 455 fn build_aturi(event: Dynamic) -> String { ··· 655 ( 656 "post1.json", 657 [ 658 + ( 659 + "rhai_match_everything.rhai", 660 + true, 661 + "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadb7behk25", 662 + ), 663 + ( 664 + "rhai_match_type.rhai", 665 + true, 666 + "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadb7behk25", 667 + ), 668 + ( 669 + "rhai_match_poster.rhai", 670 + true, 671 + "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadb7behk25", 672 + ), 673 ("rhai_match_reply_root.rhai", false, ""), 674 ], 675 ), 676 ( 677 "post2.json", 678 [ 679 + ( 680 + "rhai_match_everything.rhai", 681 + true, 682 + "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadftr72k25", 683 + ), 684 + ( 685 + "rhai_match_type.rhai", 686 + true, 687 + "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadftr72k25", 688 + ), 689 + ( 690 + "rhai_match_poster.rhai", 691 + true, 692 + "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadftr72k25", 693 + ), 694 + ( 695 + "rhai_match_reply_root.rhai", 696 + true, 697 + "at://did:plc:cbkjy5n7bk3ax2wplmtjofq2/app.bsky.feed.post/3laadftr72k25", 698 + ), 699 ], 700 ), 701 ]; ··· 713 let matcher = RhaiMatcher::new(&matcher_path.to_string_lossy()) 714 .context("could not construct matcher")?; 715 let result = matcher.matches(&value)?; 716 + assert_eq!( 717 + result.matched, matched, 718 + "matched {}: {}", 719 + input_json, matcher_file_name 720 + ); 721 + assert_eq!( 722 + result.aturi, aturi, 723 + "aturi {}: {}", 724 + input_json, matcher_file_name 725 + ); 726 } 727 } 728