Skip to content

Commit d364092

Browse files
committed
Refactor LlamaSplitMode parsing to use TryFrom with error
Replaces the From<i32> implementation for LlamaSplitMode with TryFrom<i32>, returning a custom LlamaSplitModeParseError on invalid values. Updates LlamaModelParams::split_mode to return a Result, improving error handling for unknown split modes.
1 parent 99ad808 commit d364092

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

llama-cpp-2/src/model/params.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,23 @@ pub enum LlamaSplitMode {
2222
Row = llama_cpp_sys_2::LLAMA_SPLIT_MODE_ROW as i8,
2323
}
2424

25-
/// Create a `LlamaSplitMode` from a `c_int` - returns `LlamaSplitMode::LAYER` if
26-
/// the value is not recognized.
27-
impl From<i32> for LlamaSplitMode {
28-
fn from(value: i32) -> Self {
25+
/// An error that occurs when unknown split mode is encountered.
26+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27+
pub struct LlamaSplitModeParseError(i32);
28+
29+
/// Create a `LlamaSplitMode` from a `c_int`.
30+
///
31+
/// # Errors
32+
/// Returns `()` if the value does not correspond to a valid `LlamaSplitMode`.
33+
impl TryFrom<i32> for LlamaSplitMode {
34+
type Error = LlamaSplitModeParseError;
35+
36+
fn try_from(value: i32) -> Result<Self, Self::Error> {
2937
match value {
30-
x if x == llama_cpp_sys_2::LLAMA_SPLIT_MODE_NONE => Self::None,
31-
x if x == llama_cpp_sys_2::LLAMA_SPLIT_MODE_LAYER => Self::Layer,
32-
x if x == llama_cpp_sys_2::LLAMA_SPLIT_MODE_ROW => Self::Row,
33-
_ => Self::Layer,
38+
llama_cpp_sys_2::LLAMA_SPLIT_MODE_NONE => Ok(Self::None),
39+
llama_cpp_sys_2::LLAMA_SPLIT_MODE_LAYER => Ok(Self::Layer),
40+
llama_cpp_sys_2::LLAMA_SPLIT_MODE_ROW => Ok(Self::Row),
41+
_ => Err(LlamaSplitModeParseError(value)),
3442
}
3543
}
3644
}
@@ -229,9 +237,11 @@ impl LlamaModelParams {
229237
}
230238

231239
/// get the split mode
232-
#[must_use]
233-
pub fn split_mode(&self) -> LlamaSplitMode {
234-
LlamaSplitMode::from(self.params.split_mode)
240+
///
241+
/// # Errors
242+
/// Returns `LlamaSplitModeParseError` if the unknown split mode is encountered.
243+
pub fn split_mode(&self) -> Result<LlamaSplitMode, LlamaSplitModeParseError> {
244+
LlamaSplitMode::try_from(self.params.split_mode)
235245
}
236246

237247
/// get the devices

0 commit comments

Comments
 (0)