-
Notifications
You must be signed in to change notification settings - Fork 0
/
rich_text.rs
68 lines (57 loc) · 1.77 KB
/
rich_text.rs
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
// building facets is challenging!
// cf. https://github.com/bluesky-social/atproto/blob/main/packages/api/src/rich-text/rich-text.ts
use atrium_api::app::bsky::richtext::facet::{ByteSlice, Link, Main, MainFeaturesItem};
#[derive(Default, Debug)]
pub struct RichTextBuilder {
text: String,
facets: Vec<Main>,
}
// text() - add a plain text
// link() - add a link
impl RichTextBuilder {
pub fn new() -> Self {
Default::default()
}
pub fn text<'a, S: Into<&'a str>>(mut self, text: S) -> Self {
self.text.push_str(text.into());
self
}
pub fn link<'a, S: Into<&'a str>>(mut self, url: S) -> Self {
let s = url.into();
self.facets.push(Main {
features: vec![MainFeaturesItem::Link(Box::new(Link { uri: s.into() }))],
index: ByteSlice {
byte_start: self.text.len() as i32,
byte_end: (self.text.len() + s.len()) as i32,
},
});
self.text.push_str(s);
self
}
pub fn build(self) -> (String, Vec<Main>) {
(self.text, self.facets)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rich_text_builder() {
let (text, facets) = RichTextBuilder::new()
.text("hello ")
.link("https://example.com/")
.text(" world")
.build();
assert_eq!(text, "hello https://example.com/ world");
assert_eq!(facets.len(), 1);
assert_eq!(facets[0].features.len(), 1);
assert_eq!(
facets[0].features[0],
MainFeaturesItem::Link(Box::new(Link {
uri: "https://example.com/".into()
}))
);
assert_eq!(facets[0].index.byte_start, 6);
assert_eq!(facets[0].index.byte_end, 26);
}
}