botrs 0.2.8

A Rust QQ Bot framework based on QQ Guild Bot API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
//! Schedule-related data structures for the QQ Guild Bot API.
//!
//! This module contains structures for creating and managing channel schedules
//! in QQ Guild bots.

use crate::models::{HasId, HasName, Snowflake};
use serde::{Deserialize, Serialize};

/// Reminder types for schedule events.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(from = "u8", into = "u8")]
#[repr(u8)]
pub enum RemindType {
    /// No reminder
    None = 0,
    /// Remind when event starts
    OnStart = 1,
    /// Remind 5 minutes before start
    Before5Minutes = 2,
    /// Remind 15 minutes before start
    Before15Minutes = 3,
    /// Remind 30 minutes before start
    Before30Minutes = 4,
    /// Remind 1 hour before start
    Before1Hour = 5,
    /// Remind 2 hours before start
    Before2Hours = 6,
    /// Remind 1 day before start
    Before1Day = 7,
    /// Remind 2 days before start
    Before2Days = 8,
    /// Unknown reminder type
    Unknown(u8),
}

impl From<u8> for RemindType {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::None,
            1 => Self::OnStart,
            2 => Self::Before5Minutes,
            3 => Self::Before15Minutes,
            4 => Self::Before30Minutes,
            5 => Self::Before1Hour,
            6 => Self::Before2Hours,
            7 => Self::Before1Day,
            8 => Self::Before2Days,
            other => Self::Unknown(other),
        }
    }
}

impl From<RemindType> for u8 {
    fn from(remind_type: RemindType) -> Self {
        match remind_type {
            RemindType::None => 0,
            RemindType::OnStart => 1,
            RemindType::Before5Minutes => 2,
            RemindType::Before15Minutes => 3,
            RemindType::Before30Minutes => 4,
            RemindType::Before1Hour => 5,
            RemindType::Before2Hours => 6,
            RemindType::Before1Day => 7,
            RemindType::Before2Days => 8,
            RemindType::Unknown(value) => value,
        }
    }
}

impl RemindType {
    /// Returns a human-readable description of the reminder type.
    pub fn description(&self) -> &'static str {
        match self {
            RemindType::None => "No reminder",
            RemindType::OnStart => "When event starts",
            RemindType::Before5Minutes => "5 minutes before",
            RemindType::Before15Minutes => "15 minutes before",
            RemindType::Before30Minutes => "30 minutes before",
            RemindType::Before1Hour => "1 hour before",
            RemindType::Before2Hours => "2 hours before",
            RemindType::Before1Day => "1 day before",
            RemindType::Before2Days => "2 days before",
            RemindType::Unknown(_) => "Unknown",
        }
    }

    /// Returns the minutes before the event when the reminder should be sent.
    /// Returns None for RemindType::None and RemindType::OnStart.
    pub fn minutes_before(&self) -> Option<u32> {
        match self {
            RemindType::None | RemindType::OnStart => None,
            RemindType::Before5Minutes => Some(5),
            RemindType::Before15Minutes => Some(15),
            RemindType::Before30Minutes => Some(30),
            RemindType::Before1Hour => Some(60),
            RemindType::Before2Hours => Some(120),
            RemindType::Before1Day => Some(24 * 60),
            RemindType::Before2Days => Some(48 * 60),
            RemindType::Unknown(_) => None,
        }
    }
}

impl std::fmt::Display for RemindType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

/// Represents a creator of a schedule (Member information).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ScheduleCreator {
    /// User ID of the creator
    pub user_id: Option<Snowflake>,
    /// Nickname of the creator
    pub nick: Option<String>,
    /// Username of the creator
    pub username: Option<String>,
    /// Avatar URL of the creator
    pub avatar: Option<String>,
}

impl ScheduleCreator {
    /// Creates a new ScheduleCreator instance.
    pub fn new(
        user_id: Option<String>,
        nick: Option<String>,
        username: Option<String>,
        avatar: Option<String>,
    ) -> Self {
        Self {
            user_id,
            nick,
            username,
            avatar,
        }
    }
}

impl HasId for ScheduleCreator {
    fn id(&self) -> Option<&Snowflake> {
        self.user_id.as_ref()
    }
}

