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
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

use prost::Message;

use crate::proto::make_table_data::Data;
use crate::proto::request::ClientReq;
use crate::proto::response::ClientResp;
use crate::proto::{
    MakeTableData, MakeTableReq, Request, Response, TableUpdateReq, ViewToColumnsStringResp,
};

fn replace(x: Data) -> Data {
    match x {
        Data::FromArrow(_) => Data::FromArrow("<< redacted >>".to_string().encode_to_vec()),
        Data::FromRows(_) => Data::FromRows("<< redacted >>".to_string()),
        Data::FromCols(_) => Data::FromCols("".to_string()),
        Data::FromCsv(_) => Data::FromCsv("".to_string()),
        x => x,
    }
}

/// `prost` generates `Debug` implementations that includes the `data` field,
/// which makes logs output unreadable. This `Display` implementation hides
/// fields that we don't want ot display in the logs.
impl std::fmt::Display for Request {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut msg = self.clone();
        msg = match msg {
            Request {
                client_req:
                    Some(ClientReq::MakeTableReq(MakeTableReq {
                        ref options,
                        data:
                            Some(MakeTableData {
                                data: Some(ref data),
                            }),
                    })),
                ..
            } => Request {
                client_req: Some(ClientReq::MakeTableReq(MakeTableReq {
                    options: options.clone(),
                    data: Some(MakeTableData {
                        data: Some(replace(data.clone())),
                    }),
                })),
                ..msg.clone()
            },
            Request {
                client_req:
                    Some(ClientReq::TableUpdateReq(TableUpdateReq {
                        port_id,
                        data:
                            Some(MakeTableData {
                                data: Some(ref data),
                            }),
                    })),
                ..
            } => Request {
                client_req: Some(ClientReq::TableUpdateReq(TableUpdateReq {
                    port_id,
                    data: Some(MakeTableData {
                        data: Some(replace(data.clone())),
                    }),
                })),
                ..msg.clone()
            },
            x => x,
        };

        write!(
            f,
            "{}",
            serde_json::to_string(&msg).unwrap_or("Can't deserialize".to_string())
        )
    }
}

impl std::fmt::Display for Response {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut msg = self.clone();
        msg = match msg {
            Response {
                client_resp: Some(ClientResp::ViewToColumnsStringResp(_)),
                ..
            } => Response {
                client_resp: Some(ClientResp::ViewToColumnsStringResp(
                    ViewToColumnsStringResp {
                        json_string: "<< redacted >>".to_owned(),
                    },
                )),
                ..msg.clone()
            },
            x => x,
        };

        write!(f, "{}", serde_json::to_string(&msg).unwrap())
    }
}