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
//! Lower-level parsing functions for when you don't want to use
//! the provided `Twtxt` and `Tweet` objects.

use std::collections::BTreeMap;

use regex::Regex;

type StringErr<'a, T> = std::result::Result<T, &'a str>;

/// This parses out the specified information in the `== Metadata ==` section of
/// a given `twtxt.txt` file.
///
/// # Examples
/// ```
/// # use rustwtxt;
/// # use rustwtxt::parse;
/// let twtxt = rustwtxt::pull_twtxt("https://example.org/twtxt.txt").unwrap();
/// let out = parse::metadata(&twtxt, "nick");
/// ```
pub fn metadata<'a, 'b>(twtxt: &'a str, keyword: &'b str) -> StringErr<'a, &'a str> {
    if !twtxt.contains("== Metadata ==") || !twtxt.contains(keyword) {
        return Err("File contains no metadata section, or the keyword is missing");
    }

    let regex_string = format!("{} = (.*)", keyword);

    let regex = if let Ok(val) = Regex::new(&regex_string) {
        val
    } else {
        return Err("No Keyword Matches");
    };

    let matched = if let Some(val) = regex.captures(twtxt) {
        val
    } else {
        return Err("No Keyword Matches");
    };

    let keyword_match = if let Some(val) = matched.get(1) {
        val.as_str()
    } else {
        return Err("Keyword Matched Out of Bounds");
    };

    Ok(keyword_match)
}

/// Pull the individual tweets from a remote `twtxt.txt` file into
/// a `std::collections::BTreeMap<String, String>`, The timestamp
/// is the key while the status is the value.
pub fn statuses(twtxt: &str) -> Option<BTreeMap<String, String>> {
    let mut map = BTreeMap::new();
    let lines = twtxt.split("\n").collect::<Vec<&str>>();
    lines.iter().for_each(|line| {
        if line.starts_with("#") || line.len() < 2 || !line.contains("\t") {
            return;
        }

        let status = line.split("\t").collect::<Vec<&str>>();
        let datestamp = status[0];
        map.insert(datestamp.into(), status[1].into());
    });

    if map.len() == 0 {
        return None;
    }
    Some(map)
}

/// Parse the mentions out of a `twtxt.txt` file. Returns a
/// `std::collections::BTreeMap<String, String>` with the
/// timestamp of the tweet as the key and the mention as
/// the associated value.
pub fn mentions(twtxt: &str) -> Option<BTreeMap<String, String>> {
    let statuses = if let Some(val) = statuses(&twtxt) {
        val
    } else {
        return None;
    };
    let mut map = BTreeMap::new();
    statuses.iter().for_each(|(k, v)| {
        if !v.contains("@<") {
            return;
        }

        let regex = Regex::new(r"[@<].*[>]+").unwrap();
        let out = if let Some(val) = regex.captures(v) {
            match val.get(0) {
                Some(n) => n.as_str(),
                _ => return,
            }
        } else {
            return;
        };

        let mention = out.to_string();
        map.insert(k.to_string(), mention);
    });

    if map.len() == 0 {
        return None;
    }
    Some(map)
}

/// Takes a mention in the form of `@<nick https://example.com/twtxt.txt>`
/// and reduces it to just the nickname.
///
/// # Examples
/// ```
/// # use rustwtxt;
/// # use rustwtxt::parse;
/// let status = "2019.09.09\tHey there, @<nickname https://example.com/twtxt.txt!>";
/// let mention = parse::mention_to_nickname(status).unwrap();
/// assert_eq!(mention, "nickname");
/// ```
pub fn mention_to_nickname(line: &str) -> Option<String> {
    let regex = Regex::new(r"[@<].*[>]+").unwrap();
    let mention = if let Some(val) = regex.captures(line) {
        match val.get(0) {
            Some(n) => n.as_str(),
            _ => return None,
        }
    } else {
        return None;
    };

    let mention_trimmed = mention[2..mention.len() - 1].to_string();
    let mention_split = mention_trimmed.split(" ").collect::<Vec<&str>>();
    Some(mention_split[0].into())
}

/// Parses out `#tags` from each tweet, returning a `std::collections::BTreeMap<String, String>`
/// with the timestamp as the key, and the tag as the value.
pub fn tags(twtxt: &str) -> Option<BTreeMap<String, String>> {
    let statuses = if let Some(val) = statuses(&twtxt) {
        val
    } else {
        return None;
    };
    let mut map = BTreeMap::new();
    statuses.iter().for_each(|(k, v)| {
        if !v.contains("#") {
            return;
        }

        let regex = Regex::new(r"(^|\s)#[^\s]+").unwrap();
        let tag: Vec<(String, String)> = regex
            .find_iter(v)
            .map(|ding| (k.clone(), ding.as_str().to_string()))
            .collect();

        let tags: Vec<(String, String)> = tag
            .iter()
            .map(|(k, v)| {
                let v = v
                    .chars()
                    .map(|c| {
                        if c.is_whitespace() {
                            return "".into();
                        }
                        c.to_string()
                    })
                    .collect::<String>();
                (k.clone(), v)
            })
            .collect();

        let mut tag_group = String::new();
        tags.iter().for_each(|(_, v)| {
            tag_group.push_str(v);
            tag_group.push_str(" ");
        });

        map.insert(k.to_string(), tag_group[..tag_group.len() - 1].to_string());
    });

    if map.len() == 0 {
        return None;
    }
    Some(map)
}

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

    const TEST_URL: &str = "https://gbmor.dev/twtxt.txt";

    #[test]
    fn turn_mentions_to_nick() {
        let twtxt = "2019.09.09\tHey @<gbmor https://gbmor.dev/twtxt.txt>!";
        let mention = mention_to_nickname(twtxt).unwrap();
        assert_eq!("gbmor", mention);
    }

    #[test]
    fn get_tags() {
        let tag_map = tags("test\t#test").unwrap();
        assert!("#test" == &tag_map["test"]);

        let tag_map = tags("test\tsome other #test here").unwrap();
        assert!("#test" == &tag_map["test"]);

        let tag_map = tags("test\tsome other #test").unwrap();
        assert!("#test" == &tag_map["test"]);

        let tag_map = tags("test\tsome #test goes #here").unwrap();
        assert!("#test #here" == &tag_map["test"]);
    }

    #[test]
    #[should_panic]
    fn bad_regex() {
        metadata("SOME DATA", "<#*#@(&$(%)@$)>").unwrap();
    }

    #[test]
    #[should_panic]
    fn no_matches() {
        metadata("SOME = DATA", "nick").unwrap();
    }

    #[test]
    fn get_mentions() {
        let twtxt = crate::pull_twtxt(TEST_URL).unwrap();
        let mention_map = mentions(&twtxt).unwrap();
        assert!(mention_map.len() > 1);
    }

    #[test]
    fn get_username() {
        let res = crate::pull_twtxt(TEST_URL).unwrap();
        let user = metadata(&res, "nick").unwrap();
        assert_eq!("gbmor", user);
    }

    // This passes `cargo test`, but `cargo tarpaulin` segfaults
    #[ignore]
    #[test]
    fn get_url() {
        let res = crate::pull_twtxt(TEST_URL).unwrap();
        let url = metadata(&res, "url").unwrap();
        assert_eq!(TEST_URL, url);
    }

    #[test]
    fn get_status_map() {
        let twtxt = crate::pull_twtxt(TEST_URL).unwrap();
        let res = statuses(&twtxt).unwrap();
        assert!(res.len() > 1);
    }
    #[test]
    #[should_panic]
    fn parse_bad_twtxt() {
        metadata("SOMETHING GOES HERE", "url").unwrap();
    }

    #[test]
    #[should_panic]
    fn get_bad_statuses() {
        statuses("").unwrap();
    }
}