Move LexicographicFileWalker into its own source file
This commit is contained in:
parent
63c9d599f9
commit
797db7a097
@ -1,21 +1,21 @@
|
|||||||
extern crate clap;
|
extern crate clap;
|
||||||
|
|
||||||
|
mod lexicographic_file_walker;
|
||||||
mod osm;
|
mod osm;
|
||||||
|
|
||||||
use core::panic;
|
use core::panic;
|
||||||
use std::collections::{HashMap, HashSet, VecDeque};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::error::Error;
|
use std::fs::{create_dir_all, File};
|
||||||
use std::fs::{create_dir_all, DirEntry, File};
|
use std::io::{Read, Write};
|
||||||
use std::io::{self, Read, Write};
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::str::FromStr;
|
use std::{env, fs};
|
||||||
use std::{env, fmt, fs};
|
|
||||||
|
|
||||||
use cheap_ruler::CheapRuler;
|
use cheap_ruler::CheapRuler;
|
||||||
use clap::{Arg, Parser};
|
use clap::Parser;
|
||||||
use cucumber::{self, gherkin::Step, given, when, World};
|
use cucumber::{self, gherkin::Step, given, when, World};
|
||||||
use futures::{future, FutureExt};
|
use futures::{future, FutureExt};
|
||||||
use geo_types::{point, Point};
|
use geo_types::{point, Point};
|
||||||
|
use lexicographic_file_walker::LexicographicFileWalker;
|
||||||
use osm::{OSMDb, OSMNode, OSMWay};
|
use osm::{OSMDb, OSMNode, OSMWay};
|
||||||
|
|
||||||
#[derive(Debug, Default, World)]
|
#[derive(Debug, Default, World)]
|
||||||
@ -235,57 +235,7 @@ fn get_file_as_byte_vec(path: &PathBuf) -> Vec<u8> {
|
|||||||
buffer
|
buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: port into toolbox-rs
|
#[derive(clap::ValueEnum, Clone, Default, Debug)]
|
||||||
struct LexicographicFileWalker {
|
|
||||||
dirs: VecDeque<PathBuf>,
|
|
||||||
files: VecDeque<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LexicographicFileWalker {
|
|
||||||
pub fn new(path: &PathBuf) -> Self {
|
|
||||||
let mut dirs = VecDeque::new();
|
|
||||||
|
|
||||||
if path.is_dir() {
|
|
||||||
dirs.push_back(path.clone());
|
|
||||||
}
|
|
||||||
|
|
||||||
Self {
|
|
||||||
dirs,
|
|
||||||
files: VecDeque::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Iterator for LexicographicFileWalker {
|
|
||||||
type Item = PathBuf;
|
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
|
||||||
if self.dirs.is_empty() && self.files.is_empty() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
while self.files.is_empty() && !self.dirs.is_empty() {
|
|
||||||
assert!(!self.dirs.is_empty());
|
|
||||||
let current_dir = self.dirs.pop_front().unwrap();
|
|
||||||
let mut temp_dirs = Vec::new();
|
|
||||||
|
|
||||||
for entry in fs::read_dir(current_dir).unwrap() {
|
|
||||||
let entry = entry.unwrap();
|
|
||||||
let path = entry.path();
|
|
||||||
if path.is_dir() {
|
|
||||||
temp_dirs.push(path.clone());
|
|
||||||
} else {
|
|
||||||
self.files.push_back(path.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.files.make_contiguous().sort();
|
|
||||||
temp_dirs.sort();
|
|
||||||
self.dirs.extend(temp_dirs.into_iter());
|
|
||||||
}
|
|
||||||
return self.files.pop_front();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive( clap::ValueEnum, Clone, Default, Debug)]
|
|
||||||
enum LoadMethod {
|
enum LoadMethod {
|
||||||
Mmap,
|
Mmap,
|
||||||
#[default]
|
#[default]
|
||||||
@ -330,7 +280,6 @@ struct Args {
|
|||||||
p: RoutingAlgorithm,
|
p: RoutingAlgorithm,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
println!("name: {:?}", args);
|
println!("name: {:?}", args);
|
||||||
|
51
tests/lexicographic_file_walker.rs
Normal file
51
tests/lexicographic_file_walker.rs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
use std::{collections::VecDeque, fs, path::PathBuf};
|
||||||
|
|
||||||
|
// TODO: port into toolbox-rs
|
||||||
|
pub struct LexicographicFileWalker {
|
||||||
|
dirs: VecDeque<PathBuf>,
|
||||||
|
files: VecDeque<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LexicographicFileWalker {
|
||||||
|
pub fn new(path: &PathBuf) -> Self {
|
||||||
|
let mut dirs = VecDeque::new();
|
||||||
|
|
||||||
|
if path.is_dir() {
|
||||||
|
dirs.push_back(path.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
Self {
|
||||||
|
dirs,
|
||||||
|
files: VecDeque::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Iterator for LexicographicFileWalker {
|
||||||
|
type Item = PathBuf;
|
||||||
|
|
||||||
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
if self.dirs.is_empty() && self.files.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
while self.files.is_empty() && !self.dirs.is_empty() {
|
||||||
|
assert!(!self.dirs.is_empty());
|
||||||
|
let current_dir = self.dirs.pop_front().unwrap();
|
||||||
|
let mut temp_dirs = Vec::new();
|
||||||
|
|
||||||
|
for entry in fs::read_dir(current_dir).unwrap() {
|
||||||
|
let entry = entry.unwrap();
|
||||||
|
let path = entry.path();
|
||||||
|
if path.is_dir() {
|
||||||
|
temp_dirs.push(path.clone());
|
||||||
|
} else {
|
||||||
|
self.files.push_back(path.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.files.make_contiguous().sort();
|
||||||
|
temp_dirs.sort();
|
||||||
|
self.dirs.extend(temp_dirs.into_iter());
|
||||||
|
}
|
||||||
|
return self.files.pop_front();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user