mdmodels_core/llm/input.rs
1/*
2 * Copyright (c) 2025 Jan Range
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 *
22 */
23
24use std::path::PathBuf;
25
26use reqwest::Url;
27
28use crate::datamodel::DataModel;
29
30/// Represents different types of models that can be used.
31///
32/// `ModelType` can be constructed from a local file path, a remote URL, or a `DataModel` instance.
33#[allow(clippy::large_enum_variant)]
34pub enum ModelType {
35 Path(PathBuf),
36 Remote(Url),
37 Model(DataModel),
38}
39
40impl TryFrom<PathBuf> for ModelType {
41 type Error = Box<dyn std::error::Error>;
42
43 /// Attempts to create a `ModelType` from a `PathBuf`.
44 ///
45 /// Returns an error if the path does not exist.
46 fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
47 if !path.exists() {
48 return Err(Box::from("Path does not exist"));
49 }
50 Ok(Self::Path(path))
51 }
52}
53
54impl TryFrom<Url> for ModelType {
55 type Error = Box<dyn std::error::Error>;
56
57 /// Creates a `ModelType` from a `Url`.
58 fn try_from(url: Url) -> Result<Self, Self::Error> {
59 Ok(Self::Remote(url))
60 }
61}
62
63impl TryFrom<DataModel> for ModelType {
64 type Error = Box<dyn std::error::Error>;
65
66 /// Creates a `ModelType` from a `DataModel`.
67 fn try_from(model: DataModel) -> Result<Self, Self::Error> {
68 Ok(Self::Model(model))
69 }
70}