From 29324563648a6bb67a7867c716ea80763229c0e5 Mon Sep 17 00:00:00 2001 From: madclaws Date: Sat, 18 Oct 2025 15:56:32 +0530 Subject: [PATCH 1/2] lets fix the comment parse error --- src/core/modelfile.rs | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/core/modelfile.rs b/src/core/modelfile.rs index 2103a5b..3490bfc 100644 --- a/src/core/modelfile.rs +++ b/src/core/modelfile.rs @@ -50,6 +50,14 @@ enum Output<'a> { Single(&'a str), Pair((&'a str, &'a str)), } +impl<'a> Display for Output<'a> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Single(word) => write!(f, "{}", word), + Self::Pair((word, word_1)) => write!(f, "{} {}", word, word_1), + } + } +} impl FromStr for Role { type Err = String; @@ -341,9 +349,13 @@ fn create_modelfile(commands: Vec<(&str, Output)>) -> Result ("adapter", Output::Single(adapter)) => modelfile.add_adapter(adapter), ("message", Output::Pair((role, message))) => modelfile.add_message(role, message), ("license", Output::Single(license)) => modelfile.add_license(license), - ("#", Output::Single(comment)) => modelfile.add_comment(comment), - _ => { - modelfile.errors.push(String::from("Invalid instruction")); + ("#", comment) => modelfile.add_comment(comment.to_string().as_str()), + (instruction, command) => { + modelfile.errors.push(format!( + "Invalid instruction Instruction: `{}` command: `{}`", + instruction, + command.to_string() + )); Ok(()) } }; @@ -437,6 +449,25 @@ mod tests { assert!(parse(modelfile).is_ok()); } + #[test] + fn test_invalid_instruction() { + let modelfile = " + FROM llama3.2 + adapter num_ctx 4096 + "; + + assert!(parse(modelfile).is_err()); + } + + #[test] + fn test_valid_comment() { + let modelfile = " + FROM llama3.2 + # system num_ctx 4096 + "; + + assert!(parse(modelfile).is_ok()); + } #[test] fn test_parse_modelfile_without_from() { From 0657cc1221f7add55afde4260a3183d0aeb3c246 Mon Sep 17 00:00:00 2001 From: madclaws Date: Sat, 18 Oct 2025 16:26:27 +0530 Subject: [PATCH 2/2] refactor: Removed temporary string allocation inside function call Instead allocating in such a way that the String is available throughtout the scope --- src/core/modelfile.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/modelfile.rs b/src/core/modelfile.rs index 3490bfc..a06eb3b 100644 --- a/src/core/modelfile.rs +++ b/src/core/modelfile.rs @@ -349,12 +349,14 @@ fn create_modelfile(commands: Vec<(&str, Output)>) -> Result ("adapter", Output::Single(adapter)) => modelfile.add_adapter(adapter), ("message", Output::Pair((role, message))) => modelfile.add_message(role, message), ("license", Output::Single(license)) => modelfile.add_license(license), - ("#", comment) => modelfile.add_comment(comment.to_string().as_str()), + ("#", comment) => { + let comment_str = comment.to_string(); + modelfile.add_comment(&comment_str) + } (instruction, command) => { modelfile.errors.push(format!( "Invalid instruction Instruction: `{}` command: `{}`", - instruction, - command.to_string() + instruction, command )); Ok(()) }