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::PathBuf;
35
36    use llama_cpp_error_chain::error_chain::error_chain;
37
38    use super::WrapperSourcesResponseFile;
39
40    #[test]
41    fn writing_under_a_missing_directory_reports_the_path() {
42        let response_file = WrapperSourcesResponseFile {
43            output_path: PathBuf::from("/nonexistent-directory/wrapper_sources.rsp"),
44            sys_dir: PathBuf::from("/repo/llama-cpp-bindings-sys"),
45        };
46
47        assert_eq!(
48            response_file.write().err().map(|error| error_chain(&error)),
49            Some(
50                "wrapper source list could not be written to \
51                 /nonexistent-directory/wrapper_sources.rsp: No such file or directory (os error 2)"
52                    .to_owned()
53            )
54        );
55    }
56}