Skip to main content

llama_cpp_wrapper_sources/
wrapper_sources_response_file.rs

1use std::fs;
2use std::path::PathBuf;
3
4use crate::wrapper_source_paths::wrapper_source_paths;
5use crate::wrapper_sources_error::WrapperSourcesError;
6
7pub struct WrapperSourcesResponseFile {
8    pub output_path: PathBuf,
9    pub sys_dir: PathBuf,
10}
11
12impl WrapperSourcesResponseFile {
13    /// # Errors
14    ///
15    /// Returns [`WrapperSourcesError`] when the response file cannot be written.
16    pub fn write(&self) -> Result<(), WrapperSourcesError> {
17        let mut contents = wrapper_source_paths(&self.sys_dir)
18            .into_iter()
19            .map(|source_path| format!("\"{}\"", source_path.display()))
20            .collect::<Vec<String>>()
21            .join("\n");
22
23        contents.push('\n');
24
25        fs::write(&self.output_path, contents).map_err(|source| WrapperSourcesError::Write {
26            path: self.output_path.clone(),
27            source,
28        })
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use std::path::Path;
35    use std::path::PathBuf;
36
37    use super::WrapperSourcesResponseFile;
38    use crate::wrapper_sources_error::WrapperSourcesError;
39
40    #[test]
41    fn writing_under_a_missing_directory_reports_the_path() {
42        let output_path = "/nonexistent-directory/wrapper_sources.rsp";
43        let response_file = WrapperSourcesResponseFile {
44            output_path: PathBuf::from(output_path),
45            sys_dir: PathBuf::from("/repo/llama-cpp-bindings-sys"),
46        };
47
48        assert!(matches!(
49            response_file.write(),
50            Err(WrapperSourcesError::Write { ref path, .. }) if path == Path::new(output_path)
51        ));
52    }
53}