~exprez135/castor

ref: 193de0c2079abf371286916b814804f34dd97087 castor/src/gopher/link.rs -rw-r--r-- 5.3 KiB
193de0c2Julien Blanchard [gopher] Fix parsing of h type 1 year, 6 months 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
use std::str::FromStr;
use url::Url;

#[derive(Debug)]
pub enum Link {
    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('[') {
            let mut url = String::from(line);
            let url = url.split_off(4);
            let label = String::from(line);

            match make_link(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 = String::from(line);
            let label = String::from(line);

            match make_link(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() {
            "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,
    }
}