| 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 |
use quasi_declare::declare; |
| 35 |
use tokio::runtime::Handle; |
| 36 |
|
| 37 |
use axum::extract::FromRef; |
| 38 |
|
| 39 |
use crate::AppState; |
| 40 |
use crate::auth::SessionUser; |
| 41 |
pub mod auth_pages; |
| 42 |
|
| 43 |
pub mod buyer_contacts; |
| 44 |
pub mod cart_act; |
| 45 |
pub mod clip_acts; |
| 46 |
pub mod collections; |
| 47 |
pub mod creators; |
| 48 |
pub mod custom_page; |
| 49 |
pub mod discover_search; |
| 50 |
pub mod discover_typeahead; |
| 51 |
pub mod embeds; |
| 52 |
pub mod export_act; |
| 53 |
pub mod export_portal; |
| 54 |
pub mod fan_plus; |
| 55 |
pub mod feeds; |
| 56 |
pub mod follow; |
| 57 |
pub mod forum_memberships; |
| 58 |
pub mod git_blame; |
| 59 |
pub mod git_browse; |
| 60 |
pub mod git_commit; |
| 61 |
pub mod git_explore; |
| 62 |
pub mod git_repos; |
| 63 |
pub mod item_files; |
| 64 |
pub mod item_sales; |
| 65 |
pub mod item_tabs; |
| 66 |
pub mod library_acts; |
| 67 |
pub mod library_contacts; |
| 68 |
pub mod library_tabs; |
| 69 |
pub mod license_key_act; |
| 70 |
pub mod link_remove_act; |
| 71 |
pub mod literal; |
| 72 |
pub mod media_picker; |
| 73 |
pub mod payout_summary; |
| 74 |
pub mod policy; |
| 75 |
pub mod pricing; |
| 76 |
pub mod project; |
| 77 |
pub mod project_analytics; |
| 78 |
pub mod project_blog; |
| 79 |
pub mod project_content; |
| 80 |
pub mod project_members; |
| 81 |
pub mod project_overview; |
| 82 |
pub mod project_tabs; |
| 83 |
pub mod promo_code_acts; |
| 84 |
pub mod repo_acts; |
| 85 |
pub mod residuals; |
| 86 |
pub mod rich_field; |
| 87 |
pub mod schedule_field; |
| 88 |
pub mod session_acts; |
| 89 |
pub mod settings_tabs; |
| 90 |
pub mod shortcuts; |
| 91 |
pub mod ssh_keys; |
| 92 |
pub mod team; |
| 93 |
pub mod tip; |
| 94 |
pub mod upload_field; |
| 95 |
pub mod use_cases; |
| 96 |
pub mod user; |
| 97 |
pub mod user_analytics; |
| 98 |
pub mod user_projects; |
| 99 |
pub mod user_support; |
| 100 |
pub mod user_tabs; |
| 101 |
pub mod version_delete_act; |
| 102 |
pub mod widgets; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
pub struct Viewer { |
| 111 |
|
| 112 |
pub app: AppState, |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
pub user: Option<SessionUser>, |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
pub runtime: Handle, |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
pub csrf: String, |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
pub session_id: Option<crate::db::UserSessionId>, |
| 157 |
} |
| 158 |
|
| 159 |
impl Viewer { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
pub fn block_on<F: Future>(&self, future: F) -> F::Output { |
| 166 |
self.runtime.block_on(future) |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
pub fn reader(&self) -> Result<&SessionUser, quasi_router::RouteError> { |
| 180 |
self.user |
| 181 |
.as_ref() |
| 182 |
.ok_or_else(|| quasi_router::RouteError::denied("sign in to continue")) |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
#[must_use] |
| 198 |
pub fn shell(&self) -> quasi_webview::Shell { |
| 199 |
quasi_webview::Shell::under("/static") |
| 200 |
.layered(["base", "components", "responsive"]) |
| 201 |
|
| 202 |
|
| 203 |
.sending("X-CSRF-Token", &self.csrf) |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#[must_use] |
| 221 |
pub fn document_shell(&self) -> quasi_webview::Shell { |
| 222 |
document_shell(&self.csrf) |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
#[must_use] |
| 235 |
pub fn document_shell(csrf: &str) -> quasi_webview::Shell { |
| 236 |
crate::shell::described() |
| 237 |
.sending("X-CSRF-Token", csrf) |
| 238 |
.with_body_last(crate::shell::body_last()) |
| 239 |
.with_chrome(crate::quasi::shortcuts::chrome()) |
| 240 |
.with_head(format!( |
| 241 |
"<meta name=\"csrf-token\" content=\"{}\">", |
| 242 |
crate::helpers::escape_html(csrf) |
| 243 |
)) |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
#[derive(Clone, Copy, PartialEq, Eq)] |
| 254 |
enum Audience { |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
Reader, |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
Anyone, |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
fn viewer_factory( |
| 286 |
app: AppState, |
| 287 |
audience: Audience, |
| 288 |
) -> impl Fn(&http::request::Parts) -> quasi_axum::StateFuture<Viewer> + Send + Sync + 'static { |
| 289 |
move |parts| { |
| 290 |
let app = app.clone(); |
| 291 |
let runtime = Handle::current(); |
| 292 |
|
| 293 |
let session = parts.extensions.get::<tower_sessions::Session>().cloned(); |
| 294 |
Box::pin(async move { |
| 295 |
let Some(session) = session else { |
| 296 |
|
| 297 |
|
| 298 |
return Err(quasi_router::RouteError::internal("no session layer")); |
| 299 |
}; |
| 300 |
let user = match crate::auth::authenticate(&session, &app).await { |
| 301 |
Ok(user) => Some(user), |
| 302 |
Err(_) if audience == Audience::Anyone => None, |
| 303 |
Err(_) => { |
| 304 |
return Err(quasi_router::RouteError::denied("sign in to continue")); |
| 305 |
} |
| 306 |
}; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
let csrf = crate::csrf::get_or_create_token(&session) |
| 311 |
.await |
| 312 |
.map_err(|_| quasi_router::RouteError::internal("csrf token"))?; |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
let session_id = session |
| 317 |
.get::<crate::db::UserSessionId>(crate::auth::SESSION_TRACKING_KEY) |
| 318 |
.await |
| 319 |
.ok() |
| 320 |
.flatten(); |
| 321 |
Ok(Viewer { |
| 322 |
app, |
| 323 |
user, |
| 324 |
runtime, |
| 325 |
csrf, |
| 326 |
session_id, |
| 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 |
pub const PATHS: &[&str] = &[ |
| 357 |
ssh_keys::PATH, |
| 358 |
library_contacts::PATH, |
| 359 |
buyer_contacts::PATH, |
| 360 |
user_analytics::PATH, |
| 361 |
payout_summary::PATH, |
| 362 |
forum_memberships::LIBRARY_PATH, |
| 363 |
forum_memberships::SETTINGS_PATH, |
| 364 |
]; |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
pub const DOCUMENT_PATHS: &[&str] = &[feeds::PATH, export_portal::PATH]; |
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
pub const PUBLIC_DOCUMENT_PATHS: &[&str] = &[ |
| 394 |
team::PATH, |
| 395 |
use_cases::PATH, |
| 396 |
policy::PATH, |
| 397 |
fan_plus::PATH, |
| 398 |
creators::PATH, |
| 399 |
collections::PATH, |
| 400 |
git_explore::PATH, |
| 401 |
git_repos::PATH, |
| 402 |
]; |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
#[cfg(test)] |
| 411 |
const SCREENS: &[&str] = &[ |
| 412 |
ssh_keys::SCREEN, |
| 413 |
library_contacts::SCREEN, |
| 414 |
buyer_contacts::SCREEN, |
| 415 |
user_analytics::SCREEN, |
| 416 |
payout_summary::SCREEN, |
| 417 |
forum_memberships::LIBRARY_SCREEN, |
| 418 |
forum_memberships::SETTINGS_SCREEN, |
| 419 |
]; |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
pub(super) struct Range { |
| 426 |
|
| 427 |
pub value: &'static str, |
| 428 |
|
| 429 |
pub label: &'static str, |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
pub(super) const RANGES: &[Range] = &[ |
| 440 |
Range { |
| 441 |
value: "7d", |
| 442 |
label: "Last 7 days", |
| 443 |
}, |
| 444 |
Range { |
| 445 |
value: "30d", |
| 446 |
label: "Last 30 days", |
| 447 |
}, |
| 448 |
Range { |
| 449 |
value: "90d", |
| 450 |
label: "Last 90 days", |
| 451 |
}, |
| 452 |
Range { |
| 453 |
value: "all", |
| 454 |
label: "All time", |
| 455 |
}, |
| 456 |
]; |
| 457 |
|
| 458 |
|
| 459 |
pub(super) fn range_heading(range: &str) -> &'static str { |
| 460 |
RANGES |
| 461 |
.iter() |
| 462 |
.find(|window| window.value == range) |
| 463 |
.map_or("All time", |window| window.label) |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
pub(super) fn is_shown(window: &Range, range: &str) -> bool { |
| 471 |
window.value == range |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
pub fn mounts(app: &AppState) -> Vec<(&'static str, axum::Router)> { |
| 482 |
vec![ |
| 483 |
( |
| 484 |
ssh_keys::PATH, |
| 485 |
served_panel_mount( |
| 486 |
app, |
| 487 |
ssh_keys::REGION, |
| 488 |
ssh_keys::WRITES, |
| 489 |
ssh_keys::renderer, |
| 490 |
|viewer, _| { |
| 491 |
let (username, keys, tokens, themes) = ssh_keys::reading(viewer)?; |
| 492 |
Ok(ssh_keys::pane_serve( |
| 493 |
&residuals::SSH_KEYS, |
| 494 |
&username, |
| 495 |
&keys, |
| 496 |
&tokens, |
| 497 |
&themes, |
| 498 |
)) |
| 499 |
}, |
| 500 |
), |
| 501 |
), |
| 502 |
( |
| 503 |
library_contacts::PATH, |
| 504 |
served_panel_mount( |
| 505 |
app, |
| 506 |
library_contacts::REGION, |
| 507 |
library_contacts::WRITES, |
| 508 |
library_contacts::renderer, |
| 509 |
|viewer, _| { |
| 510 |
let (buyers, shared) = library_contacts::reading(viewer)?; |
| 511 |
Ok(library_contacts::pane_serve( |
| 512 |
&residuals::LIBRARY_CONTACTS, |
| 513 |
&buyers, |
| 514 |
&shared, |
| 515 |
)) |
| 516 |
}, |
| 517 |
), |
| 518 |
), |
| 519 |
|
| 520 |
|
| 521 |
( |
| 522 |
forum_memberships::LIBRARY_PATH, |
| 523 |
served_panel_mount( |
| 524 |
app, |
| 525 |
forum_memberships::LIBRARY_REGION, |
| 526 |
&[], |
| 527 |
forum_memberships::renderer, |
| 528 |
|viewer, _| { |
| 529 |
let (memberships, base) = forum_memberships::reading(viewer)?; |
| 530 |
Ok(forum_memberships::library_pane_serve( |
| 531 |
&residuals::FORUMS_LIBRARY, |
| 532 |
&memberships, |
| 533 |
&base, |
| 534 |
)) |
| 535 |
}, |
| 536 |
), |
| 537 |
), |
| 538 |
( |
| 539 |
buyer_contacts::PATH, |
| 540 |
served_panel_mount( |
| 541 |
app, |
| 542 |
buyer_contacts::REGION, |
| 543 |
&[], |
| 544 |
buyer_contacts::renderer, |
| 545 |
|viewer, _| { |
| 546 |
Ok(buyer_contacts::pane_serve( |
| 547 |
&residuals::BUYER_CONTACTS, |
| 548 |
&buyer_contacts::reading(viewer)?, |
| 549 |
)) |
| 550 |
}, |
| 551 |
), |
| 552 |
), |
| 553 |
( |
| 554 |
user_analytics::PATH, |
| 555 |
mount(app, user_analytics::screen, &[], user_analytics::renderer), |
| 556 |
), |
| 557 |
( |
| 558 |
payout_summary::PATH, |
| 559 |
served_panel_mount( |
| 560 |
app, |
| 561 |
payout_summary::REGION, |
| 562 |
&[], |
| 563 |
payout_summary::renderer, |
| 564 |
|viewer, _| { |
| 565 |
let (balance, payouts_enabled) = payout_summary::reading(viewer)?; |
| 566 |
Ok(payout_summary::card_serve( |
| 567 |
&residuals::PAYOUT_SUMMARY, |
| 568 |
balance.as_ref(), |
| 569 |
payouts_enabled, |
| 570 |
)) |
| 571 |
}, |
| 572 |
), |
| 573 |
), |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
( |
| 578 |
project_members::NEST, |
| 579 |
writes_only(app, project_members::WRITES, project_members::renderer), |
| 580 |
), |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
( |
| 585 |
item_sales::NEST, |
| 586 |
writes_only(app, item_sales::WRITES, item_sales::renderer), |
| 587 |
), |
| 588 |
( |
| 589 |
forum_memberships::SETTINGS_PATH, |
| 590 |
served_panel_mount( |
| 591 |
app, |
| 592 |
forum_memberships::SETTINGS_REGION, |
| 593 |
&[], |
| 594 |
forum_memberships::renderer, |
| 595 |
|viewer, _| { |
| 596 |
let (memberships, base) = forum_memberships::settings_reading(viewer)?; |
| 597 |
Ok(forum_memberships::settings_pane_serve( |
| 598 |
&residuals::FORUMS_SETTINGS, |
| 599 |
&memberships, |
| 600 |
&base, |
| 601 |
)) |
| 602 |
}, |
| 603 |
), |
| 604 |
), |
| 605 |
] |
| 606 |
} |
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
pub fn document_mounts(app: &AppState) -> Vec<(&'static str, axum::Router)> { |
| 612 |
vec![ |
| 613 |
( |
| 614 |
feeds::PATH, |
| 615 |
served_gated_mount(app, feeds::PATH, feeds::renderer, |viewer, carried| { |
| 616 |
|
| 617 |
|
| 618 |
let loaded = feeds::reading(viewer, carried)?; |
| 619 |
Ok(Served { |
| 620 |
screen: feeds::page_screen(&loaded.page()), |
| 621 |
markup: feeds::page_region_serve(&residuals::FEED, &loaded.page()).into(), |
| 622 |
}) |
| 623 |
}), |
| 624 |
), |
| 625 |
( |
| 626 |
export_portal::PATH, |
| 627 |
served_gated_mount( |
| 628 |
app, |
| 629 |
export_portal::PATH, |
| 630 |
export_portal::renderer, |
| 631 |
|viewer, _| { |
| 632 |
let page = export_portal::reading(viewer)?; |
| 633 |
Ok(Served { |
| 634 |
screen: export_portal::page_screen(&page), |
| 635 |
markup: export_portal::page_region_serve(&residuals::EXPORT_PORTAL, &page) |
| 636 |
.into(), |
| 637 |
}) |
| 638 |
}, |
| 639 |
), |
| 640 |
), |
| 641 |
] |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
async fn signed_in( |
| 646 |
axum::extract::State(app): axum::extract::State<AppState>, |
| 647 |
request: axum::extract::Request, |
| 648 |
next: axum::middleware::Next, |
| 649 |
) -> axum::response::Response { |
| 650 |
use axum::response::IntoResponse as _; |
| 651 |
|
| 652 |
let Some(session) = request |
| 653 |
.extensions() |
| 654 |
.get::<tower_sessions::Session>() |
| 655 |
.cloned() |
| 656 |
else { |
| 657 |
|
| 658 |
return crate::error::AppError::Internal(anyhow::anyhow!("no session layer")) |
| 659 |
.into_response(); |
| 660 |
}; |
| 661 |
match crate::auth::authenticate(&session, &app).await { |
| 662 |
Ok(_) => next.run(request).await, |
| 663 |
Err(refusal) => refusal.into_response(), |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
pub fn public_document_mounts(app: &AppState) -> Vec<(&'static str, axum::Router)> { |
| 672 |
vec![ |
| 673 |
( |
| 674 |
team::PATH, |
| 675 |
served_document_mount(app, team::PATH, team::renderer, |_, _| { |
| 676 |
Ok(Served { |
| 677 |
screen: team::page_screen(), |
| 678 |
markup: residuals::settled(&residuals::TEAM), |
| 679 |
}) |
| 680 |
}), |
| 681 |
), |
| 682 |
( |
| 683 |
use_cases::PATH, |
| 684 |
served_document_mount(app, use_cases::PATH, use_cases::renderer, |viewer, _| { |
| 685 |
|
| 686 |
|
| 687 |
let prices = use_cases::prices(viewer); |
| 688 |
Ok(Served { |
| 689 |
screen: use_cases::page_screen(&prices), |
| 690 |
markup: use_cases::page_region_serve(&residuals::USE_CASES, &prices).into(), |
| 691 |
}) |
| 692 |
}), |
| 693 |
), |
| 694 |
( |
| 695 |
policy::PATH, |
| 696 |
served_document_mount(app, policy::PATH, policy::renderer, |_, _| { |
| 697 |
Ok(Served { |
| 698 |
screen: policy::page_screen(), |
| 699 |
markup: residuals::settled(&residuals::POLICY), |
| 700 |
}) |
| 701 |
}), |
| 702 |
), |
| 703 |
( |
| 704 |
fan_plus::PATH, |
| 705 |
served_document_mount( |
| 706 |
app, |
| 707 |
fan_plus::PATH, |
| 708 |
fan_plus::renderer, |
| 709 |
|viewer, carried| { |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
let (standing, just_subscribed) = fan_plus::reading(viewer, carried)?; |
| 714 |
Ok(Served { |
| 715 |
screen: fan_plus::page_screen(&standing, just_subscribed), |
| 716 |
markup: fan_plus::page_region_serve( |
| 717 |
&residuals::FAN_PLUS, |
| 718 |
&standing, |
| 719 |
just_subscribed, |
| 720 |
) |
| 721 |
.into(), |
| 722 |
}) |
| 723 |
}, |
| 724 |
), |
| 725 |
), |
| 726 |
( |
| 727 |
creators::PATH, |
| 728 |
served_document_mount(app, creators::PATH, creators::renderer, |viewer, _| { |
| 729 |
|
| 730 |
|
| 731 |
let (standing, total_creators, prices) = creators::reading(viewer)?; |
| 732 |
Ok(Served { |
| 733 |
screen: creators::page_screen(&standing, total_creators, &prices), |
| 734 |
markup: creators::page_region_serve( |
| 735 |
&residuals::CREATORS, |
| 736 |
&standing, |
| 737 |
total_creators, |
| 738 |
&prices, |
| 739 |
) |
| 740 |
.into(), |
| 741 |
}) |
| 742 |
}), |
| 743 |
), |
| 744 |
( |
| 745 |
collections::PATH, |
| 746 |
served_document_mount( |
| 747 |
app, |
| 748 |
collections::PATH, |
| 749 |
collections::renderer, |
| 750 |
|viewer, carried| { |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
let loaded = collections::reading(viewer, carried)?; |
| 755 |
Ok(Served { |
| 756 |
screen: collections::page_screen(&loaded), |
| 757 |
markup: collections::page_region_serve(&residuals::COLLECTIONS, &loaded) |
| 758 |
.into(), |
| 759 |
}) |
| 760 |
}, |
| 761 |
), |
| 762 |
), |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
( |
| 773 |
git_explore::PATH, |
| 774 |
served_document_mount( |
| 775 |
app, |
| 776 |
git_explore::PATH, |
| 777 |
git_explore::renderer, |
| 778 |
|viewer, carried| { |
| 779 |
let loaded = git_explore::reading(viewer, carried)?; |
| 780 |
Ok(Served { |
| 781 |
screen: git_explore::page_screen(&loaded), |
| 782 |
markup: git_explore::page_region_serve(&residuals::GIT_EXPLORE, &loaded) |
| 783 |
.into(), |
| 784 |
}) |
| 785 |
}, |
| 786 |
) |
| 787 |
.layer(tower_governor::GovernorLayer::new( |
| 788 |
crate::helpers::rate_limiter_ms( |
| 789 |
crate::constants::GIT_BROWSE_RATE_LIMIT_MS, |
| 790 |
crate::constants::GIT_BROWSE_RATE_LIMIT_BURST, |
| 791 |
), |
| 792 |
)), |
| 793 |
), |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
( |
| 798 |
git_repos::PATH, |
| 799 |
served_document_mount( |
| 800 |
app, |
| 801 |
git_repos::PATH, |
| 802 |
git_repos::renderer, |
| 803 |
|viewer, carried| { |
| 804 |
let loaded = git_repos::reading(viewer, carried)?; |
| 805 |
Ok(Served { |
| 806 |
screen: git_repos::page_screen(&loaded), |
| 807 |
markup: git_repos::page_region_serve(&residuals::GIT_REPOS, &loaded).into(), |
| 808 |
}) |
| 809 |
}, |
| 810 |
) |
| 811 |
.layer(tower_governor::GovernorLayer::new( |
| 812 |
crate::helpers::rate_limiter_ms( |
| 813 |
crate::constants::GIT_BROWSE_RATE_LIMIT_MS, |
| 814 |
crate::constants::GIT_BROWSE_RATE_LIMIT_BURST, |
| 815 |
), |
| 816 |
)), |
| 817 |
), |
| 818 |
] |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
fn served_panel_mount( |
| 845 |
app: &AppState, |
| 846 |
region: &'static str, |
| 847 |
writes: &[(quasi_router::Method, &'static str, Screen)], |
| 848 |
renderer: fn(&Viewer) -> quasi_webview::Webview, |
| 849 |
answer: fn(&Viewer, &Carried) -> Result<String, quasi_router::RouteError>, |
| 850 |
) -> axum::Router { |
| 851 |
let held = app.clone(); |
| 852 |
nest(app, None, writes, renderer).route( |
| 853 |
"/", |
| 854 |
axum::routing::get(move |mut parts: axum::http::request::Parts| { |
| 855 |
let app = held.clone(); |
| 856 |
async move { |
| 857 |
use axum::response::IntoResponse as _; |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
let viewer = match viewer_factory(app, Audience::Reader)(&parts).await { |
| 865 |
Ok(viewer) => viewer, |
| 866 |
Err(refusal) => { |
| 867 |
return axum::http::StatusCode::from_u16(refusal.class.http_status()) |
| 868 |
.unwrap_or(axum::http::StatusCode::INTERNAL_SERVER_ERROR) |
| 869 |
.into_response(); |
| 870 |
} |
| 871 |
}; |
| 872 |
|
| 873 |
let carried = Carried::of(&mut parts).await; |
| 874 |
|
| 875 |
|
| 876 |
let Ok(answered) = |
| 877 |
tokio::task::spawn_blocking(move || answer(&viewer, &carried)).await |
| 878 |
else { |
| 879 |
|
| 880 |
return axum::http::StatusCode::INTERNAL_SERVER_ERROR.into_response(); |
| 881 |
}; |
| 882 |
|
| 883 |
match answered { |
| 884 |
Ok(markup) => ( |
| 885 |
[(quasi_axum::htmx::RETARGET, format!("#{region}"))], |
| 886 |
axum::response::Html(markup), |
| 887 |
) |
| 888 |
.into_response(), |
| 889 |
Err(refusal) => axum::http::StatusCode::from_u16(refusal.class.http_status()) |
| 890 |
.unwrap_or(axum::http::StatusCode::INTERNAL_SERVER_ERROR) |
| 891 |
.into_response(), |
| 892 |
} |
| 893 |
} |
| 894 |
}), |
| 895 |
) |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
fn served_document_mount( |
| 943 |
app: &AppState, |
| 944 |
path: &'static str, |
| 945 |
renderer: fn(&Viewer) -> quasi_webview::Webview, |
| 946 |
answer: fn(&Viewer, &Carried) -> Result<Served, quasi_router::RouteError>, |
| 947 |
) -> axum::Router { |
| 948 |
served(app, path, Audience::Anyone, renderer, answer) |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
fn served_gated_mount( |
| 960 |
app: &AppState, |
| 961 |
path: &'static str, |
| 962 |
renderer: fn(&Viewer) -> quasi_webview::Webview, |
| 963 |
answer: fn(&Viewer, &Carried) -> Result<Served, quasi_router::RouteError>, |
| 964 |
) -> axum::Router { |
| 965 |
served(app, path, Audience::Reader, renderer, answer) |
| 966 |
} |
| 967 |
|
| 968 |
|
| 969 |
fn served( |
| 970 |
app: &AppState, |
| 971 |
path: &'static str, |
| 972 |
audience: Audience, |
| 973 |
renderer: fn(&Viewer) -> quasi_webview::Webview, |
| 974 |
answer: fn(&Viewer, &Carried) -> Result<Served, quasi_router::RouteError>, |
| 975 |
) -> axum::Router { |
| 976 |
let gated = matches!(audience, Audience::Reader); |
| 977 |
let held = app.clone(); |
| 978 |
let router = axum::Router::new().route( |
| 979 |
path, |
| 980 |
axum::routing::get(move |mut parts: axum::http::request::Parts| { |
| 981 |
let app = held.clone(); |
| 982 |
async move { |
| 983 |
use axum::response::IntoResponse as _; |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
let Ok(viewer) = viewer_factory(app.clone(), audience)(&parts).await else { |
| 993 |
return axum::http::StatusCode::INTERNAL_SERVER_ERROR.into_response(); |
| 994 |
}; |
| 995 |
|
| 996 |
let carried = Carried::of(&mut parts).await; |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
let held = (viewer, carried); |
| 1006 |
let Ok((answered, held)) = tokio::task::spawn_blocking(move || { |
| 1007 |
let outcome = answer(&held.0, &held.1); |
| 1008 |
(outcome, held) |
| 1009 |
}) |
| 1010 |
.await |
| 1011 |
else { |
| 1012 |
|
| 1013 |
|
| 1014 |
return axum::http::StatusCode::INTERNAL_SERVER_ERROR.into_response(); |
| 1015 |
}; |
| 1016 |
let viewer = held.0; |
| 1017 |
|
| 1018 |
match answered { |
| 1019 |
Ok(Served { screen, markup }) => { |
| 1020 |
axum::response::Html(renderer(&viewer).served(&screen, &markup)) |
| 1021 |
.into_response() |
| 1022 |
} |
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
Err(refusal) => axum::http::StatusCode::from_u16(refusal.class.http_status()) |
| 1027 |
.unwrap_or(axum::http::StatusCode::INTERNAL_SERVER_ERROR) |
| 1028 |
.into_response(), |
| 1029 |
} |
| 1030 |
} |
| 1031 |
}), |
| 1032 |
); |
| 1033 |
|
| 1034 |
if gated { |
| 1035 |
router.layer(axum::middleware::from_fn_with_state(app.clone(), signed_in)) |
| 1036 |
} else { |
| 1037 |
router |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
pub struct Served { |
| 1049 |
|
| 1050 |
pub screen: quasi_router::Screen, |
| 1051 |
|
| 1052 |
pub markup: std::borrow::Cow<'static, str>, |
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
pub struct Carried { |
| 1063 |
|
| 1064 |
query: std::collections::HashMap<String, String>, |
| 1065 |
|
| 1066 |
|
| 1067 |
captures: std::collections::HashMap<String, String>, |
| 1068 |
} |
| 1069 |
|
| 1070 |
impl Carried { |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
async fn of(parts: &mut axum::http::request::Parts) -> Self { |
| 1086 |
use axum::extract::FromRequestParts as _; |
| 1087 |
|
| 1088 |
let query = parts |
| 1089 |
.uri |
| 1090 |
.query() |
| 1091 |
.map(|query| { |
| 1092 |
url::form_urlencoded::parse(query.as_bytes()) |
| 1093 |
.map(|(key, value)| (key.into_owned(), value.into_owned())) |
| 1094 |
.collect() |
| 1095 |
}) |
| 1096 |
.unwrap_or_default(); |
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
let captures = axum::extract::RawPathParams::from_request_parts(parts, &()) |
| 1102 |
.await |
| 1103 |
.map(|params| { |
| 1104 |
params |
| 1105 |
.iter() |
| 1106 |
.map(|(key, value)| (key.to_owned(), value.to_owned())) |
| 1107 |
.collect() |
| 1108 |
}) |
| 1109 |
.unwrap_or_default(); |
| 1110 |
|
| 1111 |
Self { query, captures } |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
#[must_use] |
| 1116 |
pub fn says(&self, key: &str, value: &str) -> bool { |
| 1117 |
self.query.get(key).is_some_and(|held| held.trim() == value) |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
#[must_use] |
| 1122 |
pub fn asked(&self, key: &str) -> Option<&str> { |
| 1123 |
self.query.get(key).map(String::as_str) |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
pub fn capture(&self, name: &str) -> Result<&str, quasi_router::RouteError> { |
| 1133 |
self.captures |
| 1134 |
.get(name) |
| 1135 |
.map(String::as_str) |
| 1136 |
.ok_or_else(|| quasi_router::RouteError::not_found("no such page")) |
| 1137 |
} |
| 1138 |
} |
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
pub fn public_mounts(app: &AppState) -> Vec<(&'static str, axum::Router)> { |
| 1153 |
vec![ |
| 1154 |
(pricing::PATH, pricing_mount(app)), |
| 1155 |
(shortcuts::PATH, shortcuts_mount()), |
| 1156 |
] |
| 1157 |
} |
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
fn shortcuts_mount() -> axum::Router { |
| 1166 |
quasi_axum::Adapter::new( |
| 1167 |
shortcuts::router(), |
| 1168 |
std::sync::Arc::new(()), |
| 1169 |
std::sync::Arc::new(pricing::renderer()), |
| 1170 |
) |
| 1171 |
.into_router() |
| 1172 |
} |
| 1173 |
|
| 1174 |
|
| 1175 |
fn pricing_mount(app: &AppState) -> axum::Router { |
| 1176 |
let state = std::sync::Arc::new(pricing::Pricing { |
| 1177 |
billing: crate::Billing::from_ref(app), |
| 1178 |
founder_window_open: app.config.creator_pricing.founder_window_open, |
| 1179 |
changelog_published: crate::changelog::is_published(), |
| 1180 |
}); |
| 1181 |
let router = quasi_router::Router::<pricing::Pricing>::new() |
| 1182 |
.get("/", pricing::screen) |
| 1183 |
.get("/compare", pricing::compare); |
| 1184 |
quasi_axum::Adapter::new(router, state, std::sync::Arc::new(pricing::renderer())).into_router() |
| 1185 |
} |
| 1186 |
|
| 1187 |
declare! { |
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
|
| 1192 |
|
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
#[must_use] |
| 1233 |
#[constant] |
| 1234 |
pub shape own_prose(source: impl Into<String>) -> Node; |
| 1235 |
|
| 1236 |
rich source { |
| 1237 |
trust quasi_router::Trust::Trusted; |
| 1238 |
} |
| 1239 |
} |
| 1240 |
|
| 1241 |
|
| 1242 |
pub type Screen = |
| 1243 |
fn(&Viewer, quasi_router::Request) -> Result<quasi_router::Response, quasi_router::RouteError>; |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
fn mount( |
| 1271 |
app: &AppState, |
| 1272 |
screen: Screen, |
| 1273 |
writes: &[(quasi_router::Method, &'static str, Screen)], |
| 1274 |
renderer: fn(&Viewer) -> quasi_webview::Webview, |
| 1275 |
) -> axum::Router { |
| 1276 |
nest(app, Some(screen), writes, renderer) |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
fn writes_only( |
| 1312 |
app: &AppState, |
| 1313 |
writes: &[(quasi_router::Method, &'static str, Screen)], |
| 1314 |
renderer: fn(&Viewer) -> quasi_webview::Webview, |
| 1315 |
) -> axum::Router { |
| 1316 |
nest(app, None, writes, renderer) |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
fn nest( |
| 1321 |
app: &AppState, |
| 1322 |
screen: Option<Screen>, |
| 1323 |
writes: &[(quasi_router::Method, &'static str, Screen)], |
| 1324 |
renderer: fn(&Viewer) -> quasi_webview::Webview, |
| 1325 |
) -> axum::Router { |
| 1326 |
let mut quasi = quasi_router::Router::<Viewer>::new(); |
| 1327 |
if let Some(screen) = screen { |
| 1328 |
quasi = quasi.get("/", screen); |
| 1329 |
} |
| 1330 |
for (method, path, handler) in writes { |
| 1331 |
quasi = match method { |
| 1332 |
quasi_router::Method::Delete => quasi.delete(path, *handler), |
| 1333 |
quasi_router::Method::Put => quasi.put(path, *handler), |
| 1334 |
quasi_router::Method::Get => quasi.get(path, *handler), |
| 1335 |
quasi_router::Method::Post => quasi.post(path, *handler), |
| 1336 |
}; |
| 1337 |
} |
| 1338 |
quasi_axum::Adapter::per_viewer( |
| 1339 |
quasi, |
| 1340 |
viewer_factory(app.clone(), Audience::Reader), |
| 1341 |
move |viewer, _, _| renderer(viewer), |
| 1342 |
) |
| 1343 |
.into_router() |
| 1344 |
} |
| 1345 |
|
| 1346 |
#[cfg(test)] |
| 1347 |
mod tests { |
| 1348 |
use super::*; |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
#[test] |
| 1355 |
fn our_own_sentence_is_trusted_and_a_readers_is_not() { |
| 1356 |
let ours = own_prose("Read the [docs](/docs)."); |
| 1357 |
let theirs = quasi_router::Node::rich("Read the [docs](/docs)."); |
| 1358 |
|
| 1359 |
assert!(matches!( |
| 1360 |
ours, |
| 1361 |
quasi_router::Node::Rich { |
| 1362 |
trust: quasi_router::Trust::Trusted, |
| 1363 |
.. |
| 1364 |
} |
| 1365 |
)); |
| 1366 |
assert!(matches!( |
| 1367 |
theirs, |
| 1368 |
quasi_router::Node::Rich { |
| 1369 |
trust: quasi_router::Trust::Untrusted, |
| 1370 |
.. |
| 1371 |
} |
| 1372 |
)); |
| 1373 |
} |
| 1374 |
|
| 1375 |
#[test] |
| 1376 |
fn every_screen_is_listed_in_paths() { |
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
assert_eq!( |
| 1382 |
PATHS.len(), |
| 1383 |
SCREENS.len(), |
| 1384 |
"PATHS and SCREENS describe the same screens" |
| 1385 |
); |
| 1386 |
|
| 1387 |
let source = include_str!("mod.rs"); |
| 1388 |
let mounted = source |
| 1389 |
.split_once("pub fn mounts(") |
| 1390 |
.expect("mounts exists") |
| 1391 |
.1 |
| 1392 |
.split_once("\n}") |
| 1393 |
.expect("mounts ends") |
| 1394 |
.0; |
| 1395 |
|
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
let registered = mounted.matches("mount(").count(); |
| 1407 |
assert_eq!( |
| 1408 |
registered, |
| 1409 |
PATHS.len(), |
| 1410 |
"mounts registers {registered} screens, PATHS lists {}", |
| 1411 |
PATHS.len() |
| 1412 |
); |
| 1413 |
} |
| 1414 |
|
| 1415 |
#[test] |
| 1416 |
fn every_document_screen_is_listed_in_document_paths() { |
| 1417 |
let source = include_str!("mod.rs"); |
| 1418 |
let mounted = source |
| 1419 |
.split_once("pub fn document_mounts(") |
| 1420 |
.expect("document_mounts exists") |
| 1421 |
.1 |
| 1422 |
.split_once("\n}") |
| 1423 |
.expect("document_mounts ends") |
| 1424 |
.0; |
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
let adapted = mounted.matches("document_mount(").count(); |
| 1432 |
let served = mounted.matches("served_gated_mount(").count(); |
| 1433 |
assert_eq!( |
| 1434 |
adapted + served, |
| 1435 |
DOCUMENT_PATHS.len(), |
| 1436 |
"document_mounts and DOCUMENT_PATHS describe the same screens" |
| 1437 |
); |
| 1438 |
} |
| 1439 |
|
| 1440 |
#[test] |
| 1441 |
fn every_public_document_is_listed_in_public_document_paths() { |
| 1442 |
let source = include_str!("mod.rs"); |
| 1443 |
let mounted = source |
| 1444 |
.split_once("pub fn public_document_mounts(") |
| 1445 |
.expect("public_document_mounts exists") |
| 1446 |
.1 |
| 1447 |
.split_once("\n}") |
| 1448 |
.expect("public_document_mounts ends") |
| 1449 |
.0; |
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
let adapted = mounted.matches("public_document_mount(").count(); |
| 1458 |
let served = mounted.matches("served_document_mount(").count(); |
| 1459 |
assert_eq!( |
| 1460 |
adapted + served, |
| 1461 |
PUBLIC_DOCUMENT_PATHS.len(), |
| 1462 |
"public_document_mounts and PUBLIC_DOCUMENT_PATHS describe the same screens" |
| 1463 |
); |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
#[test] |
| 1475 |
fn every_described_git_address_keeps_the_browse_limiter() { |
| 1476 |
let source = include_str!("mod.rs"); |
| 1477 |
let mounted = source |
| 1478 |
.split_once("pub fn public_document_mounts(") |
| 1479 |
.expect("public_document_mounts exists") |
| 1480 |
.1 |
| 1481 |
.split_once("\n}") |
| 1482 |
.expect("public_document_mounts ends") |
| 1483 |
.0; |
| 1484 |
|
| 1485 |
let git_paths = PUBLIC_DOCUMENT_PATHS |
| 1486 |
.iter() |
| 1487 |
.filter(|path| path.starts_with("/git")) |
| 1488 |
.count(); |
| 1489 |
assert!(git_paths > 0, "no git document is mounted yet"); |
| 1490 |
assert_eq!( |
| 1491 |
mounted.matches("GIT_BROWSE_RATE_LIMIT_MS").count(), |
| 1492 |
git_paths, |
| 1493 |
"a described /git address is mounted without the browse limiter" |
| 1494 |
); |
| 1495 |
} |
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
#[test] |
| 1502 |
fn no_document_is_in_both_lists() { |
| 1503 |
for path in PUBLIC_DOCUMENT_PATHS { |
| 1504 |
assert!( |
| 1505 |
!DOCUMENT_PATHS.contains(path), |
| 1506 |
"{path} is mounted both gated and public" |
| 1507 |
); |
| 1508 |
} |
| 1509 |
} |
| 1510 |
|
| 1511 |
#[test] |
| 1512 |
fn a_path_is_claimed_by_exactly_one_screen() { |
| 1513 |
|
| 1514 |
|
| 1515 |
let mut seen = PATHS.to_vec(); |
| 1516 |
seen.extend_from_slice(DOCUMENT_PATHS); |
| 1517 |
seen.extend_from_slice(PUBLIC_DOCUMENT_PATHS); |
| 1518 |
seen.sort_unstable(); |
| 1519 |
let before = seen.len(); |
| 1520 |
seen.dedup(); |
| 1521 |
assert_eq!( |
| 1522 |
before, |
| 1523 |
seen.len(), |
| 1524 |
"two screens claim one address: {seen:?}" |
| 1525 |
); |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|