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
use chrono::NaiveDateTime;
use std::ops::Range;
#[cfg(feature = "serde-1")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-1", serde(rename_all = "kebab-case"))]
pub struct Message {
pub meta: Metadata,
pub body: Body,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-1", serde(rename_all = "kebab-case"))]
pub struct Metadata {
pub timestamp: NaiveDateTime,
pub sender: String,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-1", serde(rename_all = "kebab-case", tag = "type"))]
pub enum Body {
DirectMessage(DirectMessage),
Attachment(Attachment),
}
impl From<DirectMessage> for Body {
fn from(other: DirectMessage) -> Body { Body::DirectMessage(other) }
}
impl From<Attachment> for Body {
fn from(other: Attachment) -> Body { Body::Attachment(other) }
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-1", serde(rename_all = "kebab-case"))]
pub struct DirectMessage {
pub content: String,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde-1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-1", serde(rename_all = "kebab-case"))]
pub struct Attachment {
pub name: String,
pub span: Span,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde-1", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde-1", serde(rename_all = "kebab-case"))]
pub struct Span {
pub start: usize,
pub end: usize,
}
impl Span {
pub const fn new(start: usize, end: usize) -> Self { Span { start, end } }
pub const fn as_range(self) -> Range<usize> { self.start..self.end }
pub fn get(self, text: &str) -> Option<&str> { text.get(self.as_range()) }
}