Skip to main content

deaddrop_core/
priority.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(
4    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
5)]
6#[serde(rename_all = "snake_case")]
7pub enum Priority {
8    Bulk = 0,
9    #[default]
10    Normal = 1,
11    Important = 2,
12    Urgent = 3,
13}
14
15impl Priority {
16    pub fn as_u8(self) -> u8 {
17        self as u8
18    }
19
20    pub fn from_u8(v: u8) -> Self {
21        match v {
22            0 => Self::Bulk,
23            2 => Self::Important,
24            3 => Self::Urgent,
25            _ => Self::Normal,
26        }
27    }
28
29    /// Per-peer share of transfer slots. Urgent cannot monopolize the link.
30    pub fn slot_weight(self) -> u32 {
31        match self {
32            Self::Bulk => 1,
33            Self::Normal => 2,
34            Self::Important => 3,
35            Self::Urgent => 3,
36        }
37    }
38
39    /// CLI names: background/bulk, normal, high, critical.
40    /// Wire values stay Bulk=0, Normal=1, Important=2, Urgent=3.
41    pub fn parse_cli(s: &str) -> Option<Self> {
42        match s.trim().to_ascii_lowercase().as_str() {
43            "background" | "bulk" => Some(Self::Bulk),
44            "normal" => Some(Self::Normal),
45            "high" | "important" => Some(Self::Important),
46            "critical" | "urgent" => Some(Self::Urgent),
47            _ => None,
48        }
49    }
50
51    pub fn cli_name(self) -> &'static str {
52        match self {
53            Self::Bulk => "bulk",
54            Self::Normal => "normal",
55            Self::Important => "high",
56            Self::Urgent => "critical",
57        }
58    }
59}