Factor FileRoute into separate module

This commit is contained in:
Lucien Greathouse 2018-04-12 22:14:14 -07:00
parent 610db157c8
commit c61445ddaa
3 changed files with 47 additions and 44 deletions

37
server/src/file_route.rs Normal file
View file

@ -0,0 +1,37 @@
use std::path::{Path, PathBuf};
// TODO: Add lifetime, switch to using Cow<'a, str> instead of String? It's
// possible that it would be too cumbersome!
#[derive(Debug, Clone, PartialEq, Hash)]
pub struct FileRoute {
pub partition: String,
pub route: Vec<String>,
}
impl FileRoute {
/// Creates a PathBuf out of the `FileRoute` based on the given partition
/// `Path`.
// TODO: Tests
pub fn to_path_buf(&self, partition_path: &Path) -> PathBuf {
let mut result = partition_path.to_path_buf();
for route_piece in &self.route {
result.push(route_piece);
}
result
}
/// Creates a version of the FileRoute with the given extra pieces appended
/// to the end.
// TODO: Test
pub fn extended_with(&self, pieces: &[&str]) -> FileRoute {
let mut result = self.clone();
for piece in pieces {
result.route.push(piece.to_string());
}
result
}
}

View file

@ -10,13 +10,14 @@ extern crate regex;
#[cfg(test)]
extern crate tempfile;
pub mod rbx_session;
pub mod commands;
pub mod file_route;
pub mod id;
pub mod middleware;
pub mod pathext;
pub mod project;
pub mod rbx;
pub mod rbx_session;
pub mod vfs;
pub mod web;
pub mod web_util;
pub mod project;
pub mod pathext;
pub mod vfs;
pub mod rbx;
pub mod middleware;
pub mod commands;

View file

@ -1,45 +1,10 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::collections::HashMap;
use std::io::Read;
use std::fs::{self, File};
use id::{Id};
// TODO: Add lifetime, switch to using Cow<'a, str> instead of String? It's
// possible that it would be too cumbersome!
#[derive(Debug, Clone, PartialEq, Hash)]
struct FileRoute {
pub partition: String,
pub route: Vec<String>,
}
impl FileRoute {
/// Creates a PathBuf out of the `FileRoute` based on the given partition
/// `Path`.
// TODO: Tests
pub fn to_path_buf(&self, partition_path: &Path) -> PathBuf {
let mut result = partition_path.to_path_buf();
for route_piece in &self.route {
result.push(route_piece);
}
result
}
/// Creates a version of the FileRoute with the given extra pieces appended
/// to the end.
// TODO: Test
pub fn extended_with(&self, pieces: &[&str]) -> FileRoute {
let mut result = self.clone();
for piece in pieces {
result.route.push(piece.to_string());
}
result
}
}
use file_route::FileRoute;
/// Represents a file or directory that has been read from the filesystem.
// TODO: Keep track of file Path or FileRoute?