Skip to main content

aimux_core/
model_id.rs

1//! Model identifier helpers.
2
3use serde::{Deserialize, Serialize};
4use std::fmt;
5use ts_rs::TS;
6
7/// A model identifier in `"provider/model-name"` format, e.g. `"openai/gpt-4o"`.
8#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, TS)]
9#[ts(export)]
10pub struct ModelId {
11    provider: String,
12    model: String,
13}
14
15impl ModelId {
16    pub fn new(provider: impl Into<String>, model: impl Into<String>) -> Self {
17        Self {
18            provider: provider.into(),
19            model: model.into(),
20        }
21    }
22
23    #[must_use]
24    pub fn provider(&self) -> &str {
25        &self.provider
26    }
27
28    #[must_use]
29    pub fn model(&self) -> &str {
30        &self.model
31    }
32}
33
34impl fmt::Display for ModelId {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "{}/{}", self.provider, self.model)
37    }
38}
39
40impl std::str::FromStr for ModelId {
41    type Err = String;
42    fn from_str(s: &str) -> Result<Self, Self::Err> {
43        let (provider, model) = s
44            .split_once('/')
45            .ok_or_else(|| format!("expected 'provider/model', got '{s}'"))?;
46        Ok(Self::new(provider, model))
47    }
48}