impl HasName for ScheduleCreator {
    fn name(&self) -> &str {
        self.nick
            .as_deref()
            .or(self.username.as_deref())
            .unwrap_or("Unknown")
    }
}

/// Represents a schedule event in a channel.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Schedule {
    /// Unique identifier for the schedule
    pub id: Option<Snowflake>,
    /// Name of the schedule event
    pub name: String,
    /// Description of the schedule event
    pub description: Option<String>,
    /// Start timestamp (Unix timestamp as string)
    pub start_timestamp: String,
    /// End timestamp (Unix timestamp as string)
    pub end_timestamp: String,
    /// Creator of the schedule
    pub creator: Option<ScheduleCreator>,
    /// Channel ID to jump to when the event starts
    pub jump_channel_id: Option<Snowflake>,
    /// Reminder type for the schedule
    pub remind_type: Option<RemindType>,
}

impl Schedule {
    /// Creates a new Schedule instance.
    ///
    /// # Arguments
    ///
    /// * `name` - Name of the schedule event
    /// * `start_timestamp` - Start time as Unix timestamp string
    /// * `end_timestamp` - End time as Unix timestamp string
    /// * `jump_channel_id` - Optional channel ID to jump to
    /// * `remind_type` - Type of reminder to set
    pub fn new(
        name: impl Into<String>,
        start_timestamp: impl Into<String>,
        end_timestamp: impl Into<String>,
        jump_channel_id: Option<String>,
        remind_type: RemindType,
    ) -> Self {
        Self {
            id: None,
            name: name.into(),
            description: None,
            start_timestamp: start_timestamp.into(),
            end_timestamp: end_timestamp.into(),
            creator: None,
            jump_channel_id,
            remind_type: Some(remind_type),
        }
    }

    /// Sets the description for this schedule.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Sets the creator for this schedule.
    pub fn with_creator(mut self, creator: ScheduleCreator) -> Self {
        self.creator = Some(creator);
        self
    }

    /// Sets the ID for this schedule.
    pub fn with_id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(id.into());
        self
    }

    /// Returns true if the schedule has a reminder set.
    pub fn has_reminder(&self) -> bool {
        !matches!(self.remind_type, Some(RemindType::None) | None)
    }

    /// Gets the reminder description.
    pub fn reminder_description(&self) -> &'static str {
        self.remind_type
            .as_ref()
            .map(|r| r.description())
            .unwrap_or("No reminder")
    }

    /// Attempts to parse the start timestamp as a Unix timestamp.
    pub fn start_timestamp_parsed(&self) -> Result<i64, std::num::ParseIntError> {
        self.start_timestamp.parse::<i64>()
    }

    /// Attempts to parse the end timestamp as a Unix timestamp.
    pub fn end_timestamp_parsed(&self) -> Result<i64, std::num::ParseIntError> {
        self.end_timestamp.parse::<i64>()
    }

    /// Returns the duration of the event in seconds, if timestamps can be parsed.
    pub fn duration_seconds(&self) -> Option<i64> {
        let start = self.start_timestamp_parsed().ok()?;
        let end = self.end_timestamp_parsed().ok()?;
        Some(end - start)
    }

    /// Returns true if this schedule has a jump channel set.
    pub fn has_jump_channel(&self) -> bool {
        self.jump_channel_id.is_some()
    }
}

impl HasId for Schedule {
    fn id(&self) -> Option<&Snowflake> {
        self.id.as_ref()
    }
}

impl HasName for Schedule {
    fn name(&self) -> &str {
        &self.name
    }
}

