codex_protocol/
tool_name.rs1use serde::Deserialize;
2use serde::Serialize;
3use std::cmp::Ordering;
4use std::fmt;
5
6#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
9pub struct ToolName {
10 pub name: String,
11 pub namespace: Option<String>,
12}
13
14impl ToolName {
15 pub fn new(namespace: Option<String>, name: impl Into<String>) -> Self {
16 Self {
17 name: name.into(),
18 namespace,
19 }
20 }
21
22 pub fn plain(name: impl Into<String>) -> Self {
23 Self {
24 name: name.into(),
25 namespace: None,
26 }
27 }
28
29 pub fn namespaced(namespace: impl Into<String>, name: impl Into<String>) -> Self {
30 Self {
31 name: name.into(),
32 namespace: Some(namespace.into()),
33 }
34 }
35}
36
37impl fmt::Display for ToolName {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 match &self.namespace {
40 Some(namespace) => write!(f, "{namespace}{}", self.name),
41 None => f.write_str(&self.name),
42 }
43 }
44}
45
46impl Ord for ToolName {
47 fn cmp(&self, other: &Self) -> Ordering {
48 let lhs = match &self.namespace {
49 Some(namespace) => (namespace.as_str(), Some(self.name.as_str())),
50 None => (self.name.as_str(), None),
51 };
52 let rhs = match &other.namespace {
53 Some(namespace) => (namespace.as_str(), Some(other.name.as_str())),
54 None => (other.name.as_str(), None),
55 };
56 lhs.cmp(&rhs)
57 }
58}
59
60impl PartialOrd for ToolName {
61 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
62 Some(self.cmp(other))
63 }
64}
65
66impl From<String> for ToolName {
67 fn from(name: String) -> Self {
68 Self::plain(name)
69 }
70}
71
72impl From<&str> for ToolName {
73 fn from(name: &str) -> Self {
74 Self::plain(name)
75 }
76}