Skip to main content

bpi_rs/misc/
params.rs

1use crate::ids::Aid;
2use crate::{BpiError, BpiResult};
3
4const DEFAULT_PLATFORM: &str = "unix";
5const DEFAULT_SHARE_CHANNEL: &str = "COPY";
6const DEFAULT_SHARE_ID: &str = "main.ugc-video-detail.0.0.pv";
7const DEFAULT_SHARE_MODE: u32 = 4;
8const DEFAULT_BUVID: &str = "qwq";
9const DEFAULT_BUILD: u64 = 6_114_514;
10
11/// Parameters for `/x/share/click` b23.tv short link generation.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct MiscB23ShortLinkParams {
14    aid: Aid,
15    platform: String,
16    share_channel: String,
17    share_id: String,
18    share_mode: u32,
19    buvid: String,
20    build: u64,
21}
22
23impl MiscB23ShortLinkParams {
24    pub fn new(aid: Aid) -> Self {
25        Self {
26            aid,
27            platform: DEFAULT_PLATFORM.to_string(),
28            share_channel: DEFAULT_SHARE_CHANNEL.to_string(),
29            share_id: DEFAULT_SHARE_ID.to_string(),
30            share_mode: DEFAULT_SHARE_MODE,
31            buvid: DEFAULT_BUVID.to_string(),
32            build: DEFAULT_BUILD,
33        }
34    }
35
36    pub fn with_platform(mut self, platform: impl Into<String>) -> BpiResult<Self> {
37        self.platform = normalize_non_blank("platform", platform.into())?;
38        Ok(self)
39    }
40
41    pub fn with_share_channel(mut self, share_channel: impl Into<String>) -> BpiResult<Self> {
42        self.share_channel = normalize_non_blank("share_channel", share_channel.into())?;
43        Ok(self)
44    }
45
46    pub fn with_share_id(mut self, share_id: impl Into<String>) -> BpiResult<Self> {
47        self.share_id = normalize_non_blank("share_id", share_id.into())?;
48        Ok(self)
49    }
50
51    pub fn with_share_mode(mut self, share_mode: u32) -> Self {
52        self.share_mode = share_mode;
53        self
54    }
55
56    pub fn with_buvid(mut self, buvid: impl Into<String>) -> BpiResult<Self> {
57        self.buvid = normalize_non_blank("buvid", buvid.into())?;
58        Ok(self)
59    }
60
61    pub fn with_build(mut self, build: u64) -> Self {
62        self.build = build;
63        self
64    }
65
66    pub fn form_pairs(&self) -> Vec<(&'static str, String)> {
67        vec![
68            ("platform", self.platform.clone()),
69            ("share_channel", self.share_channel.clone()),
70            ("share_id", self.share_id.clone()),
71            ("share_mode", self.share_mode.to_string()),
72            ("oid", self.aid.to_string()),
73            ("buvid", self.buvid.clone()),
74            ("build", self.build.to_string()),
75        ]
76    }
77}
78
79fn normalize_non_blank(field: &'static str, value: String) -> BpiResult<String> {
80    let value = value.trim();
81    if value.is_empty() {
82        return Err(BpiError::invalid_parameter(field, "value cannot be blank"));
83    }
84
85    Ok(value.to_string())
86}