~exprez135/castor

ref: 2210b43c047d8c7d346ca35d6549e4f93902618d castor/src/gopher/link.rs -rw-r--r-- 7.0 KiB
2210b43c — Julien Blanchard [gopher] don't crash on schemes in text 1 year, 1 month ago
                                                                                
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
extern crate linkify;
use linkify::LinkFinder;

use std::str::FromStr;
use url::Url;

#[derive(Debug)]
pub enum Link {
    File(Url, String),
    Ftp(Url, String),
    Finger(Url, String),
    Gemini(Url, String),
    Gopher(Url, String),
    Http(Url, String),
    Image(Url, String),
    Relative(String, String),
    Unknown(Url, String),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ParseError;

impl FromStr for Link {
    type Err = ParseError;

    // Parses a &str into an instance of 'Link'
    fn from_str(line: &str) -> Result<Self, Self::Err> {
        let mut els = line.split('\t');

        if line.starts_with('0') || line.starts_with('1') {
            let label = els.next().expect("no label");
            let path = els.next();
            let host = els.next();
            let port = els.next();

            if let Some(host) = host {
                if let Some(path) = path {
                    let mut text = String::from(label);
                    let selector = text.remove(0);

                    if let Some(port) = port {
                        if path != "" {
                            match Url::parse(&format!(
                                "gopher://{}:{}/{}{}",
                                host, port, selector, path
                            )) {
                                Ok(url) => Ok(Link::Gopher(url, text)),
                                Err(e) => {
                                    println!("ERR {:?}", e);
                                    Err(ParseError)
                                }
                            }
                        } else {
                            match Url::parse(&format!("gopher://{}:{}", host, port)) {
                                Ok(url) => Ok(Link::Gopher(url, text)),
                                Err(e) => {
                                    println!("ERR {:?}", e);
                                    Err(ParseError)
                                }
                            }
                        }
                    } else {
                        Err(ParseError)
                    }
                } else {
                    Err(ParseError)
                }
            } else {
                Err(ParseError)
            }
        } else if line.starts_with('g') || line.starts_with('I') {
            let label = els.next().expect("no label");
            let path = els.next();
            let host = els.next();
            let port = els.next();

            if let Some(host) = host {
                if let Some(p) = path {
                    let mut text = String::from(label);
                    let selector = text.remove(0);

                    let path = if p.starts_with('/') {
                        p.to_string()
                    } else {
                        format!("/{}", p)
                    };

                    if let Some(port) = port {
                        match Url::parse(&format!(
                            "gopher://{}:{}/{}{}",
                            host, port, selector, path
                        )) {
                            Ok(url) => Ok(Link::Image(url, text)),
                            Err(e) => {
                                println!("ERR {:?}", e);
                                Err(ParseError)
                            }
                        }
                    } else {
                        Err(ParseError)
                    }
                } else {
                    Err(ParseError)
                }
            } else {
                Err(ParseError)
            }
        } else if line.starts_with('9') {
            let label = els.next().expect("no label");
            let path = els.next();
            let host = els.next();
            let port = els.next();

            if let Some(host) = host {
                if let Some(p) = path {
                    let mut text = String::from(label);
                    let selector = text.remove(0);

                    let path = if p.starts_with('/') {
                        p.to_string()
                    } else {
                        format!("/{}", p)
                    };

                    if let Some(port) = port {
                        match Url::parse(&format!(
                            "gopher://{}:{}/{}{}",
                            host, port, selector, path
                        )) {
                            Ok(url) => Ok(Link::File(url, text)),
                            Err(e) => {
                                println!("ERR {:?}", e);
                                Err(ParseError)
                            }
                        }
                    } else {
                        Err(ParseError)
                    }
                } else {
                    Err(ParseError)
                }
            } else {
                Err(ParseError)
            }
        } else if line.starts_with('[') {
            let url = extract_url(line);
            let label = String::from(line);

            match make_link(String::from(url), label) {
                Some(link) => Ok(link),
                None => Err(ParseError),
            }
        } else if line.starts_with('h') {
            let label = els.next();
            let url = els.next();

            if let Some(label) = label {
                if let Some(url) = url {
                    let mut label = String::from(label);
                    label.remove(0);
                    let mut url = String::from(url);
                    let url = url.split_off(4);
                    match make_link(url, label) {
                        Some(link) => Ok(link),
                        None => Err(ParseError),
                    }
                } else {
                    Err(ParseError)
                }
            } else {
                Err(ParseError)
            }
        } else if line.contains("://") {
            let url = extract_url(line);
            let label = String::from(line);

            match make_link(String::from(url), label) {
                Some(link) => Ok(link),
                None => Err(ParseError),
            }
        } else {
            Err(ParseError)
        }
    }
}

pub fn make_link(url: String, label: String) -> Option<Link> {
    match Url::parse(&url) {
        Ok(url) => match url.scheme() {
            "finger" => Some(Link::Finger(url, label)),
            "ftp" => Some(Link::Ftp(url, label)),
            "gemini" => Some(Link::Gemini(url, label)),
            "gopher" => Some(Link::Gopher(url, label)),
            "http" => Some(Link::Http(url, label)),
            "https" => Some(Link::Http(url, label)),
            _ => Some(Link::Unknown(url, label)),
        },
        Err(url::ParseError::RelativeUrlWithoutBase) => Some(Link::Relative(url, label)),
        _ => None,
    }
}

fn extract_url(line: &str) -> &str {
    let finder = LinkFinder::new();
    let links: Vec<_> = finder.links(line).collect();
    if links.is_empty() {
        line
    } else {
        let link = &links[0];
        link.as_str()
    }
}