Skip to content

Commit

Permalink
9.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdaq committed Dec 25, 2022
1 parent 4545fd1 commit efd1186
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 9 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
[package]
name = "file-ext"
version = "8.0.1"
version = "9.0.0"
authors = ["Bohdan Tsap <[email protected]>"]
repository = "https://github.com/bohdaq/file-ext"
description = "Collection of utility functions to work with files"
edition = "2021"
license = "MIT OR Apache-2.0 OR ISC OR LGPL-3.0-or-later OR CC-BY-4.0"
categories = ["filesystem", "os", "embedded"]
keywords = ["file", "read", "create", "update", "delete"]
rust-version = "1.65"
rust-version = "1.66"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rust-web-server = "8.0.0"
27 changes: 27 additions & 0 deletions src/date_time_ext/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(test)]
mod tests;

pub struct DateTimeExt;

impl DateTimeExt {
pub fn _now_unix_epoch_nanos() -> u128 {
let now = SystemTime::now();
let nanos = DateTimeExt::_system_time_to_unix_nanos(now);
nanos
}

pub fn _system_time_to_unix_nanos(system_time: SystemTime) -> u128 {
let boxed_duration = system_time.duration_since(UNIX_EPOCH);
if boxed_duration.is_err() {
eprintln!("unable to get duration from system time {}", boxed_duration.err().unwrap());
let nanos = 0 as u128;
return nanos
}

let nanos = boxed_duration.unwrap().as_nanos();
nanos
}

}
16 changes: 16 additions & 0 deletions src/date_time_ext/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::time::SystemTime;
use crate::date_time_ext::DateTimeExt;


#[test]
fn system_to_nanos() {
let now = SystemTime::now();
let nanos = DateTimeExt::_system_time_to_unix_nanos(now);
assert_ne!(nanos, 0);
}

#[test]
fn now_as_nanos() {
let nanos = DateTimeExt::_now_unix_epoch_nanos();
assert_ne!(nanos, 0);
}
28 changes: 22 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,38 @@ use std::{env, fs};
use std::fs::{File, OpenOptions};
use std::io::{BufReader, Read, Seek, SeekFrom, Write};
use std::path::Path;
use rust_web_server::ext::date_time_ext::DateTimeExt;
use rust_web_server::range::Range;
use rust_web_server::symbol::SYMBOL;
use crate::date_time_ext::DateTimeExt;
use crate::symbol::SYMBOL;

#[cfg(test)]
mod tests;
mod date_time_ext;
mod symbol;

pub struct FileExt;

impl FileExt {

/// Returns portion of a file of specified range. Range described as starting from byte M up to byte N.
pub fn read_file_partially(filepath: &str, range: &Range) -> Result<Vec<u8>, String> {
/// # Examples
///
/// ```
/// use file_ext::FileExt;
/// #[test]
/// fn partial_read() {
/// let path = "test/index.html";
/// let file_raw_bytes = FileExt::read_file_partially(path, 4, 10).unwrap();
/// let content = String::from_utf8(file_raw_bytes).unwrap();
///
/// let expected_content = "CTYPE h";
///
/// assert_eq!(expected_content, content);
/// }
/// ```
pub fn read_file_partially(filepath: &str, start: u64, end: u64) -> Result<Vec<u8>, String> {
let mut file_content = Vec::new();

let buff_length = (range.end - range.start) + 1;
let buff_length = (end - start) + 1;
let boxed_open = File::open(filepath);
if boxed_open.is_err() {
let error_msg = boxed_open.err().unwrap();
Expand All @@ -28,7 +44,7 @@ impl FileExt {
let file = boxed_open.unwrap();
let mut reader = BufReader::new(file);

let boxed_seek = reader.seek(SeekFrom::Start(range.start));
let boxed_seek = reader.seek(SeekFrom::Start(start));
if boxed_seek.is_ok() {
let boxed_read = reader.take(buff_length).read_to_end(&mut file_content);
if boxed_read.is_err() {
Expand Down
42 changes: 42 additions & 0 deletions src/symbol/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#[cfg(test)]
mod tests;

pub struct Symbol {
pub new_line_carriage_return: &'static str,
pub new_line: &'static str,
pub carriage_return: &'static str,
pub empty_string: &'static str,
pub whitespace: &'static str,
pub equals: &'static str,
pub comma: &'static str,
pub hyphen: &'static str,
pub slash: &'static str,
pub semicolon: &'static str,
pub colon: &'static str,
pub number_sign: &'static str,
pub opening_square_bracket: &'static str,
pub closing_square_bracket: &'static str,
pub quotation_mark: &'static str,
pub underscore: &'static str,
pub single_quote: &'static str,
}

pub const SYMBOL: Symbol = Symbol {
new_line: "\n",
carriage_return: "\r",
new_line_carriage_return: "\r\n",
empty_string: "",
whitespace: " ",
equals: "=",
comma: ",",
hyphen: "-",
slash: "/",
semicolon: ";",
colon: ":",
number_sign: "#",
opening_square_bracket: "[",
closing_square_bracket: "]",
quotation_mark: "\"",
underscore: "_",
single_quote: "'",
};
16 changes: 16 additions & 0 deletions src/symbol/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::symbol::SYMBOL;

#[test]
fn symbol_check() {
assert_eq!(SYMBOL.new_line, "\n");
assert_eq!(SYMBOL.carriage_return, "\r");
assert_eq!(SYMBOL.new_line_carriage_return, "\r\n");
assert_eq!(SYMBOL.empty_string, "");
assert_eq!(SYMBOL.whitespace, " ");
assert_eq!(SYMBOL.equals, "=");
assert_eq!(SYMBOL.comma, ",");
assert_eq!(SYMBOL.hyphen, "-");
assert_eq!(SYMBOL.slash, "/");
assert_eq!(SYMBOL.semicolon, ";");
assert_eq!(SYMBOL.colon, ":");
}
11 changes: 11 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ fn file_content() {
assert_eq!(expected_content, content);
}

#[test]
fn partial_read() {
let path = "test/index.html";
let file_raw_bytes = FileExt::read_file_partially(path, 4, 10).unwrap();
let content = String::from_utf8(file_raw_bytes).unwrap();

let expected_content = "CTYPE h";

assert_eq!(expected_content, content);
}

#[test]
fn link_points_to() {
let path = "test/index_rewrite";
Expand Down

0 comments on commit efd1186

Please sign in to comment.