aboutsummaryrefslogtreecommitdiffstats
path: root/src/stream/tag.rs
blob: 36874c2bede8af8e7849466d395b64fa4a0c8f3f (plain) (blame)
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
/**
 * Representation of a Tag
 *
 * SPDX-FileCopyrightText: 2023 Eilertsens Kodeknekkeri
 * SPDX-FileCopyrightText: 2023 Harald Eilertsen <haraldei@anduin.net>
 *
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

use serde::Deserialize;
use url::Url;

#[derive(Debug, Deserialize, PartialEq)]
pub enum TagType {
    #[serde(alias = "hashtag")]
    Hashtag,

    #[serde(alias = "mention")]
    Mention,
}

#[derive(Debug, Deserialize, PartialEq)]
pub struct Tag {
    #[serde(alias = "name")]
    tag: String,

    #[serde(alias = "href")]
    url: Url,

    #[serde(rename = "type")]
    tag_type: TagType,
}

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

    #[test]
    fn test_parsing_tag_from_json() {
        let json = r#"
              {
                "tag": "hubzilla",
                "url": "https://example.com/search?tag=hubzilla",
                "type": "hashtag"
              }
        "#;

        let tag: Tag = serde_json::from_str(json).unwrap();
        assert_eq!("hubzilla", tag.tag);
        assert_eq!("https://example.com/search?tag=hubzilla", tag.url.to_string());
        assert_eq!(TagType::Hashtag, tag.tag_type);
    }

    #[test]
    fn test_parsing_mention_from_json() {
        let json = r#"
            {
              "type": "Mention",
              "href": "https://example.com/channel/ben",
              "name": "@ben@example.com"
            }
        "#;

        let tag: Tag = serde_json::from_str(json).unwrap();
        assert_eq!(TagType::Mention, tag.tag_type);
        assert_eq!("https://example.com/channel/ben", tag.url.to_string());
        assert_eq!("@ben@example.com", tag.tag);
    }
}