Skip to content

Commit

Permalink
Review feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Aug 25, 2020
1 parent b722890 commit 4362245
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 26 deletions.
8 changes: 3 additions & 5 deletions components/cldr-json-data-provider/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ use std::path::PathBuf;

/// Helper function to open a file and return failures as a crate error.
pub fn open_reader(path: PathBuf) -> Result<BufReader<File>, Error> {
let file = match File::open(&path) {
Ok(file) => file,
Err(err) => return Err(Error::IoError(err, path)),
};
Ok(BufReader::new(file))
File::open(&path)
.map(BufReader::new)
.map_err(|e| Error::IoError(e, path))
}
32 changes: 16 additions & 16 deletions components/cldr-json-data-provider/src/support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ pub(crate) struct LazyCldrProvider<T> {
src: RwLock<Option<T>>,
}

impl<T> Default for LazyCldrProvider<T> {
fn default() -> Self {
LazyCldrProvider {
src: RwLock::new(None),
}
}
}

fn map_poison<E>(_err: E) -> DataError {
// Can't return the PoisonError directly because it has lifetime parameters.
DataError::new_resc_error(crate::error::Error::PoisonError)
Expand All @@ -23,12 +31,6 @@ where
T: DataProvider<'d> + DataKeySupport + DataEntryCollection + TryFrom<&'b CldrPaths>,
<T as TryFrom<&'b CldrPaths>>::Error: 'static + std::error::Error,
{
pub fn new() -> Self {
Self {
src: RwLock::new(None),
}
}

/// Call T::load, initializing T if necessary.
pub fn try_load(
&self,
Expand All @@ -45,11 +47,10 @@ where
if src.is_none() {
src.replace(T::try_from(cldr_paths).map_err(DataError::new_resc_error)?);
}
// The RwLock is guaranteed to be populated at this point.
if let Some(data_provider) = src.as_ref() {
return data_provider.load(req).map(Some);
}
unreachable!();
let data_provider = src
.as_ref()
.expect("The RwLock must be populated at this point.");
data_provider.load(req).map(Some)
}

/// Call T::iter_for_key, initializing T if necessary.
Expand All @@ -68,10 +69,9 @@ where
if src.is_none() {
src.replace(T::try_from(cldr_paths).map_err(DataError::new_resc_error)?);
}
// The RwLock is guaranteed to be populated at this point.
if let Some(data_provider) = src.as_ref() {
return data_provider.iter_for_key(data_key).map(Some);
}
unreachable!();
let data_provider = src
.as_ref()
.expect("The RwLock must be populated at this point.");
data_provider.iter_for_key(data_key).map(Some)
}
}
9 changes: 4 additions & 5 deletions components/cldr-json-data-provider/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ impl<'a, 'd> CldrJsonDataProvider<'a, 'd> {
pub fn new(cldr_paths: &'a CldrPaths) -> Self {
CldrJsonDataProvider {
cldr_paths,
plurals: LazyCldrProvider::new(),
plurals: Default::default(),
}
}
}

impl<'a, 'd> DataProvider<'d> for CldrJsonDataProvider<'a, 'd> {
fn load(&self, req: &DataRequest) -> Result<DataResponse<'d>, DataError> {
if let Some(resp) = self.plurals.try_load(req, &self.cldr_paths)? {
return Ok(resp);
}
Err(DataError::UnsupportedDataKey(req.data_key))
self.plurals
.try_load(req, &self.cldr_paths)?
.ok_or(DataError::UnsupportedDataKey(req.data_key))
}
}

Expand Down

0 comments on commit 4362245

Please sign in to comment.