Separating file_linked package
This commit is contained in:
parent
04a70cd8f9
commit
acdfe269bd
6 changed files with 44 additions and 60 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,6 +1,6 @@
|
|||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
/gemla/target/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
|
|
11
file_linked/Cargo.toml
Normal file
11
file_linked/Cargo.toml
Normal file
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "file_linked"
|
||||
version = "0.1.0"
|
||||
authors = ["vandomej <jacob.vandome15@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
|
@ -1,17 +1,19 @@
|
|||
//! A wrapper around an object that ties it to a physical file
|
||||
//!
|
||||
|
||||
extern crate serde;
|
||||
|
||||
use std::fmt;
|
||||
use std::fs;
|
||||
use std::io::Read;
|
||||
use std::io::Write;
|
||||
use std::str::FromStr;
|
||||
use std::string::String;
|
||||
use std::string::ToString;
|
||||
use std::io::prelude::*;
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::Serialize;
|
||||
|
||||
/// A wrapper around an object `T` that ties the object to a physical file
|
||||
pub struct FileLinked<T>
|
||||
where
|
||||
T: ToString,
|
||||
T: Serialize,
|
||||
{
|
||||
val: T,
|
||||
path: String,
|
||||
|
@ -19,13 +21,13 @@ where
|
|||
|
||||
impl<T> FileLinked<T>
|
||||
where
|
||||
T: ToString,
|
||||
T: Serialize,
|
||||
{
|
||||
/// Returns a readonly reference of `T`
|
||||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use gemla::file_linked::*;
|
||||
/// # use file_linked::*;
|
||||
/// # use serde::{Deserialize, Serialize};
|
||||
/// # use std::fmt;
|
||||
/// # use std::string::ToString;
|
||||
|
@ -37,13 +39,6 @@ where
|
|||
/// # pub c: f64
|
||||
/// # }
|
||||
/// #
|
||||
/// # impl ToString for Test {
|
||||
/// # fn to_string(&self) -> String {
|
||||
/// # serde_json::to_string(self)
|
||||
/// # .expect("unable to deserialize")
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn main() {
|
||||
/// let test = Test {
|
||||
/// a: 1,
|
||||
|
@ -69,7 +64,7 @@ where
|
|||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use gemla::file_linked::*;
|
||||
/// # use file_linked::*;
|
||||
/// # use serde::{Deserialize, Serialize};
|
||||
/// # use std::fmt;
|
||||
/// # use std::string::ToString;
|
||||
|
@ -81,13 +76,6 @@ where
|
|||
/// pub c: f64
|
||||
/// }
|
||||
///
|
||||
/// impl ToString for Test {
|
||||
/// fn to_string(&self) -> String {
|
||||
/// serde_json::to_string(self)
|
||||
/// .expect("unable to deserialize")
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// # fn main() {
|
||||
/// let test = Test {
|
||||
/// a: 1,
|
||||
|
@ -121,7 +109,7 @@ where
|
|||
.open(&self.path)
|
||||
.map_err(|_| format!("Unable to open path {}", self.path))?;
|
||||
|
||||
write!(file, "{}", self.val.to_string())
|
||||
write!(file, "{}", serde_json::to_string(&self.val).map_err(|e| e.to_string())?)
|
||||
.or_else(|_| Err(String::from("Unable to write to file.")))?;
|
||||
|
||||
Ok(())
|
||||
|
@ -132,7 +120,7 @@ where
|
|||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use gemla::file_linked::*;
|
||||
/// # use file_linked::*;
|
||||
/// # use serde::{Deserialize, Serialize};
|
||||
/// # use std::fmt;
|
||||
/// # use std::string::ToString;
|
||||
|
@ -143,13 +131,6 @@ where
|
|||
/// # pub b: String,
|
||||
/// # pub c: f64
|
||||
/// # }
|
||||
/// #
|
||||
/// # impl ToString for Test {
|
||||
/// # fn to_string(&self) -> String {
|
||||
/// # serde_json::to_string(self)
|
||||
/// # .expect("unable to deserialize")
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn main() -> Result<(), String> {
|
||||
/// let test = Test {
|
||||
|
@ -184,7 +165,7 @@ where
|
|||
///
|
||||
/// # Examples
|
||||
/// ```
|
||||
/// # use gemla::file_linked::*;
|
||||
/// # use file_linked::*;
|
||||
/// # use serde::{Deserialize, Serialize};
|
||||
/// # use std::fmt;
|
||||
/// # use std::string::ToString;
|
||||
|
@ -196,13 +177,6 @@ where
|
|||
/// # pub c: f64
|
||||
/// # }
|
||||
/// #
|
||||
/// # impl ToString for Test {
|
||||
/// # fn to_string(&self) -> String {
|
||||
/// # serde_json::to_string(self)
|
||||
/// # .expect("unable to deserialize")
|
||||
/// # }
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn main() -> Result<(), String> {
|
||||
/// let test = Test {
|
||||
/// a: 1,
|
||||
|
@ -237,7 +211,7 @@ where
|
|||
|
||||
impl<T> FileLinked<T>
|
||||
where
|
||||
T: ToString + FromStr + Default,
|
||||
T: Serialize + DeserializeOwned + Default,
|
||||
{
|
||||
pub fn from_file(path: &str) -> Result<FileLinked<T>, String> {
|
||||
let meta = fs::metadata(path);
|
||||
|
@ -252,7 +226,7 @@ where
|
|||
file.read_to_string(&mut s)
|
||||
.map_err(|_| String::from("Unable to read from file."))?;
|
||||
|
||||
let val = T::from_str(&s)
|
||||
let val = serde_json::from_str(&s)
|
||||
.map_err(|_| String::from("Unable to parse value from file."))?;
|
||||
|
||||
Ok(FileLinked {
|
||||
|
@ -277,7 +251,7 @@ where
|
|||
|
||||
impl<T: fmt::Display> fmt::Display for FileLinked<T>
|
||||
where
|
||||
T: ToString,
|
||||
T: Serialize,
|
||||
{
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.val)
|
||||
|
@ -291,30 +265,28 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_mutate() -> Result<(), String> {
|
||||
let tree = btree!(1, btree!(2), btree!(3, btree!(4),));
|
||||
let mut linked_tree = FileLinked::new(tree, String::from("test.txt"))?;
|
||||
let list = vec![1, 2, 3, 4];
|
||||
let mut file_linked_list = FileLinked::new(list, String::from("test.txt"))?;
|
||||
|
||||
assert_eq!(
|
||||
format!("{}", linked_tree.readonly()),
|
||||
"{\"val\":1,\"left\":{\"val\":2,\"left\":null,\"right\":null},\"right\":{\"val\":3,\"left\":{\"val\":4,\"left\":null,\"right\":null},\"right\":null}}"
|
||||
format!("{:?}", file_linked_list.readonly()),
|
||||
"[1, 2, 3, 4]"
|
||||
);
|
||||
|
||||
linked_tree.mutate(|v1| v1.val = 10)?;
|
||||
file_linked_list.mutate(|v1| v1.push(5))?;
|
||||
|
||||
assert_eq!(
|
||||
format!("{}", linked_tree.readonly()),
|
||||
"{\"val\":10,\"left\":{\"val\":2,\"left\":null,\"right\":null},\"right\":{\"val\":3,\"left\":{\"val\":4,\"left\":null,\"right\":null},\"right\":null}}"
|
||||
format!("{:?}", file_linked_list.readonly()),
|
||||
"[1, 2, 3, 4, 5]"
|
||||
);
|
||||
|
||||
linked_tree.mutate(|v1| {
|
||||
let mut left = v1.left.clone().unwrap();
|
||||
left.val = 13;
|
||||
v1.left = Some(left);
|
||||
file_linked_list.mutate(|v1| {
|
||||
v1[1] = 1
|
||||
})?;
|
||||
|
||||
assert_eq!(
|
||||
format!("{}", linked_tree.readonly()),
|
||||
"{\"val\":10,\"left\":{\"val\":13,\"left\":null,\"right\":null},\"right\":{\"val\":3,\"left\":{\"val\":4,\"left\":null,\"right\":null},\"right\":null}}"
|
||||
format!("{:?}", file_linked_list.readonly()),
|
||||
"[1, 1, 3, 4, 5]"
|
||||
);
|
||||
|
||||
fs::remove_file("test.txt").expect("Unable to remove file");
|
|
@ -10,4 +10,5 @@ serde_json = "1.0"
|
|||
uuid = { version = "0.7", features = ["serde", "v4"] }
|
||||
clap = { version = "~2.27.0", features = ["yaml"] }
|
||||
toml = "0.5.8"
|
||||
regex = "1"
|
||||
regex = "1"
|
||||
file_linked = { path = "../file_linked" }
|
|
@ -3,9 +3,9 @@
|
|||
|
||||
pub mod genetic_node;
|
||||
|
||||
use super::file_linked::FileLinked;
|
||||
use super::tree;
|
||||
|
||||
use file_linked::FileLinked;
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
extern crate regex;
|
||||
extern crate file_linked;
|
||||
|
||||
#[macro_use]
|
||||
pub mod tree;
|
||||
pub mod bracket;
|
||||
pub mod constants;
|
||||
pub mod file_linked;
|
||||
|
|
Loading…
Add table
Reference in a new issue