<commit_before>//! Resource system.
//!
//! Currently, a resource is a disk-cached object that can be hot-reloaded while you use it.
//! Resource can be serialized and deserialized as you see fit. The concept of *caching* and
//! *loading* are split in different code location so that you can easily compose both – provide
//! the loading code and ask the resource system to cache it for you.
//!
//! This great flexibility is exposed in the public interface so that the cache can be augmented
//! with user-provided objects. You might be interested in implementing `Load`, `CacheKey` – from
//! the [any-cache](https://crates.io/crates/any-cache) crate — as well as providing a type wrapper
//! over the key to access to your resource.
//!
//! # Note on keys
//!
//! If you use the resource system, your resources will be cached and accessible by their key. The
//! key type is not enforced. Resource’s keys are typed to enable namespacing: if you have two
//! resources which ID is `34`, because the key type is different, you can safely cache the resource
//! with the ID `34` without any clashing or undefined behaviors. More in the any-cache crate.

use any_cache::{Cache, HashCache};
pub use any_cache::CacheKey;
use notify::{Op, RawEvent, RecursiveMode, Watcher, raw_watcher};
use notify::op::WRITE;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::channel;
use std::thread;
use std::time::{Duration, Instant};

/// Loadable object from disk.
///
/// An object can be loaded from disk if given a path it can be output a `LoadResult<_>`. It’s
/// important to note that you’re not supposed to load objects directly from this trait. Instead,
/// you should use a `Store`.
pub trait Load: 'static + Sized {
  // /// Convert from a key to its path representation.
  // fn key_to_path(key: &Self::Key) -> PathBuf;

  /// Load a resource. The `Store` can be used to load or declare additional resource dependencies.
  /// The result type is used to register for dependency events.
  fn load<P>(path: P, cache: &mut Store) -> Result<LoadResult<Self>, LoadError> where P: AsRef<Path>;
}

/// Result of a resource loading. This type enables you to register a resource for reloading events
/// of others (dependencies). If you don’t need to run specific code on a dependency reloading, use
/// the `.into()` function to lift your return value to `LoadResult<_>`.
pub struct LoadResult<T> {
  /// The loaded object.
  res: T,
  /// The list of dependencies to listen for events.
  dependencies: Vec<PathBuf>
}

impl<T> LoadResult<T> {
  pub fn with_dependencies(res: T, dependencies: Vec<PathBuf>) -> Self {
    LoadResult { res, dependencies }
  }
}

impl<T> From<T> for LoadResult<T> {
  fn from(res: T) -> Self {
    LoadResult {
      res,
      dependencies: Vec::new()
    }
  }
}<commit_msg>Remove some useless commented code.<commit_after>//! Resource system.
//!
//! Currently, a resource is a disk-cached object that can be hot-reloaded while you use it.
//! Resource can be serialized and deserialized as you see fit. The concept of *caching* and
//! *loading* are split in different code location so that you can easily compose both – provide
//! the loading code and ask the resource system to cache it for you.
//!
//! This great flexibility is exposed in the public interface so that the cache can be augmented
//! with user-provided objects. You might be interested in implementing `Load`, `CacheKey` – from
//! the [any-cache](https://crates.io/crates/any-cache) crate — as well as providing a type wrapper
//! over the key to access to your resource.
//!
//! # Note on keys
//!
//! If you use the resource system, your resources will be cached and accessible by their key. The
//! key type is not enforced. Resource’s keys are typed to enable namespacing: if you have two
//! resources which ID is `34`, because the key type is different, you can safely cache the resource
//! with the ID `34` without any clashing or undefined behaviors. More in the any-cache crate.

use any_cache::{Cache, HashCache};
pub use any_cache::CacheKey;
use notify::{Op, RawEvent, RecursiveMode, Watcher, raw_watcher};
use notify::op::WRITE;
use std::cell::RefCell;
use std::collections::HashMap;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::channel;
use std::thread;
use std::time::{Duration, Instant};

/// Loadable object from disk.
///
/// An object can be loaded from disk if given a path it can be output a `LoadResult<_>`. It’s
/// important to note that you’re not supposed to load objects directly from this trait. Instead,
/// you should use a `Store`.
pub trait Load: 'static + Sized {
  /// Load a resource. The `Store` can be used to load or declare additional resource dependencies.
  /// The result type is used to register for dependency events.
  fn load<P>(path: P, cache: &mut Store) -> Result<LoadResult<Self>, LoadError> where P: AsRef<Path>;
}

/// Result of a resource loading. This type enables you to register a resource for reloading events
/// of others (dependencies). If you don’t need to run specific code on a dependency reloading, use
/// the `.into()` function to lift your return value to `LoadResult<_>`.
pub struct LoadResult<T> {
  /// The loaded object.
  res: T,
  /// The list of dependencies to listen for events.
  dependencies: Vec<PathBuf>
}

impl<T> LoadResult<T> {
  pub fn with_dependencies(res: T, dependencies: Vec<PathBuf>) -> Self {
    LoadResult { res, dependencies }
  }
}

impl<T> From<T> for LoadResult<T> {
  fn from(res: T) -> Self {
    LoadResult {
      res,
      dependencies: Vec::new()
    }
  }
}
{'fraction_non_alphanumeric': 0.11813759555246699, 'fraction_numerical': 0.001389854065323141, 'mean_word_length': 3.742174629324547, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 38, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4697314', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1619, 'n_words': 776}