hipcheck_common/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use anyhow::anyhow;
4
5use std::{result::Result as StdResult, str::FromStr};
6
7pub mod chunk;
8pub mod error;
9pub mod types;
10
11pub mod proto {
12	include!(concat!(env!("OUT_DIR"), "/hipcheck.v1.rs"));
13}
14
15pub struct QueryTarget {
16	pub publisher: String,
17	pub plugin: String,
18	pub query: Option<String>,
19}
20
21impl FromStr for QueryTarget {
22	type Err = anyhow::Error;
23
24	fn from_str(s: &str) -> StdResult<Self, Self::Err> {
25		let parts: Vec<&str> = s.split('/').collect();
26		match parts.as_slice() {
27			[publisher, plugin, query] => Ok(Self {
28				publisher: publisher.to_string(),
29				plugin: plugin.to_string(),
30				query: Some(query.to_string()),
31			}),
32			[publisher, plugin] => Ok(Self {
33				publisher: publisher.to_string(),
34				plugin: plugin.to_string(),
35				query: None,
36			}),
37			_ => Err(anyhow!("Invalid query target string '{}'", s)),
38		}
39	}
40}
41
42impl TryInto<QueryTarget> for &str {
43	type Error = anyhow::Error;
44	fn try_into(self) -> StdResult<QueryTarget, Self::Error> {
45		QueryTarget::from_str(self)
46	}
47}