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
use async_recursion::async_recursion;
use futures::future::join_all;
use petgraph::graph::DiGraph;
use reqwest::Client;
use std::collections::{HashMap, HashSet};
use tokio::sync::{Mutex, RwLock};
use tracing::{info, instrument};
use url::Url;

async fn get_webpage(client: &Client, url: &Url) -> Result<String, reqwest::Error> {
    client.get(url.clone()).send().await?.text().await
}

pub async fn build_graph(
    client: &Client,
    root: Url,
    get_children: impl Fn(&Url, &str, usize) -> Option<HashSet<Url>> + 'static + Clone,
) -> (DiGraph<Url, ()>, HashMap<Url, Result<String, String>>) {
    let nodes = Default::default();
    let edges = Default::default();
    edit_graph(client, root, get_children, &nodes, &edges, 0).await;
    let nodes = nodes.into_inner();
    let edges = edges.into_inner();
    let mut graph = DiGraph::new();
    let mut indices = HashMap::new();
    for (url, _) in &nodes {
        indices.insert(url.clone(), graph.add_node(url.clone()));
    }
    for (from, to) in edges {
        graph.add_edge(indices[&from], indices[&to], ());
    }
    (graph, nodes)
}

#[async_recursion(?Send)]
#[instrument(skip_all, fields(parent))]
async fn edit_graph(
    client: &Client,
    parent: Url,
    get_children: impl Fn(&Url, &str, usize) -> Option<HashSet<Url>> + 'static + Clone,
    nodes: &RwLock<HashMap<Url, Result<String, String>>>,
    edges: &Mutex<HashSet<(Url, Url)>>,
    depth: usize,
) {
    if nodes.read().await.contains_key(&parent) {
        return;
    }
    let res = get_webpage(client, &parent)
        .await
        .map_err(|e| e.to_string());
    {
        let mut write = nodes.write().await;
        match write.contains_key(&parent) {
            true => return,
            false => {
                info!("Add nodes from {parent}");
                write.insert(parent.clone(), res.clone());
                drop(write);

                if let Ok(s) = res {
                    if let Some(children) = get_children(&parent, &s, depth) {
                        info!("Disovered {} children", children.len());
                        let mut write = edges.lock().await;
                        for child in &children {
                            let newly_added = write.insert((parent.clone(), child.clone()));
                            assert!(newly_added, "logic error - created same edge twice");
                        }
                        drop(write);
                        join_all(children.into_iter().map(|new_parent| {
                            edit_graph(
                                client,
                                new_parent,
                                get_children.clone(),
                                nodes,
                                edges,
                                depth + 1,
                            )
                        }))
                        .await;
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::collections::{HashMap, HashSet};

    use httptest::{matchers::request, responders::status_code, Expectation, Server};
    use petgraph::graph::DiGraph;
    use soup::{NodeExt, QueryBuilderExt, Soup};
    use url::Url;

    use crate::build_graph;

    const LINK_TO_BAR: &'static str = r#"<a href="/bar">bar</a>"#;
    const LINK_TO_FOO: &'static str = r#"<a href="/foo">foo</a>"#;

    #[tokio::test]
    async fn cyclic() {
        let (graph, pages) = do_test(
            Server::run()
                .serve("/", LINK_TO_FOO)
                .serve("/foo", LINK_TO_BAR)
                .serve("/bar", LINK_TO_FOO),
        )
        .await;
        assert_eq!(graph.node_count(), 3);
        assert_eq!(pages.len(), 3);
    }

    #[tokio::test]
    async fn two_children() {
        let (graph, pages) = do_test(
            Server::run()
                .serve(
                    "/",
                    Box::leak(format!("{}{}", LINK_TO_FOO, LINK_TO_BAR).into_boxed_str()),
                )
                .no_serve("/foo")
                .no_serve("/bar"),
        )
        .await;
        assert_eq!(graph.node_count(), 3);
        assert_eq!(pages.len(), 3);
    }

    #[tokio::test]
    async fn single_grandchild() {
        let (graph, pages) = do_test(
            Server::run()
                .serve("/", LINK_TO_FOO)
                .serve("/foo", LINK_TO_BAR)
                .no_serve("/bar"),
        )
        .await;
        assert_eq!(graph.node_count(), 3);
        assert_eq!(pages.len(), 3);
    }

    #[tokio::test]
    async fn single_child() {
        let (graph, pages) = do_test(Server::run().serve("/", LINK_TO_FOO).no_serve("/foo")).await;
        assert_eq!(graph.node_count(), 2);
        assert_eq!(pages.len(), 2);
    }

    #[tokio::test]
    async fn terminal_node() {
        let (graph, pages) = do_test(Server::run().serve("/", "")).await;
        assert_eq!(graph.node_count(), 1);
        assert_eq!(pages.len(), 1);
    }

    #[tokio::test]
    async fn terminal_node_err() {
        let (graph, pages) = do_test(Server::run().no_serve("/")).await;
        assert_eq!(graph.node_count(), 1);
        assert_eq!(pages.len(), 1);
    }

    async fn do_test(server: Server) -> (DiGraph<Url, ()>, HashMap<Url, Result<String, String>>) {
        build_graph(
            &Default::default(),
            server
                .url("/")
                .to_string()
                .parse()
                .expect("URI isn't a URL"),
            get_all_children,
        )
        .await
    }

    fn get_all_children(url: &Url, body: &str, _depth: usize) -> Option<HashSet<Url>> {
        Some(
            Soup::new(body)
                .tag("a")
                .attr_name("href")
                .find_all()
                .map(|anchor| {
                    let href = anchor.get("href").expect("Already filtered by href");
                    match href.parse::<Url>() {
                        Ok(url) => Ok(url),
                        Err(url::ParseError::RelativeUrlWithoutBase) => url.join(&href),
                        Err(e) => Err(e),
                    }
                })
                .filter_map(Result::ok)
                .collect(),
        )
    }

    trait ServerExt {
        fn serve(self, path: &'static str, body: &'static str) -> Self;
        fn no_serve(self, path: &'static str) -> Self;
    }

    impl ServerExt for Server {
        fn serve(self, path: &'static str, body: &'static str) -> Self {
            self.expect(
                Expectation::matching(request::method_path("GET", path))
                    .respond_with(status_code(200).body(body)),
            );
            self
        }

        fn no_serve(self, path: &'static str) -> Self {
            self.expect(
                Expectation::matching(request::method_path("GET", path))
                    .respond_with(status_code(400)),
            );
            self
        }
    }
}