Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use serde::Deserialize;
use serde_json::{self, from_reader};
use std::io;
use std::fmt;
use std::fs::File;
use std::io;
use std::path::PathBuf;

use crate::key::Key;
Expand All @@ -27,7 +27,7 @@ pub enum JsonError {
/// The file specified by the key failed to open.
CannotOpenFile(PathBuf, io::Error),
/// The input key doesn’t provide enough information to open a file.
NoKey
NoKey,
}

impl fmt::Display for JsonError {
Expand All @@ -39,24 +39,21 @@ impl fmt::Display for JsonError {
write!(f, "cannot open file {}: {}", path.display(), e)
}

JsonError::NoKey => f.write_str("no path key available")
JsonError::NoKey => f.write_str("no path key available"),
}
}
}

impl<C, K, T> Load<C, K, Json> for T
where K: Key + Into<Option<PathBuf>>,
T: 'static + for<'de> Deserialize<'de> {
where
K: Key + Into<Option<PathBuf>>,
T: 'static + for<'de> Deserialize<'de>,
{
Copy link
Owner

@hadronized hadronized Sep 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can find a way to use rustfmt to have that curly brace on the line just above, I would happily merge! :

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phaazon I am new to rust. I found that brace_style doesn’t respect PreferSameLine and its an open issue in rustfmt itself - rust-lang/rustfmt#3299 which is opened by you. Can you please guide me about how to go about this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t really know what to do to fix this, besides fixing it on my own (or anyone who would like to).

type Error = JsonError;

fn load(
key: K,
_: &mut Storage<C, K>,
_: &mut C
) -> Result<Loaded<Self, K>, Self::Error> {
fn load(key: K, _: &mut Storage<C, K>, _: &mut C) -> Result<Loaded<Self, K>, Self::Error> {
if let Some(path) = key.into() {
let file = File::open(&path)
.map_err(|ioerr| JsonError::CannotOpenFile(path, ioerr))?;
let file = File::open(&path).map_err(|ioerr| JsonError::CannotOpenFile(path, ioerr))?;

from_reader(file)
.map(Loaded::without_dep)
Expand Down
24 changes: 16 additions & 8 deletions src/key.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Module exporting all key types recognized by this crate.

use any_cache::CacheKey;
use std::hash::{Hash, Hasher};
use std::fmt::{self, Display};
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::path::{Component, Path, PathBuf};

Expand Down Expand Up @@ -30,12 +30,13 @@ pub enum SimpleKey {
}

impl SimpleKey {
pub fn from_path<P>(path: P) -> Self where P: AsRef<Path> {
pub fn from_path<P>(path: P) -> Self
where P: AsRef<Path> {
SimpleKey::Path(path.as_ref().to_owned())
}
}

impl<'a> From<&'a Path> for SimpleKey {
impl<'a> From<&'a Path> for SimpleKey {
fn from(path: &Path) -> Self {
SimpleKey::from_path(path)
}
Expand All @@ -51,7 +52,7 @@ impl Into<Option<PathBuf>> for SimpleKey {
fn into(self) -> Option<PathBuf> {
match self {
SimpleKey::Path(path) => Some(path),
_ => None
_ => None,
}
}
}
Expand All @@ -72,7 +73,7 @@ impl Display for SimpleKey {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
SimpleKey::Path(ref path) => write!(f, "{}", path.display()),
SimpleKey::Logical(ref name) => write!(f, "{}", name)
SimpleKey::Logical(ref name) => write!(f, "{}", name),
}
}
}
Expand Down Expand Up @@ -108,12 +109,19 @@ impl<K, T> PrivateKey<K, T> {
}
}

impl<K, T> Hash for PrivateKey<K, T> where K: Hash {
fn hash<H>(&self, state: &mut H) where H: Hasher {
impl<K, T> Hash for PrivateKey<K, T>
where K: Hash
{
fn hash<H>(&self, state: &mut H)
where H: Hasher {
self.0.hash(state)
}
}

impl<K, T> CacheKey for PrivateKey<K, T> where T: 'static, K: Key {
impl<K, T> CacheKey for PrivateKey<K, T>
where
T: 'static,
K: Key,
{
type Target = Res<T>;
}
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,14 +574,19 @@
//! [RON]: https://github.com/ron-rs/ron

pub mod context;
#[cfg(feature = "json")] pub mod json;
#[cfg(feature = "ron-impl")] pub mod ron;
#[cfg(feature = "toml-impl")] pub mod toml;
#[cfg(feature = "json")]
pub mod json;
pub mod key;
pub mod load;
pub mod res;
#[cfg(feature = "ron-impl")]
pub mod ron;
#[cfg(feature = "toml-impl")]
pub mod toml;

pub use crate::context::Inspect;
pub use crate::key::{Key, SimpleKey};
pub use crate::load::{Discovery, Load, Loaded, Storage, Store, StoreError, StoreErrorOr, StoreOpt};
pub use crate::load::{
Discovery, Load, Loaded, Storage, Store, StoreError, StoreErrorOr, StoreOpt,
};
pub use crate::res::Res;
Loading