rayso_rs/
config.rs

1// Example URL: https://ray.so/#background=true&darkMode=true&padding=32&theme=candy&language=rust&code=cHJpbnRsbiEoIkhlbGxvLCB3b3JsZCIpOwo
2
3use base64::{engine::general_purpose::URL_SAFE, Engine as _};
4
5#[derive(Debug)]
6pub struct RaysoConfig {
7    background: bool,
8    dark_mode: bool,
9    padding: i32,
10    theme: String,
11    language: String,
12    code: String,
13    title: String,
14}
15
16#[derive(Default, Debug)]
17pub struct RaysoConfigBuilder {
18    background: Option<bool>,
19    dark_mode: Option<bool>,
20    padding: Option<i32>,
21    theme: Option<String>,
22    language: Option<String>,
23    code: Option<String>,
24    title: Option<String>,
25}
26
27impl RaysoConfigBuilder {
28    pub fn background(mut self, background: bool) -> RaysoConfigBuilder {
29        self.background = Some(background);
30        self
31    }
32
33    pub fn dark_mode(mut self, dark_mode: bool) -> RaysoConfigBuilder {
34        self.dark_mode = Some(dark_mode);
35        self
36    }
37
38    pub fn padding(mut self, padding: i32) -> RaysoConfigBuilder {
39        self.padding = Some(padding);
40        self
41    }
42
43    pub fn theme(mut self, theme: &str) -> RaysoConfigBuilder {
44        self.theme = Some(theme.to_string());
45        self
46    }
47
48    pub fn language(mut self, language: &str) -> RaysoConfigBuilder {
49        self.language = Some(language.to_string());
50        self
51    }
52
53    pub fn code(mut self, code: &str) -> RaysoConfigBuilder {
54        let encoded = URL_SAFE.encode(code.as_bytes());
55        self.code = Some(encoded);
56        self
57    }
58
59    pub fn title(mut self, title: &str) -> RaysoConfigBuilder {
60        self.title = Some(title.to_string());
61        self
62    }
63
64    pub fn build(self) -> RaysoConfig {
65        let encoded = URL_SAFE.encode("fn main() { println!(\"Hello, world\"); }".as_bytes());
66
67        RaysoConfig {
68            background: self.background.unwrap_or(true),
69            dark_mode: self.dark_mode.unwrap_or(true),
70            padding: self.padding.unwrap_or(32),
71            theme: self.theme.unwrap_or("candy".to_string()),
72            language: self.language.unwrap_or("rust".to_string()),
73            code: self.code.unwrap_or(encoded.to_string()),
74            title: self.title.unwrap_or("rayso-rs".to_string()),
75        }
76    }
77}
78
79impl RaysoConfig {
80    pub fn builder() -> RaysoConfigBuilder {
81        RaysoConfigBuilder::default()
82    }
83
84    pub fn to_url(&self) -> String {
85        let title = urlencoding::encode(&self.title);
86
87        format!(
88            "https://ray.so/#background={}&darkMode={}&padding={}&theme={}&language={}&code={}&title={}",
89            self.background,
90            self.dark_mode,
91            self.padding,
92            self.theme,
93            self.language,
94            self.code,
95            title
96        )
97    }
98}