Skip to main content

rig_core/completion/
handle.rs

1//! Serializable model labels resolved to live handles by a runtime.
2//!
3//! ```
4//! use rig_core::completion::ModelRef;
5//!
6//! let model = ModelRef::new("assistant");
7//! assert_eq!(model.as_str(), "assistant");
8//! ```
9
10use std::{fmt, sync::Arc};
11
12use serde::{Deserialize, Serialize};
13
14/// Model label serialized as a string. Equality compares labels without
15/// provider interpretation; resolving a label to a live handle is the runtime's responsibility.
16#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
17#[serde(transparent)]
18pub struct ModelRef(Arc<str>);
19
20impl ModelRef {
21    /// Build a reference from any string-like value.
22    pub fn new(label: impl Into<Arc<str>>) -> Self {
23        Self(label.into())
24    }
25
26    /// The label as a string slice.
27    pub fn as_str(&self) -> &str {
28        &self.0
29    }
30}
31
32impl std::ops::Deref for ModelRef {
33    type Target = str;
34
35    fn deref(&self) -> &str {
36        &self.0
37    }
38}
39
40impl AsRef<str> for ModelRef {
41    fn as_ref(&self) -> &str {
42        &self.0
43    }
44}
45
46impl fmt::Display for ModelRef {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        f.write_str(&self.0)
49    }
50}
51
52impl From<&str> for ModelRef {
53    fn from(label: &str) -> Self {
54        Self(Arc::from(label))
55    }
56}
57
58impl From<String> for ModelRef {
59    fn from(label: String) -> Self {
60        Self(Arc::from(label))
61    }
62}
63
64impl From<Arc<str>> for ModelRef {
65    fn from(label: Arc<str>) -> Self {
66        Self(label)
67    }
68}
69
70impl From<ModelRef> for String {
71    fn from(label: ModelRef) -> Self {
72        label.0.to_string()
73    }
74}
75
76impl PartialEq<str> for ModelRef {
77    fn eq(&self, other: &str) -> bool {
78        &*self.0 == other
79    }
80}
81
82impl PartialEq<&str> for ModelRef {
83    fn eq(&self, other: &&str) -> bool {
84        &*self.0 == *other
85    }
86}