tg_flows/
util.rs

1use crate::types::{MessageEntity, User};
2
3/// Converts an optional iterator to a flattened iterator.
4pub(crate) fn flatten<I>(opt: Option<I>) -> impl Iterator<Item = I::Item>
5where
6    I: IntoIterator,
7{
8    struct Flat<I>(Option<I>);
9
10    impl<I> Iterator for Flat<I>
11    where
12        I: Iterator,
13    {
14        type Item = I::Item;
15
16        fn next(&mut self) -> Option<Self::Item> {
17            self.0.as_mut()?.next()
18        }
19
20        fn size_hint(&self) -> (usize, Option<usize>) {
21            match &self.0 {
22                None => (0, Some(0)),
23                Some(i) => i.size_hint(),
24            }
25        }
26    }
27
28    Flat(opt.map(<_>::into_iter))
29}
30
31pub(crate) fn mentioned_users_from_entities(
32    entities: &[MessageEntity],
33) -> impl Iterator<Item = &User> {
34    use crate::types::MessageEntityKind::*;
35
36    entities.iter().filter_map(|entity| match &entity.kind {
37        TextMention { user } => Some(user),
38
39        Mention
40        | Hashtag
41        | Cashtag
42        | BotCommand
43        | Url
44        | Email
45        | PhoneNumber
46        | Bold
47        | Italic
48        | Underline
49        | Strikethrough
50        | Spoiler
51        | Code
52        | Pre { language: _ }
53        | TextLink { url: _ }
54        | CustomEmoji { custom_emoji_id: _ } => None,
55    })
56}