package_boundary.rs (24446B)
1 use std::collections::BTreeSet; 2 use std::fs; 3 use std::path::{Path, PathBuf}; 4 5 #[allow(unused_imports)] 6 use radroots_nostr::{ 7 Error as _, 8 event::{Coordinate as _, Event as _, EventId as _, Kind as _, Metadata as _, Timestamp as _}, 9 filter::Filter as _, 10 key as _, 11 tag::{Tag as _, TagKind as _, TagStandard as _}, 12 }; 13 14 const MANIFEST: &str = include_str!("../Cargo.toml"); 15 const ROOT: &str = include_str!("../src/lib.rs"); 16 const EVENT_MODULE: &str = include_str!("../src/event.rs"); 17 const FILTER_MODULE: &str = include_str!("../src/filter.rs"); 18 const KEY_MODULE: &str = include_str!("../src/key.rs"); 19 const NIP17_MODULE: &str = include_str!("../src/nip17.rs"); 20 const NIP17_OPENING: &str = include_str!("../src/nip17/opening.rs"); 21 #[cfg(feature = "nip17")] 22 const PRIVATE_OPENING_CONTRACT: &str = 23 include_str!("../../../contracts/protocol/private_opening.v1.json"); 24 #[cfg(feature = "commerce")] 25 const COMMERCE_INGRESS_CONTRACT: &str = 26 include_str!("../../../contracts/protocol/commerce_ingress.v1.json"); 27 const BLOSSOM_MODULE: &str = include_str!("../src/blossom.rs"); 28 const README: &str = include_str!("../README.md"); 29 const SIGNING_MODULE: &str = include_str!("../src/signing.rs"); 30 const TAG_MODULE: &str = include_str!("../src/tag.rs"); 31 const TYPES_MODULE: &str = include_str!("../src/types.rs"); 32 const PUBLIC_API: &str = include_str!("../../../contracts/api_baselines/radroots_nostr.txt"); 33 const IDENTITY_MANIFEST: &str = include_str!("../../identity/Cargo.toml"); 34 const IDENTITY_KEY_MODULE: &str = include_str!("../../identity/src/key.rs"); 35 const TRANSPORT_MANIFEST: &str = include_str!("../../transport_nostr/Cargo.toml"); 36 const TRANSPORT_ROOT: &str = include_str!("../../transport_nostr/src/lib.rs"); 37 38 #[test] 39 fn manifest_has_final_identity_features_and_radroots_dependencies() { 40 for required in [ 41 "name = \"radroots_nostr\"", 42 "version = \"0.1.0-alpha\"", 43 "publish = [\"crates-io\"]", 44 "[lib]\nname = \"radroots_nostr\"", 45 "default = [\"std\", \"events\"]", 46 "radroots_identity = { workspace = true, default-features = false }", 47 "nostr = { workspace = true, default-features = false, features = [", 48 "\"nostr/std\"", 49 "\"radroots_event/std\"", 50 "\"radroots_event_codec/std\"", 51 "\"radroots_identity/std\"", 52 ] { 53 assert!( 54 MANIFEST.contains(required), 55 "manifest is missing `{required}`" 56 ); 57 } 58 59 let dependencies = table_keys(MANIFEST, "[dependencies]"); 60 for required in [ 61 "radroots_identity", 62 "radroots_event", 63 "radroots_event_codec", 64 ] { 65 let declaration = table_value(MANIFEST, "[dependencies]", required) 66 .unwrap_or_else(|| panic!("missing required dependency `{required}`")); 67 assert!( 68 !declaration.contains("optional = true"), 69 "`{required}` must be required" 70 ); 71 } 72 for optional in ["radroots_signing", "radroots_blossom", "radroots_protocol"] { 73 let declaration = table_value(MANIFEST, "[dependencies]", optional) 74 .unwrap_or_else(|| panic!("missing optional dependency `{optional}`")); 75 assert!( 76 declaration.contains("optional = true"), 77 "`{optional}` must be optional" 78 ); 79 } 80 assert_eq!( 81 dependencies 82 .iter() 83 .copied() 84 .filter(|dependency| dependency.starts_with("radroots_")) 85 .collect::<BTreeSet<_>>(), 86 BTreeSet::from([ 87 "radroots_blossom", 88 "radroots_event", 89 "radroots_event_codec", 90 "radroots_identity", 91 "radroots_protocol", 92 "radroots_signing", 93 ]) 94 ); 95 96 for forbidden in [ 97 "keyring", 98 "nostr-sdk", 99 "nostr_sdk", 100 "radroots_outbox", 101 "radroots_storage", 102 "radroots_transport", 103 "reqwest", 104 "sqlx", 105 "tokio", 106 ] { 107 assert!( 108 !dependencies.contains(forbidden), 109 "portable Nostr adapter must not depend on `{forbidden}`" 110 ); 111 } 112 113 let features = table_keys(MANIFEST, "[features]"); 114 assert_eq!( 115 features, 116 BTreeSet::from([ 117 "blossom", 118 "commerce", 119 "default", 120 "events", 121 "nip17", 122 "private_ingress", 123 "public_ingress", 124 "signing", 125 "std" 126 ]), 127 "manifest feature vocabulary must match the base charter and commerce ingress contract" 128 ); 129 for feature in features { 130 let declaration = table_value(MANIFEST, "[features]", feature) 131 .unwrap_or_else(|| panic!("missing feature declaration `{feature}`")); 132 for forbidden in [ 133 "client", 134 "http", 135 "network", 136 "relay", 137 "reqwest", 138 "runtime", 139 "transport", 140 ] { 141 assert!( 142 !declaration.contains(forbidden), 143 "feature `{feature}` activates forbidden live-I/O authority `{forbidden}`" 144 ); 145 } 146 } 147 } 148 149 #[test] 150 fn crate_root_establishes_the_final_public_module_skeleton() { 151 for module in [ 152 "blossom", 153 "commerce", 154 "event", 155 "filter", 156 "key", 157 "nip17", 158 "private_ingress", 159 "public_ingress", 160 "signing", 161 "tag", 162 ] { 163 assert!( 164 ROOT.contains(&format!("pub mod {module};")), 165 "crate root is missing final module `{module}`" 166 ); 167 } 168 let root_reexports = ROOT 169 .lines() 170 .map(str::trim) 171 .filter(|line| line.starts_with("pub use ")) 172 .collect::<Vec<_>>(); 173 assert_eq!( 174 root_reexports, 175 ["pub use error::Error;"], 176 "the crate root must re-export only the canonical Error alias" 177 ); 178 assert!( 179 !ROOT.contains("pub mod prelude"), 180 "lower-level Nostr crate must not publish a broad prelude" 181 ); 182 for retired in [ 183 "codec_adapters", 184 "draft_signing", 185 "error", 186 "event_adapters", 187 "event_verify", 188 "events", 189 "job_adapter", 190 "types", 191 "util", 192 ] { 193 assert!( 194 !ROOT.contains(&format!("pub mod {retired};")), 195 "superseded module remains public: `{retired}`" 196 ); 197 } 198 } 199 200 #[test] 201 fn protocol_values_are_exposed_only_at_explicit_adapter_modules() { 202 for (module, aliases) in [ 203 ( 204 EVENT_MODULE, 205 [ 206 "pub type Coordinate", 207 "pub type Event", 208 "pub type EventId", 209 "pub type Kind", 210 "pub type Metadata", 211 "pub type Timestamp", 212 ] 213 .as_slice(), 214 ), 215 (FILTER_MODULE, ["pub type Filter"].as_slice()), 216 ( 217 TAG_MODULE, 218 ["pub type Tag", "pub type TagKind", "pub type TagStandard"].as_slice(), 219 ), 220 ] { 221 for alias in aliases { 222 assert!( 223 module.contains(alias), 224 "explicit adapter module is missing `{alias}`" 225 ); 226 } 227 } 228 229 for forbidden in [ 230 "pub type RadrootsNostrCoordinate", 231 "pub type RadrootsNostrEvent", 232 "pub type RadrootsNostrEventId", 233 "pub type RadrootsNostrFilter", 234 "pub type RadrootsNostrKind", 235 "pub type RadrootsNostrMetadata", 236 "pub type RadrootsNostrTag", 237 "pub type RadrootsNostrTagKind", 238 "pub type RadrootsNostrTagStandard", 239 "pub type RadrootsNostrTimestamp", 240 ] { 241 assert!( 242 !TYPES_MODULE.contains(forbidden), 243 "broad predecessor alias remains public in types: `{forbidden}`" 244 ); 245 } 246 247 assert!(ROOT.contains("mod event_convert;")); 248 assert!(ROOT.contains("mod tags;")); 249 assert!(!ROOT.contains("pub mod event_convert;")); 250 assert!(!ROOT.contains("pub mod tags;")); 251 } 252 253 #[test] 254 fn live_client_and_http_ownership_belongs_to_transport_nostr() { 255 for forbidden in [ 256 "nostr-sdk", 257 "reqwest", 258 "client =", 259 "http =", 260 "radroots_nostr/client", 261 ] { 262 assert!( 263 !MANIFEST.contains(forbidden), 264 "portable manifest still owns forbidden live-client surface `{forbidden}`" 265 ); 266 } 267 268 for forbidden in ["pub mod client;", "pub mod relays;", "pub mod nip11;"] { 269 assert!( 270 !ROOT.contains(forbidden), 271 "portable crate root still exposes live-client module `{forbidden}`" 272 ); 273 } 274 275 for required in ["nostr-sdk", "radroots_transport", "radroots_event_codec"] { 276 assert!( 277 TRANSPORT_MANIFEST.contains(required), 278 "transport manifest is missing live-client ownership marker `{required}`" 279 ); 280 } 281 for required in [ 282 "mod auth;", 283 "mod client;", 284 "mod relay;", 285 "mod sink;", 286 "mod source;", 287 "mod status;", 288 "pub use client::{Config, NostrTransport, ReconnectBackoff};", 289 "pub use relay::{RelayUrl, RelayUrlPolicy};", 290 ] { 291 assert!( 292 TRANSPORT_ROOT.contains(required), 293 "transport crate root is missing live-client owner `{required}`" 294 ); 295 } 296 297 let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); 298 assert!(!manifest_dir.join("src/client.rs").exists()); 299 assert!(!manifest_dir.join("src/relays.rs").exists()); 300 assert!(!manifest_dir.join("src/nip11.rs").exists()); 301 assert!( 302 manifest_dir 303 .join("../transport_nostr/src/client.rs") 304 .exists() 305 ); 306 assert!( 307 !manifest_dir 308 .join("../transport_nostr/src/relays.rs") 309 .exists() 310 ); 311 assert!( 312 !manifest_dir 313 .join("../transport_nostr/src/nip11.rs") 314 .exists() 315 ); 316 assert!(!TRANSPORT_MANIFEST.contains("reqwest")); 317 318 let forbidden_source = [ 319 "nostr_sdk", 320 "reqwest::", 321 "feature = \"client\"", 322 "feature = \"http\"", 323 "crate::client", 324 ]; 325 for source_path in rust_sources(&manifest_dir.join("src")) { 326 let source = fs::read_to_string(&source_path) 327 .unwrap_or_else(|error| panic!("failed to read {}: {error}", source_path.display())); 328 for forbidden in forbidden_source { 329 assert!( 330 !source.contains(forbidden), 331 "{} still contains live-client ownership marker `{forbidden}`", 332 source_path.display() 333 ); 334 } 335 } 336 } 337 338 #[test] 339 fn nostr_key_conversion_is_explicit_and_identity_remains_public_only() { 340 for required in [ 341 "nostr/nip49", 342 "pub fn public_key_to_nostr", 343 "pub fn public_key_from_nostr", 344 "pub fn public_key_to_npub", 345 "pub fn public_key_from_npub", 346 "pub fn parse_public_key", 347 "pub fn parse_secret_key", 348 "pub fn secret_key_to_nsec", 349 "pub fn encrypt_secret_key_nip49", 350 "pub fn encrypt_secret_key_nip49_with_options", 351 "pub fn decrypt_secret_key_nip49", 352 ] { 353 let authority = if required == "nostr/nip49" { 354 MANIFEST 355 } else { 356 KEY_MODULE 357 }; 358 assert!( 359 authority.contains(required), 360 "Nostr key authority is missing `{required}`" 361 ); 362 } 363 364 for forbidden in ["nostr =", "nip49", "nsec", "ncryptsec", "SecretKey"] { 365 assert!( 366 !IDENTITY_MANIFEST.contains(forbidden), 367 "identity manifest regained Nostr secret ownership `{forbidden}`" 368 ); 369 assert!( 370 !IDENTITY_KEY_MODULE.contains(forbidden), 371 "identity key module regained Nostr secret ownership `{forbidden}`" 372 ); 373 } 374 375 for secret_function in [ 376 "parse_secret_key", 377 "secret_key_to_nsec", 378 "encrypt_secret_key_nip49", 379 "encrypt_secret_key_nip49_with_options", 380 "decrypt_secret_key_nip49", 381 ] { 382 let signature = format!("pub fn {secret_function}"); 383 let position = KEY_MODULE 384 .find(&signature) 385 .unwrap_or_else(|| panic!("missing secret adapter `{secret_function}`")); 386 let prefix = &KEY_MODULE[..position]; 387 let nearby = &prefix[prefix.len().saturating_sub(160)..]; 388 assert!( 389 nearby.contains("#[cfg(feature = \"signing\")]"), 390 "secret adapter `{secret_function}` is not signing-gated" 391 ); 392 } 393 } 394 395 #[test] 396 fn local_signer_consumes_only_the_opaque_secret_boundary() { 397 for required in [ 398 "pub fn new(secret_key: SecretKey)", 399 "pub fn generate()", 400 "pub const fn public_key(&self) -> PublicKey", 401 "key::SecretKey", 402 ] { 403 assert!( 404 SIGNING_MODULE.contains(required), 405 "local signer boundary is missing `{required}`" 406 ); 407 } 408 for forbidden in [ 409 "pub const fn new(keys: RadrootsNostrKeys)", 410 "pub fn new(keys: RadrootsNostrKeys)", 411 "pub fn keys(", 412 "pub fn secret_key(", 413 ] { 414 assert!( 415 !SIGNING_MODULE.contains(forbidden), 416 "local signer leaks an upstream representation: `{forbidden}`" 417 ); 418 } 419 } 420 421 #[test] 422 fn focused_nip_and_blossom_features_own_no_network_operations() { 423 for required in [ 424 "blossom = [\"events\", \"dep:base64\", \"dep:radroots_blossom\"]", 425 "nip17 = [\"events\", \"nostr/nip44\", \"nostr/nip59\", \"dep:base64\", \"radroots_protocol/chat\"]", 426 ] { 427 assert!( 428 MANIFEST.contains(required), 429 "focused feature contract is missing `{required}`" 430 ); 431 } 432 for required in [ 433 "pub async fn wrap_message<T>(", 434 "pub async fn wrap_message_file<T>(", 435 "pub async fn unwrap_gift_wrap<T>(", 436 "pub const fn code(&self) -> &'static str", 437 ] { 438 assert!( 439 NIP17_MODULE.contains(required), 440 "NIP-17 adapter is missing `{required}`" 441 ); 442 } 443 for required in [ 444 "pub fn sign_authorization(", 445 "pub fn encode_authorization_header(", 446 "pub fn decode_verify_authorization_header(", 447 ] { 448 assert!( 449 BLOSSOM_MODULE.contains(required), 450 "Blossom adapter is missing `{required}`" 451 ); 452 } 453 for (name, source) in [ 454 ("NIP-17", NIP17_MODULE), 455 ("NIP-17 opening", NIP17_OPENING), 456 ("Blossom", BLOSSOM_MODULE), 457 ] { 458 for forbidden in [ 459 "nostr_sdk", 460 "reqwest::", 461 "tokio::spawn", 462 "std::net", 463 "TcpStream", 464 "UdpSocket", 465 "RelayPool", 466 ] { 467 assert!( 468 !source.contains(forbidden), 469 "{name} adapter owns forbidden network operation `{forbidden}`" 470 ); 471 } 472 } 473 let readme_words = README.split_whitespace().collect::<Vec<_>>().join(" "); 474 for required in [ 475 "This crate owns no relay client", 476 "It does not select relays, deliver events, retry operations, or persist message state.", 477 ] { 478 assert!( 479 readme_words.contains(required), 480 "README is missing focused ownership statement `{required}`" 481 ); 482 } 483 for forbidden in [ 484 "Portable relay-client lifecycle", 485 "With the `client` feature", 486 ] { 487 assert!( 488 !README.contains(forbidden), 489 "README retains removed network ownership statement `{forbidden}`" 490 ); 491 } 492 } 493 494 #[cfg(feature = "nip17")] 495 #[test] 496 fn private_opening_contract_preserves_bounded_protocol_only_scope() { 497 let contract: serde_json::Value = serde_json::from_str(PRIVATE_OPENING_CONTRACT).unwrap(); 498 assert_eq!( 499 contract["signed_max_utf8_bytes"], 500 radroots_protocol::chat::SIGNED_MAX_BYTES 501 ); 502 assert_eq!( 503 contract["rumor_max_utf8_bytes"], 504 radroots_protocol::chat::RUMOR_MAX_BYTES 505 ); 506 assert_eq!( 507 contract["supported_rumor_kinds"], 508 serde_json::json!([14, 15]) 509 ); 510 assert!(!NIP17_MODULE.contains("nip59::extract_rumor(signer, gift_wrap)")); 511 assert!(NIP17_MODULE.contains("opening::open(signer, gift_wrap)")); 512 assert!( 513 contract["limits"] 514 .as_array() 515 .unwrap() 516 .iter() 517 .any(|v| v.as_str().unwrap().contains("raw outer ingress")) 518 ); 519 } 520 521 #[cfg(feature = "commerce")] 522 #[test] 523 fn commerce_ingress_contract_binds_complete_budgets_and_independent_packaged_vectors() { 524 use radroots_protocol::profile; 525 let contract: serde_json::Value = serde_json::from_str(COMMERCE_INGRESS_CONTRACT).unwrap(); 526 assert_eq!(contract["schema"], "radroots.nostr.commerce_ingress.v1"); 527 assert_eq!( 528 contract["content_max_utf8_bytes"], 529 profile::PRIVATE_RECORD_MAX_BYTES 530 ); 531 assert_eq!( 532 contract["rumor_max_utf8_bytes"], 533 profile::UNSIGNED_RUMOR_MAX_BYTES 534 ); 535 assert_eq!( 536 contract["signed_max_utf8_bytes"], 537 profile::SIGNED_EVENT_MAX_BYTES 538 ); 539 assert_eq!(contract["carrier"]["rumor_kind"], profile::COMMERCE_KIND); 540 assert_eq!(contract["limits"]["structured_execution_enabled"], false); 541 let fixture: serde_json::Value = 542 serde_json::from_str(include_str!("fixtures/commerce_envelopes.v1.json")).unwrap(); 543 assert_eq!( 544 fixture["positives"].as_array().unwrap().len(), 545 contract["independent_vectors"]["positives"] 546 .as_u64() 547 .unwrap() as usize 548 ); 549 assert_eq!( 550 fixture["negatives"].as_array().unwrap().len(), 551 contract["independent_vectors"]["negative_or_unsupported"] 552 .as_u64() 553 .unwrap() as usize 554 ); 555 assert_eq!(fixture["producer"]["primary_nip44_reference_equal"], true); 556 assert_eq!(fixture["producer"]["production_entropy_claimed"], false); 557 assert!(!MANIFEST.contains("node_modules")); 558 } 559 560 #[test] 561 fn production_api_declares_no_traits_or_host_runtime_implementations() { 562 let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); 563 let mut public_traits = BTreeSet::new(); 564 565 for source_path in rust_sources(&manifest_dir.join("src")) { 566 let source = fs::read_to_string(&source_path) 567 .unwrap_or_else(|error| panic!("failed to read {}: {error}", source_path.display())); 568 let production = source.split("\n#[cfg(test)]").next().unwrap_or(&source); 569 for line in production.lines() { 570 let trimmed = line.trim_start(); 571 if let Some(name) = trimmed 572 .strip_prefix("pub trait ") 573 .and_then(|rest| rest.split([':', '<', ' ']).next()) 574 { 575 public_traits.insert(name.to_owned()); 576 } 577 for forbidden in [ 578 "nostr_sdk::", 579 "reqwest::", 580 "sqlx::", 581 "tokio::spawn", 582 "std::net::", 583 "std::thread::spawn", 584 ] { 585 assert!( 586 !trimmed.contains(forbidden), 587 "{} owns forbidden host/runtime implementation `{forbidden}`: {trimmed}", 588 source_path.display() 589 ); 590 } 591 } 592 } 593 594 assert!( 595 public_traits.is_empty(), 596 "concrete protocol adapter must not publish an SPI trait: {public_traits:?}" 597 ); 598 } 599 600 #[test] 601 fn workspace_consumers_do_not_use_superseded_nostr_alias_paths() { 602 let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); 603 let crates_dir = manifest_dir 604 .parent() 605 .expect("Nostr crate must have a crates directory") 606 .to_path_buf(); 607 608 for source_path in rust_sources(&crates_dir) { 609 if source_path.starts_with(&manifest_dir) { 610 continue; 611 } 612 let source = fs::read_to_string(&source_path) 613 .unwrap_or_else(|error| panic!("failed to read {}: {error}", source_path.display())); 614 for forbidden in [ 615 "radroots_nostr::codec_adapters::", 616 "radroots_nostr::draft_signing::", 617 "radroots_nostr::error::", 618 "radroots_nostr::event_adapters::", 619 "radroots_nostr::event_verify::", 620 "radroots_nostr::events::", 621 "radroots_nostr::job_adapter::", 622 "radroots_nostr::prelude", 623 "radroots_nostr::types::", 624 "radroots_nostr::util::", 625 ] { 626 assert!( 627 !source.contains(forbidden), 628 "{} still imports superseded Nostr path `{forbidden}`", 629 source_path.display() 630 ); 631 } 632 } 633 } 634 635 #[test] 636 fn superseded_surface_retirement_is_explicit_and_release_bounded() { 637 assert!(!MANIFEST.contains("codec = []")); 638 for forbidden in ["pub fn radroots_nostr_", "pub async fn radroots_nostr_"] { 639 for source_path in rust_sources(&PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src")) { 640 let source = fs::read_to_string(&source_path).unwrap_or_else(|error| { 641 panic!("failed to read {}: {error}", source_path.display()) 642 }); 643 assert!( 644 !source.contains(forbidden), 645 "{} retains prefixed public item `{forbidden}`", 646 source_path.display() 647 ); 648 } 649 } 650 assert!(MANIFEST.contains("publish = [\"crates-io\"]")); 651 assert!( 652 !Path::new(env!("CARGO_MANIFEST_DIR")) 653 .join("COMPATIBILITY.md") 654 .exists(), 655 "the completed migration record must not ship as an active compatibility contract" 656 ); 657 658 let public_modules = PUBLIC_API 659 .lines() 660 .filter_map(|line| line.strip_prefix("pub mod radroots_nostr::")) 661 .collect::<BTreeSet<_>>(); 662 assert_eq!( 663 public_modules, 664 BTreeSet::from([ 665 "blossom", 666 "commerce", 667 "event", 668 "filter", 669 "key", 670 "nip17", 671 "private_ingress", 672 "public_ingress", 673 "signing", 674 "signing::gift_wrap", 675 "tag" 676 ]), 677 "reviewed API baseline must expose only chartered modules" 678 ); 679 for forbidden in [ 680 "::RadrootsNostr", 681 "::radroots_nostr_", 682 "pub radroots_nostr::event::ApplicationHandlerSpec::", 683 "pub radroots_nostr::nip17::WrapOptions::", 684 ] { 685 assert!( 686 !PUBLIC_API.contains(forbidden), 687 "reviewed API baseline retains forbidden surface `{forbidden}`" 688 ); 689 } 690 for required in [ 691 "pub fn radroots_nostr::blossom::SignedAuthorization::from_signed_event", 692 "pub fn radroots_nostr::blossom::encode_signed_event_authorization_header", 693 ] { 694 assert!( 695 PUBLIC_API.contains(required), 696 "reviewed API baseline is missing `{required}`" 697 ); 698 } 699 } 700 701 fn rust_sources(root: &Path) -> Vec<PathBuf> { 702 let mut pending = vec![root.to_path_buf()]; 703 let mut sources = Vec::new(); 704 while let Some(directory) = pending.pop() { 705 for entry in fs::read_dir(&directory) 706 .unwrap_or_else(|error| panic!("failed to read {}: {error}", directory.display())) 707 { 708 let path = entry 709 .expect("source directory entry must be readable") 710 .path(); 711 if path.is_dir() { 712 pending.push(path); 713 } else if path.extension().is_some_and(|extension| extension == "rs") { 714 sources.push(path); 715 } 716 } 717 } 718 sources 719 } 720 721 fn table_keys<'a>(source: &'a str, header: &str) -> BTreeSet<&'a str> { 722 table_lines(source, header) 723 .filter_map(|line| line.split_once('=').map(|(key, _)| key.trim())) 724 .collect() 725 } 726 727 fn table_value<'a>(source: &'a str, header: &str, key: &str) -> Option<&'a str> { 728 table_lines(source, header).find_map(|line| { 729 let (candidate, value) = line.split_once('=')?; 730 (candidate.trim() == key).then_some(value.trim()) 731 }) 732 } 733 734 fn table_lines<'a>(source: &'a str, header: &str) -> impl Iterator<Item = &'a str> { 735 let start = source 736 .find(header) 737 .unwrap_or_else(|| panic!("missing table `{header}`")); 738 source[start + header.len()..] 739 .lines() 740 .skip_while(|line| line.trim().is_empty()) 741 .take_while(|line| !line.trim_start().starts_with('[')) 742 .filter(|line| !line.trim().is_empty() && !line.trim_start().starts_with('#')) 743 }