Skip to content

Commit

Permalink
rust: Drop glob for read_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhekhorn committed Aug 21, 2024
1 parent 229670a commit c6784d9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/dekoder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ rustdoc-args = ["--html-in-header", "doc-header.html"]

[dependencies]
tar = "0.4.41"
glob = "0.3.1"
float-cmp = "0.9.0"
yaml-rust2 = "0.8"
lz4_flex = "0.9.2"
Expand Down
26 changes: 14 additions & 12 deletions crates/dekoder/src/inventory.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
//! Assets manager.
use glob::glob;
use lz4_flex::frame::FrameDecoder;
use ndarray_npy::NpzReader;
use std::collections::HashMap;
use std::ffi::OsString;
use std::fs::{read_to_string, File};
use std::ffi::{OsStr, OsString};
use std::fs::{read_dir, read_to_string, File};
use std::io::Cursor;
use std::path::PathBuf;
use yaml_rust2::{Yaml, YamlLoader};

use crate::{EKOError, Operator, Result};

/// Headers are in yaml files.
const HEADER_EXT: &str = "*.yaml";
const HEADER_EXT: &str = "yaml";

/// Assets manager.
pub(crate) struct Inventory<K: Eq + for<'a> TryFrom<&'a Yaml, Error = EKOError>> {
Expand All @@ -25,16 +24,19 @@ pub(crate) struct Inventory<K: Eq + for<'a> TryFrom<&'a Yaml, Error = EKOError>>
impl<K: Eq + for<'a> TryFrom<&'a Yaml, Error = EKOError>> Inventory<K> {
/// Load all available entries.
pub fn load_keys(&mut self) -> Result<()> {
let path = self.path.join(HEADER_EXT);
let path = path
.to_str()
.ok_or(EKOError::KeyError("due to invalid path".to_owned()))?;
for entry in glob(path)
.map_err(|_| EKOError::KeyError("because failed to read glob pattern".to_owned()))?
.filter_map(core::result::Result::ok)
{
for entry in read_dir(&self.path)? {
// is header file?
let entry = entry?.path();
if !entry
.extension()
.is_some_and(|ext| ext.eq(OsStr::new(HEADER_EXT)))
{
continue;
}
// read
let cnt = YamlLoader::load_from_str(&read_to_string(&entry)?)
.map_err(|_| EKOError::KeyError("because failed to read yaml file.".to_owned()))?;
// add to register
self.keys.insert(
entry
.file_name()
Expand Down

0 comments on commit c6784d9

Please sign in to comment.