Advertisement
Rengaw

crash_counter [refactored]

Jan 18th, 2021 (edited)
1,708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.28 KB | None | 0 0
  1. use app_dirs::{AppDataType, AppInfo};
  2. use std::env;
  3. use std::error::Error;
  4. use std::fs;
  5. use std::path::PathBuf;
  6. use std::str;
  7. use std::sync::mpsc;
  8. use std::time::Duration;
  9.  
  10. use notify::{RecursiveMode, Watcher};
  11.  
  12. type Receiver = mpsc::Receiver<notify::DebouncedEvent>;
  13. type Event = notify::DebouncedEvent;
  14.  
  15. const APP_INFO: AppInfo = AppInfo {
  16.     name: "crash_counter",
  17.     author: "Wagner",
  18. };
  19.  
  20. struct AppPaths {
  21.     crashes_dir: PathBuf,
  22.     count_file: PathBuf,
  23. }
  24.  
  25. fn main() -> Result<(), Box<dyn Error>> {
  26.     let paths = AppPaths {
  27.         crashes_dir: get_crashes_dir()?,
  28.         count_file: get_count_file()?,
  29.     };
  30.  
  31.     // Create a channel to receive the events.
  32.     let (tx, rx) = mpsc::channel();
  33.  
  34.     // Create a watcher object, delivering debounced events.
  35.     // The notification back-end is selected based on the platform.
  36.     let mut watcher = notify::watcher(tx, Duration::from_secs(10))?;
  37.  
  38.     watcher.watch(&paths.crashes_dir, RecursiveMode::Recursive)?;
  39.  
  40.     receive(rx, &paths)?;
  41.  
  42.     Ok(())
  43. }
  44.  
  45. fn get_crashes_dir() -> Result<PathBuf, Box<dyn Error>> {
  46.     let temp_dir = env::temp_dir();
  47.     let crashes_dir = temp_dir.join("Unity").join("Editor").join("Crashes");
  48.     if !crashes_dir.exists() {
  49.         println!("Creating directory: {:?}", crashes_dir);
  50.         fs::create_dir_all(&crashes_dir)?;
  51.     }
  52.     Ok(crashes_dir)
  53. }
  54.  
  55. fn get_count_file() -> Result<PathBuf, Box<dyn Error>> {
  56.     let count_file = app_dirs::app_root(AppDataType::UserData, &APP_INFO)?.join("count.txt");
  57.  
  58.     if !count_file.exists() {
  59.         fs::write(&count_file, b"0")?;
  60.     }
  61.  
  62.     Ok(count_file)
  63. }
  64.  
  65. fn receive(rx: Receiver, paths: &AppPaths) -> Result<(), Box<dyn Error>> {
  66.     println!("Listening for events...");
  67.     loop {
  68.         match rx.recv() {
  69.             Ok(event) => handle_event(event, paths)?,
  70.             Err(error) => println!("Watch error: {:?}", error),
  71.         }
  72.     }
  73. }
  74.  
  75. fn handle_event(event: Event, paths: &AppPaths) -> Result<(), Box<dyn Error>> {
  76.     let count = match fs::read(&paths.count_file) {
  77.         Ok(contents) => str::from_utf8(&contents)?.parse::<u128>()?,
  78.         Err(_) => 0,
  79.     } + 1;
  80.  
  81.     std::fs::write(&paths.count_file, count.to_string())?;
  82.  
  83.     println!("{:?} | count = {}", event, count);
  84.  
  85.     Ok(())
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement