Skip to main content

twitter_text/
validator.rs

1// Copyright 2019 Robert Sayre
2// Licensed under the Apache License, Version 2.0
3// http://www.apache.org/licenses/LICENSE-2.0
4
5use twitter_text_config;
6use parse;
7
8use twitter_text_parser::twitter_text::TwitterTextParser;
9use twitter_text_parser::twitter_text::Rule;
10use pest::Parser;
11
12const MAX_TWEET_LENGTH: i32 = 280;
13
14pub struct Validator {
15    short_url_length: i32,
16    short_url_length_https: i32,
17}
18
19impl Validator {
20    pub fn new() -> Validator {
21        Validator {
22            short_url_length: 23,
23            short_url_length_https: 23
24        }
25    }
26
27    pub fn is_valid_tweet(&self, s: &str) -> bool {
28        parse(s, &twitter_text_config::config_v1(), false).is_valid
29    }
30
31    pub fn is_valid_username(&self, s: &str) -> bool {
32        TwitterTextParser::parse(Rule::valid_username, s).is_ok()
33    }
34
35    pub fn is_valid_list(&self, s: &str) -> bool {
36        TwitterTextParser::parse(Rule::valid_list, s).is_ok()
37    }
38
39    pub fn is_valid_hashtag(&self, s: &str) -> bool {
40        TwitterTextParser::parse(Rule::hashtag, s).is_ok()
41    }
42
43    pub fn is_valid_url(&self, s: &str) -> bool {
44        TwitterTextParser::parse(Rule::valid_url, s).is_ok()
45    }
46
47    pub fn is_valid_url_without_protocol(&self, s: &str) -> bool {
48        TwitterTextParser::parse(Rule::url_without_protocol, s).is_ok()
49    }
50
51    pub fn get_max_tweet_length(&self) -> i32 { MAX_TWEET_LENGTH }
52
53    pub fn get_short_url_length(&self) -> i32 {
54        self.short_url_length
55    }
56
57    pub fn set_short_url_length(&mut self, short_url_length: i32) {
58        self.short_url_length = short_url_length;
59    }
60
61    pub fn get_short_url_length_https(&self) -> i32 {
62        self.short_url_length_https
63    }
64
65    pub fn set_short_url_length_https(&mut self, short_url_length_https: i32) {
66        self.short_url_length_https = short_url_length_https;
67    }
68}