|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use crate::color::Color; |
| 3 | +use std::sync::Arc; |
| 4 | +use tokio::sync::broadcast; |
| 5 | + |
| 6 | +//main.rs |
| 7 | +fn default_zero() -> u32 { |
| 8 | + 0 |
| 9 | +} |
| 10 | + |
| 11 | +#[derive(Serialize, Deserialize, Clone, Debug)] |
| 12 | +pub struct Segment { |
| 13 | + label: String, |
| 14 | + time: u32, |
| 15 | + sound: bool, |
| 16 | + color: Option<Color>, |
| 17 | + #[serde(default = "default_zero")] |
| 18 | + count_to: u32, |
| 19 | +} |
| 20 | + |
| 21 | +#[derive(Serialize, Deserialize, Clone, Debug)] |
| 22 | +pub enum PreStartBehaviour { |
| 23 | + ShowZero, |
| 24 | + RunNormally, |
| 25 | +} |
| 26 | + |
| 27 | +impl Default for PreStartBehaviour { |
| 28 | + fn default() -> Self { |
| 29 | + PreStartBehaviour::ShowZero |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Serialize, Deserialize, Clone, Default, Debug)] |
| 34 | +pub struct DisplayOptions { |
| 35 | + #[serde(default)] |
| 36 | + clock: bool, |
| 37 | + #[serde(default)] |
| 38 | + pre_start_behaviour: PreStartBehaviour, |
| 39 | +} |
| 40 | + |
| 41 | +#[derive(Serialize, Deserialize, Clone, Default, Debug)] |
| 42 | +pub struct Timer { |
| 43 | + // Return after TimerRequest |
| 44 | + pub segments: Vec<Segment>, |
| 45 | + pub repeat: bool, |
| 46 | + pub display_options: Option<DisplayOptions>, |
| 47 | + pub start_at: u64, |
| 48 | + pub stop_at: Option<u64>, |
| 49 | + pub password: String, |
| 50 | + pub id: String, // 5 random chars |
| 51 | +} |
| 52 | + |
| 53 | +#[derive(Serialize, Clone)] |
| 54 | +#[serde(tag = "type", content = "data")] |
| 55 | +pub enum DonationMethod { |
| 56 | + PayPal(String), |
| 57 | +} |
| 58 | + |
| 59 | +#[derive(Serialize, Clone)] |
| 60 | +pub struct InstanceProperties { |
| 61 | + pub demo: bool, |
| 62 | + pub donation: Option<Vec<DonationMethod>>, |
| 63 | +} |
| 64 | + |
| 65 | +pub type SharedState = Arc<AppState>; |
| 66 | +pub struct AppState { |
| 67 | + pub redis: redis::aio::ConnectionManager, |
| 68 | + pub jwt_key: String, |
| 69 | + pub instance_properties: InstanceProperties, |
| 70 | + pub redis_task_rx: broadcast::Receiver<Timer>, |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +//timer.rs |
| 75 | + |
| 76 | +#[derive(Serialize, Deserialize, Debug)] |
| 77 | +pub struct TimerResponse { |
| 78 | + pub segments: Vec<Segment>, |
| 79 | + pub id: String, |
| 80 | + pub repeat: bool, |
| 81 | + pub display_options: DisplayOptions, |
| 82 | + pub start_at: u64, |
| 83 | + pub stop_at: Option<u64>, |
| 84 | +} |
| 85 | + |
| 86 | +impl Into<TimerResponse> for Timer { |
| 87 | + fn into(self) -> TimerResponse { |
| 88 | + TimerResponse { |
| 89 | + segments: self.segments, |
| 90 | + id: self.id, |
| 91 | + repeat: self.repeat, |
| 92 | + display_options: self.display_options.unwrap_or(DisplayOptions::default()), |
| 93 | + start_at: self.start_at, |
| 94 | + stop_at: self.stop_at, |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[derive(Serialize, Deserialize, Debug)] |
| 100 | +pub struct TimerCreationResponse { |
| 101 | + pub timer: TimerResponse, |
| 102 | + pub token: String, |
| 103 | +} |
| 104 | + |
| 105 | +#[derive(Serialize, Deserialize)] |
| 106 | +pub struct TimerCreationRequest { |
| 107 | + // Get from User |
| 108 | + pub segments: Vec<Segment>, |
| 109 | + pub id: String, |
| 110 | + pub password: String, |
| 111 | + pub repeat: bool, |
| 112 | + pub start_at: u64, |
| 113 | +} |
| 114 | + |
| 115 | +#[derive(Serialize, Deserialize)] |
| 116 | +pub struct TimerUpdateRequest { |
| 117 | + // Get from User |
| 118 | + pub segments: Vec<Segment>, |
| 119 | + pub repeat: bool, |
| 120 | + pub display_options: Option<DisplayOptions>, |
| 121 | + pub start_at: u64, |
| 122 | + pub stop_at: Option<u64>, |
| 123 | +} |
| 124 | + |
| 125 | +#[derive(Serialize, Deserialize)] |
| 126 | +pub struct TokenRequest { |
| 127 | + pub id: String, |
| 128 | + pub password: String, |
| 129 | +} |
| 130 | + |
| 131 | +#[derive(Serialize, Deserialize, Debug)] |
| 132 | +pub struct Claims { |
| 133 | + pub id: String, |
| 134 | + pub exp: usize, |
| 135 | + pub iss: String, |
| 136 | +} |
| 137 | + |
| 138 | +#[derive(Serialize, Deserialize, Debug)] |
| 139 | +pub struct TokenResponse { |
| 140 | + pub token: String, |
| 141 | +} |
0 commit comments