llama_cpp_wrapper_sources/
wrapper_build_inputs.rs1use std::path::PathBuf;
2
3use crate::compile_commands_file::CompileCommandsFile;
4use crate::wrapper_sources_error::WrapperSourcesError;
5use crate::wrapper_sources_response_file::WrapperSourcesResponseFile;
6
7fn required_argument(
8 arguments: &mut impl Iterator<Item = String>,
9 name: &'static str,
10) -> Result<String, WrapperSourcesError> {
11 arguments
12 .next()
13 .ok_or(WrapperSourcesError::MissingArgument { name })
14}
15
16pub struct WrapperBuildInputs {
18 compile_commands_file: CompileCommandsFile,
19 response_file: WrapperSourcesResponseFile,
20}
21
22impl WrapperBuildInputs {
23 pub fn from_arguments(
30 mut arguments: impl Iterator<Item = String>,
31 ) -> Result<Self, WrapperSourcesError> {
32 let sys_dir = PathBuf::from(required_argument(&mut arguments, "sys-dir")?);
33 let llama_cpp_dir = PathBuf::from(required_argument(&mut arguments, "llama-cpp-dir")?);
34 let compile_commands_path = required_argument(&mut arguments, "compile-commands-path")?;
35 let response_file_path = required_argument(&mut arguments, "response-file-path")?;
36
37 Ok(Self {
38 compile_commands_file: CompileCommandsFile {
39 output_path: PathBuf::from(compile_commands_path),
40 sys_dir: sys_dir.clone(),
41 llama_cpp_dir,
42 },
43 response_file: WrapperSourcesResponseFile {
44 output_path: PathBuf::from(response_file_path),
45 sys_dir,
46 },
47 })
48 }
49
50 pub fn write(&self) -> Result<(), WrapperSourcesError> {
55 self.compile_commands_file.write()?;
56 self.response_file.write()
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use std::path::PathBuf;
63
64 use super::WrapperBuildInputs;
65
66 const ARGUMENTS: [&str; 4] = [
67 "/repo/llama-cpp-bindings-sys",
68 "/repo/llama-cpp-native/llama.cpp",
69 "/repo/target/compile_commands.json",
70 "/repo/target/wrapper_sources.rsp",
71 ];
72
73 fn missing_argument_message(given: &[&str]) -> Option<String> {
74 WrapperBuildInputs::from_arguments(given.iter().map(|argument| (*argument).to_owned()))
75 .err()
76 .map(|error| error.to_string())
77 }
78
79 #[test]
80 fn the_first_absent_argument_is_named() {
81 assert_eq!(
82 [
83 missing_argument_message(&ARGUMENTS[..0]),
84 missing_argument_message(&ARGUMENTS[..1]),
85 missing_argument_message(&ARGUMENTS[..2]),
86 missing_argument_message(&ARGUMENTS[..3]),
87 missing_argument_message(&ARGUMENTS),
88 ],
89 [
90 Some("required command line argument <sys-dir> is missing".to_owned()),
91 Some("required command line argument <llama-cpp-dir> is missing".to_owned()),
92 Some(
93 "required command line argument <compile-commands-path> is missing".to_owned()
94 ),
95 Some("required command line argument <response-file-path> is missing".to_owned()),
96 None,
97 ]
98 );
99 }
100
101 #[test]
102 fn both_files_are_written_for_the_sys_and_llama_cpp_dirs_at_the_paths_given_in_order() {
103 let inputs = WrapperBuildInputs::from_arguments(
104 ARGUMENTS.iter().map(|argument| (*argument).to_owned()),
105 )
106 .expect("every argument is given");
107
108 assert_eq!(
109 [
110 &inputs.compile_commands_file.sys_dir,
111 &inputs.compile_commands_file.llama_cpp_dir,
112 &inputs.compile_commands_file.output_path,
113 &inputs.response_file.sys_dir,
114 &inputs.response_file.output_path,
115 ],
116 [
117 &PathBuf::from(ARGUMENTS[0]),
118 &PathBuf::from(ARGUMENTS[1]),
119 &PathBuf::from(ARGUMENTS[2]),
120 &PathBuf::from(ARGUMENTS[0]),
121 &PathBuf::from(ARGUMENTS[3]),
122 ]
123 );
124 }
125}