Skip to main content

gosub_sonar/net/
request_ref.rs

1//! Correlation tags for tracking fetch requests without coupling to application internals.
2
3use std::fmt::Display;
4
5/// Request references tag a fetch with an application-defined correlation ID,
6/// without the net layer needing to know about higher-level concepts like tabs
7/// or navigation stacks.
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
9pub enum RequestReference {
10    /// A numbered background or prefetch task
11    Background(u64),
12    /// An application-defined opaque task group ID
13    Tagged(u64),
14}
15
16impl Default for RequestReference {
17    fn default() -> Self {
18        RequestReference::Background(0)
19    }
20}
21
22impl Display for RequestReference {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            RequestReference::Background(id) => write!(f, "BG({})", id),
26            RequestReference::Tagged(id) => write!(f, "Tagged({})", id),
27        }
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn display_background() {
37        assert_eq!(format!("{}", RequestReference::Background(42)), "BG(42)");
38    }
39
40    #[test]
41    fn display_tagged() {
42        assert_eq!(format!("{}", RequestReference::Tagged(7)), "Tagged(7)");
43    }
44}