lib.rs 681 B

123456789101112131415161718192021222324252627
  1. use std::{
  2. fs::{create_dir_all, File},
  3. io::{self, Write},
  4. path::Path,
  5. };
  6. pub mod bindgen;
  7. pub mod generate;
  8. pub mod rustfmt;
  9. pub use generate::{generate, InputFile};
  10. pub fn write_to_file<T: AsRef<Path>>(path: T, code: &str) -> Result<(), io::Error> {
  11. // Create parent directories if they don't exist already
  12. if let Some(parent) = path.as_ref().parent() {
  13. if !parent.exists() {
  14. create_dir_all(parent)?;
  15. }
  16. }
  17. let mut file = File::create(path)?;
  18. file.write_all(code.as_bytes())
  19. }
  20. pub fn write_to_file_fmt<T: AsRef<Path>>(path: T, code: &str) -> Result<(), io::Error> {
  21. write_to_file(path, &rustfmt::format(code)?)
  22. }