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