impl std::fmt::Display for Schedule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Schedule {{ id: {:?}, name: {}, start: {}, end: {}, reminder: {} }}",
            self.id,
            self.name,
            self.start_timestamp,
            self.end_timestamp,
            self.reminder_description()
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_remind_type_conversion() {
        assert_eq!(RemindType::from(0), RemindType::None);
        assert_eq!(RemindType::from(1), RemindType::OnStart);
        assert_eq!(RemindType::from(5), RemindType::Before1Hour);
        assert_eq!(u8::from(RemindType::Before15Minutes), 3);
        assert_eq!(u8::from(RemindType::Before1Day), 7);

        assert_eq!(RemindType::from(99), RemindType::Unknown(99));
        assert_eq!(u8::from(RemindType::Unknown(99)), 99);
    }

    #[test]
    fn test_remind_type_description() {
        assert_eq!(RemindType::None.description(), "No reminder");
        assert_eq!(
            RemindType::Before30Minutes.description(),
            "30 minutes before"
        );
        assert_eq!(RemindType::Before1Day.description(), "1 day before");
    }

    #[test]
    fn test_remind_type_minutes_before() {
        assert_eq!(RemindType::None.minutes_before(), None);
        assert_eq!(RemindType::OnStart.minutes_before(), None);
        assert_eq!(RemindType::Before5Minutes.minutes_before(), Some(5));
        assert_eq!(RemindType::Before1Hour.minutes_before(), Some(60));
        assert_eq!(RemindType::Before1Day.minutes_before(), Some(24 * 60));
    }

    #[test]
    fn test_schedule_creator() {
        let creator = ScheduleCreator::new(
            Some("user123".to_string()),
            Some("TestUser".to_string()),
            Some("testuser".to_string()),
            Some("https://example.com/avatar.png".to_string()),
        );

        assert_eq!(creator.id(), Some(&"user123".to_string()));
        assert_eq!(creator.name(), "TestUser");
    }

    #[test]
    fn test_schedule_creator_fallback_name() {
        let creator = ScheduleCreator::new(
            Some("user123".to_string()),
            None,
            Some("testuser".to_string()),
            None,
        );

        assert_eq!(creator.name(), "testuser");

        let creator_no_name = ScheduleCreator::new(None, None, None, None);
        assert_eq!(creator_no_name.name(), "Unknown");
    }

    #[test]
    fn test_schedule_creation() {
        let schedule = Schedule::new(
            "Team Meeting",
            "1640995200",
            "1640998800",
            Some("channel123".to_string()),
            RemindType::Before15Minutes,
        );

        assert_eq!(schedule.name, "Team Meeting");
        assert_eq!(schedule.start_timestamp, "1640995200");
        assert_eq!(schedule.end_timestamp, "1640998800");
        assert_eq!(schedule.jump_channel_id, Some("channel123".to_string()));
        assert_eq!(schedule.remind_type, Some(RemindType::Before15Minutes));
        assert!(schedule.has_reminder());
        assert!(schedule.has_jump_channel());
    }

    #[test]
    fn test_schedule_with_description() {
        let schedule = Schedule::new(
            "Daily Standup",
            "1640995200",
            "1640996400",
            None,
            RemindType::Before5Minutes,
        )
        .with_description("Daily team standup meeting");

        assert_eq!(
            schedule.description,
            Some("Daily team standup meeting".to_string())
        );
    }

    #[test]
    fn test_schedule_duration() {
        let schedule = Schedule::new(
            "Test Event",
            "1640995200", // Start
            "1640998800", // End (1 hour later)
            None,
            RemindType::None,
        );

        assert_eq!(schedule.duration_seconds(), Some(3600)); // 1 hour = 3600 seconds
    }

    #[test]
    fn test_schedule_no_reminder() {
        let schedule = Schedule::new(
            "No Reminder Event",
            "1640995200",
            "1640998800",
            None,
            RemindType::None,
        );

        assert!(!schedule.has_reminder());
        assert_eq!(schedule.reminder_description(), "No reminder");
    }

    #[test]
    fn test_schedule_display() {
        let schedule = Schedule::new(
            "Test Meeting",
            "1640995200",
            "1640998800",
            Some("channel456".to_string()),
            RemindType::Before30Minutes,
        );

        let display = format!("{}", schedule);
        assert!(display.contains("Test Meeting"));
        assert!(display.contains("1640995200"));
        assert!(display.contains("30 minutes before"));
    }

    #[test]
    fn test_schedule_timestamp_parsing() {
        let schedule = Schedule::new(
            "Parse Test",
            "1640995200",
            "invalid_timestamp",
            None,
            RemindType::None,
        );

        assert!(schedule.start_timestamp_parsed().is_ok());
        assert!(schedule.end_timestamp_parsed().is_err());
        assert_eq!(schedule.duration_seconds(), None);
    }
}