Skip to main content

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 metrics;
22pub mod selector;
23pub mod slow_query_event;
24
25#[derive(Debug, Clone, Eq, PartialEq)]
26pub struct DisplayProcessId {
27    pub server_addr: String,
28    pub id: u32,
29}
30
31impl Display for DisplayProcessId {
32    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
33        write!(f, "{}/{}", self.server_addr, self.id)
34    }
35}
36
37impl TryFrom<&str> for DisplayProcessId {
38    type Error = error::Error;
39
40    fn try_from(value: &str) -> Result<Self, Self::Error> {
41        let mut split = value.split('/');
42        let server_addr = split
43            .next()
44            .context(error::ParseProcessIdSnafu { s: value })?
45            .to_string();
46        let id = split
47            .next()
48            .context(error::ParseProcessIdSnafu { s: value })?;
49        let id = u32::from_str(id)
50            .ok()
51            .context(error::ParseProcessIdSnafu { s: value })?;
52        Ok(DisplayProcessId { server_addr, id })
53    }
54}