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