common_frontend/
lib.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::fmt::{Display, Formatter};
16use std::str::FromStr;
17
18use snafu::OptionExt;
19
20pub mod error;
21pub mod selector;
22pub mod slow_query_event;
23
24#[derive(Debug, Clone, Eq, PartialEq)]
25pub struct DisplayProcessId {
26    pub server_addr: String,
27    pub id: u32,
28}
29
30impl Display for DisplayProcessId {
31    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
32        write!(f, "{}/{}", self.server_addr, self.id)
33    }
34}
35
36impl TryFrom<&str> for DisplayProcessId {
37    type Error = error::Error;
38
39    fn try_from(value: &str) -> Result<Self, Self::Error> {
40        let mut split = value.split('/');
41        let server_addr = split
42            .next()
43            .context(error::ParseProcessIdSnafu { s: value })?
44            .to_string();
45        let id = split
46            .next()
47            .context(error::ParseProcessIdSnafu { s: value })?;
48        let id = u32::from_str(id)
49            .ok()
50            .context(error::ParseProcessIdSnafu { s: value })?;
51        Ok(DisplayProcessId { server_addr, id })
52    }
53}