File size: 3,473 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use super::{Image, ImageToHTML};
use serde::Serialize;
use std::time::Duration;
use std::thread;
use lazy_static::lazy_static;
use html_escape;

lazy_static! {
    static ref HTML_LOG_INNER: HtmlLogInner = HtmlLogInner::new();
}

#[derive(Debug, Serialize)]
struct PostMessage {
    message: String,
}

struct HtmlLogInner {
    client: reqwest::blocking::Client,
}

impl HtmlLogInner {
    fn new() -> Self {
        // let optional_proxy_url: Option<String> = Some("http://localhost:8888".to_string());
        let optional_proxy_url: Option<String> = None;
        let mut builder = reqwest::blocking::ClientBuilder::new();
        if let Some(proxy_url) = optional_proxy_url {
            let proxy: reqwest::Proxy = reqwest::Proxy::http(proxy_url).expect("proxy");
            builder = builder.proxy(proxy);
        }
        let client: reqwest::blocking::Client = builder.build().expect("client");
        
        Self {
            client
        }
    }

    #[allow(dead_code)]
    fn post_event(&self, html: String) {
        let message = PostMessage {
            message: html
        };

        let url = "http://localhost:9000/event";

        let result = self.client.post(url)
            .timeout(Duration::from_secs(5))
            .json(&message)
            .send();
        let response = match result {
            Ok(v) => v,
            Err(error) => {
                error!("could not POST request. {:?}", error);
                thread::sleep(Duration::from_millis(300));
                return;
            }
        };
        match response.status() {
            reqwest::StatusCode::OK => {
                // do nothing
            },
            reqwest::StatusCode::PAYLOAD_TOO_LARGE => {
                error!("Request payload is too large! {:#?}", response);
                thread::sleep(Duration::from_millis(300));
            },
            s => {
                error!("Expected status 200, but got something else. status: {:?} {:#?}", s, response);
                thread::sleep(Duration::from_millis(300));
            }
        }
    }
}

#[allow(dead_code)]
pub struct HtmlLog;

impl HtmlLog {
    /// Example: 
    ///
    /// HtmlLog::text("hello world");
    #[allow(dead_code)]
    pub fn text<S: AsRef<str>>(text: S) {
        let text_str: &str = text.as_ref();
        let s = html_escape::encode_text(text_str);
        HTML_LOG_INNER.post_event(s.to_string());
    }

    /// Example: 
    ///
    /// HtmlLog::html("hello <b>world</b>");
    #[allow(dead_code)]
    pub fn html<S: AsRef<str>>(html: S) {
        let html_str: &str = html.as_ref();
        HTML_LOG_INNER.post_event(html_str.to_string());
    }

    /// Example: 
    ///
    /// HtmlLog::image(&input);
    #[allow(dead_code)]
    pub fn image(image: &Image) {
        let s = image.to_html();
        HTML_LOG_INNER.post_event(s);
    }    

    /// Example: 
    /// 
    /// HtmlLog::compare_images(vec![input.clone(), output.clone()]);
    #[allow(dead_code)]
    pub fn compare_images(images: Vec<Image>) {
        let html_vec: Vec<String> = images.iter().map(|image| image.to_html()).collect();
        let compare_item_vec: Vec<String> = html_vec.iter().map(|html| format!("<span class=\"themearc compare-item\">{}</span>", html)).collect();
        let compare_items: String = compare_item_vec.join("");
        let s = format!("<div class=\"themearc compare\">{}</div>", compare_items);
        HTML_LOG_INNER.post_event(s);
    }
}