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
|
# Generated by Cargo
|
||||||
# will have compiled files and executables
|
# will have compiled files and executables
|
||||||
/gemla/target/
|
target/
|
||||||
|
|
||||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
# 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
|
# 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
|
//! A wrapper around an object that ties it to a physical file
|
||||||
|
//!
|
||||||
|
|
||||||
|
extern crate serde;
|
||||||
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Read;
|
use std::io::prelude::*;
|
||||||
use std::io::Write;
|
|
||||||
use std::str::FromStr;
|
use serde::de::DeserializeOwned;
|
||||||
use std::string::String;
|
use serde::Serialize;
|
||||||
use std::string::ToString;
|
|
||||||
|
|
||||||
/// A wrapper around an object `T` that ties the object to a physical file
|
/// A wrapper around an object `T` that ties the object to a physical file
|
||||||
pub struct FileLinked<T>
|
pub struct FileLinked<T>
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: Serialize,
|
||||||
{
|
{
|
||||||
val: T,
|
val: T,
|
||||||
path: String,
|
path: String,
|
||||||
|
@ -19,13 +21,13 @@ where
|
||||||
|
|
||||||
impl<T> FileLinked<T>
|
impl<T> FileLinked<T>
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: Serialize,
|
||||||
{
|
{
|
||||||
/// Returns a readonly reference of `T`
|
/// Returns a readonly reference of `T`
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// # use gemla::file_linked::*;
|
/// # use file_linked::*;
|
||||||
/// # use serde::{Deserialize, Serialize};
|
/// # use serde::{Deserialize, Serialize};
|
||||||
/// # use std::fmt;
|
/// # use std::fmt;
|
||||||
/// # use std::string::ToString;
|
/// # use std::string::ToString;
|
||||||
|
@ -37,13 +39,6 @@ where
|
||||||
/// # pub c: f64
|
/// # pub c: f64
|
||||||
/// # }
|
/// # }
|
||||||
/// #
|
/// #
|
||||||
/// # impl ToString for Test {
|
|
||||||
/// # fn to_string(&self) -> String {
|
|
||||||
/// # serde_json::to_string(self)
|
|
||||||
/// # .expect("unable to deserialize")
|
|
||||||
/// # }
|
|
||||||
/// # }
|
|
||||||
/// #
|
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// let test = Test {
|
/// let test = Test {
|
||||||
/// a: 1,
|
/// a: 1,
|
||||||
|
@ -69,7 +64,7 @@ where
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// # use gemla::file_linked::*;
|
/// # use file_linked::*;
|
||||||
/// # use serde::{Deserialize, Serialize};
|
/// # use serde::{Deserialize, Serialize};
|
||||||
/// # use std::fmt;
|
/// # use std::fmt;
|
||||||
/// # use std::string::ToString;
|
/// # use std::string::ToString;
|
||||||
|
@ -81,13 +76,6 @@ where
|
||||||
/// pub c: f64
|
/// pub c: f64
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// impl ToString for Test {
|
|
||||||
/// fn to_string(&self) -> String {
|
|
||||||
/// serde_json::to_string(self)
|
|
||||||
/// .expect("unable to deserialize")
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// # fn main() {
|
/// # fn main() {
|
||||||
/// let test = Test {
|
/// let test = Test {
|
||||||
/// a: 1,
|
/// a: 1,
|
||||||
|
@ -121,7 +109,7 @@ where
|
||||||
.open(&self.path)
|
.open(&self.path)
|
||||||
.map_err(|_| format!("Unable to open path {}", 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.")))?;
|
.or_else(|_| Err(String::from("Unable to write to file.")))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -132,7 +120,7 @@ where
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// # use gemla::file_linked::*;
|
/// # use file_linked::*;
|
||||||
/// # use serde::{Deserialize, Serialize};
|
/// # use serde::{Deserialize, Serialize};
|
||||||
/// # use std::fmt;
|
/// # use std::fmt;
|
||||||
/// # use std::string::ToString;
|
/// # use std::string::ToString;
|
||||||
|
@ -144,13 +132,6 @@ where
|
||||||
/// # pub c: f64
|
/// # 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> {
|
/// # fn main() -> Result<(), String> {
|
||||||
/// let test = Test {
|
/// let test = Test {
|
||||||
/// a: 1,
|
/// a: 1,
|
||||||
|
@ -184,7 +165,7 @@ where
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
/// ```
|
/// ```
|
||||||
/// # use gemla::file_linked::*;
|
/// # use file_linked::*;
|
||||||
/// # use serde::{Deserialize, Serialize};
|
/// # use serde::{Deserialize, Serialize};
|
||||||
/// # use std::fmt;
|
/// # use std::fmt;
|
||||||
/// # use std::string::ToString;
|
/// # use std::string::ToString;
|
||||||
|
@ -196,13 +177,6 @@ where
|
||||||
/// # pub c: f64
|
/// # 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> {
|
/// # fn main() -> Result<(), String> {
|
||||||
/// let test = Test {
|
/// let test = Test {
|
||||||
/// a: 1,
|
/// a: 1,
|
||||||
|
@ -237,7 +211,7 @@ where
|
||||||
|
|
||||||
impl<T> FileLinked<T>
|
impl<T> FileLinked<T>
|
||||||
where
|
where
|
||||||
T: ToString + FromStr + Default,
|
T: Serialize + DeserializeOwned + Default,
|
||||||
{
|
{
|
||||||
pub fn from_file(path: &str) -> Result<FileLinked<T>, String> {
|
pub fn from_file(path: &str) -> Result<FileLinked<T>, String> {
|
||||||
let meta = fs::metadata(path);
|
let meta = fs::metadata(path);
|
||||||
|
@ -252,7 +226,7 @@ where
|
||||||
file.read_to_string(&mut s)
|
file.read_to_string(&mut s)
|
||||||
.map_err(|_| String::from("Unable to read from file."))?;
|
.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."))?;
|
.map_err(|_| String::from("Unable to parse value from file."))?;
|
||||||
|
|
||||||
Ok(FileLinked {
|
Ok(FileLinked {
|
||||||
|
@ -277,7 +251,7 @@ where
|
||||||
|
|
||||||
impl<T: fmt::Display> fmt::Display for FileLinked<T>
|
impl<T: fmt::Display> fmt::Display for FileLinked<T>
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: Serialize,
|
||||||
{
|
{
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
write!(f, "{}", self.val)
|
write!(f, "{}", self.val)
|
||||||
|
@ -291,30 +265,28 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_mutate() -> Result<(), String> {
|
fn test_mutate() -> Result<(), String> {
|
||||||
let tree = btree!(1, btree!(2), btree!(3, btree!(4),));
|
let list = vec![1, 2, 3, 4];
|
||||||
let mut linked_tree = FileLinked::new(tree, String::from("test.txt"))?;
|
let mut file_linked_list = FileLinked::new(list, String::from("test.txt"))?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{}", linked_tree.readonly()),
|
format!("{:?}", file_linked_list.readonly()),
|
||||||
"{\"val\":1,\"left\":{\"val\":2,\"left\":null,\"right\":null},\"right\":{\"val\":3,\"left\":{\"val\":4,\"left\":null,\"right\":null},\"right\":null}}"
|
"[1, 2, 3, 4]"
|
||||||
);
|
);
|
||||||
|
|
||||||
linked_tree.mutate(|v1| v1.val = 10)?;
|
file_linked_list.mutate(|v1| v1.push(5))?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{}", linked_tree.readonly()),
|
format!("{:?}", file_linked_list.readonly()),
|
||||||
"{\"val\":10,\"left\":{\"val\":2,\"left\":null,\"right\":null},\"right\":{\"val\":3,\"left\":{\"val\":4,\"left\":null,\"right\":null},\"right\":null}}"
|
"[1, 2, 3, 4, 5]"
|
||||||
);
|
);
|
||||||
|
|
||||||
linked_tree.mutate(|v1| {
|
file_linked_list.mutate(|v1| {
|
||||||
let mut left = v1.left.clone().unwrap();
|
v1[1] = 1
|
||||||
left.val = 13;
|
|
||||||
v1.left = Some(left);
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
format!("{}", linked_tree.readonly()),
|
format!("{:?}", file_linked_list.readonly()),
|
||||||
"{\"val\":10,\"left\":{\"val\":13,\"left\":null,\"right\":null},\"right\":{\"val\":3,\"left\":{\"val\":4,\"left\":null,\"right\":null},\"right\":null}}"
|
"[1, 1, 3, 4, 5]"
|
||||||
);
|
);
|
||||||
|
|
||||||
fs::remove_file("test.txt").expect("Unable to remove file");
|
fs::remove_file("test.txt").expect("Unable to remove file");
|
|
@ -11,3 +11,4 @@ uuid = { version = "0.7", features = ["serde", "v4"] }
|
||||||
clap = { version = "~2.27.0", features = ["yaml"] }
|
clap = { version = "~2.27.0", features = ["yaml"] }
|
||||||
toml = "0.5.8"
|
toml = "0.5.8"
|
||||||
regex = "1"
|
regex = "1"
|
||||||
|
file_linked = { path = "../file_linked" }
|
|
@ -3,9 +3,9 @@
|
||||||
|
|
||||||
pub mod genetic_node;
|
pub mod genetic_node;
|
||||||
|
|
||||||
use super::file_linked::FileLinked;
|
|
||||||
use super::tree;
|
use super::tree;
|
||||||
|
|
||||||
|
use file_linked::FileLinked;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
extern crate regex;
|
extern crate regex;
|
||||||
|
extern crate file_linked;
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod tree;
|
pub mod tree;
|
||||||
pub mod bracket;
|
pub mod bracket;
|
||||||
pub mod constants;
|
pub mod constants;
|
||||||
pub mod file_linked;
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue