Compare commits

..

22 Commits

Author SHA1 Message Date
Savanni D'Gerinel e461cb9908 Import the new level-one parser
This is the parser that does a raw parse of the SGF file, interpreting components but not enforcing node types.
2023-10-30 01:57:00 +00:00
Savanni D'Gerinel 942e91009e Disable sgf::go and provide a shim for a game 2023-10-30 01:57:00 +00:00
Savanni D'Gerinel 48113d6ccb Bump version to 0.2.0 2023-10-26 00:26:52 -04:00
Savanni D'Gerinel d878f4e82c Resolve more linting issues 2023-10-26 00:19:13 -04:00
Savanni D'Gerinel 7949033857 Add the handler to delete a file 2023-10-26 00:14:10 -04:00
Savanni D'Gerinel ce874e1d30 Fix the form to string conversion and set up the Delete form 2023-10-26 00:12:45 -04:00
Savanni D'Gerinel 07b8bb7bfe Style the authentication page for mobile 2023-10-26 00:03:49 -04:00
Savanni D'Gerinel a403c1b1b3 Hugely refactor the HTML 2023-10-26 00:03:39 -04:00
Savanni D'Gerinel 9a014af75a Remove my custom Image struct 2023-10-25 23:24:41 -04:00
Savanni D'Gerinel 448231739b Remove my custom Unordered List 2023-10-25 23:08:02 -04:00
Savanni D'Gerinel b0027032a4 Rename the password field to be compatible with 1Password 2023-10-25 23:05:06 -04:00
Savanni D'Gerinel 41bbfa14f3 Bump file-service tag to 0.1.2 2023-10-25 10:38:11 -04:00
Savanni D'Gerinel 66876e41c0 Clean up broken tests and clippy warnings 2023-10-25 10:35:24 -04:00
Savanni D'Gerinel ee348c29cb Render the name and the uploaded date for each file in the gallery 2023-10-25 10:20:14 -04:00
Savanni D'Gerinel e96b8087e2 Add filenames to FileInfo and then set those filenames when creating the file 2023-10-25 10:17:17 -04:00
Savanni D'Gerinel 12df1f4b9b Create an UnorderedList HTML container 2023-10-25 09:47:27 -04:00
Savanni D'Gerinel c2e34db79c Map on the data within the node instead of the node itself 2023-10-24 23:05:02 -04:00
Savanni D'Gerinel 0fbfb4f1ad Add a tree map operation 2023-10-20 23:43:47 -04:00
Savanni D'Gerinel c2e78d7c54 Clean up some unnecessary references 2023-10-20 20:28:36 -04:00
Savanni D'Gerinel 2ceccbf38d Remove the Clone constraint from T 2023-10-20 20:17:33 -04:00
Savanni D'Gerinel fbf6a9e76e Move the refcell to inside of the Node 2023-10-20 19:49:31 -04:00
Savanni D'Gerinel 52f814e663 Build a basic tree and experiment with traversals 2023-10-20 18:32:43 -04:00
15 changed files with 356 additions and 114 deletions

6
Cargo.lock generated
View File

@ -816,7 +816,7 @@ dependencies = [
[[package]] [[package]]
name = "file-service" name = "file-service"
version = "0.1.1" version = "0.2.0"
dependencies = [ dependencies = [
"base64ct", "base64ct",
"build_html", "build_html",
@ -4060,6 +4060,10 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079" checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
[[package]]
name = "tree"
version = "0.1.0"
[[package]] [[package]]
name = "try-lock" name = "try-lock"
version = "0.2.4" version = "0.2.4"

View File

@ -20,4 +20,5 @@ members = [
"result-extended", "result-extended",
"screenplay", "screenplay",
"sgf", "sgf",
"tree",
] ]

View File

@ -23,6 +23,7 @@ RUST_ALL_TARGETS=(
"result-extended" "result-extended"
"screenplay" "screenplay"
"sgf" "sgf"
"tree"
) )
build_rust_targets() { build_rust_targets() {

View File

@ -1,6 +1,6 @@
[package] [package]
name = "file-service" name = "file-service"
version = "0.1.1" version = "0.2.0"
authors = ["savanni@luminescent-dreams.com"] authors = ["savanni@luminescent-dreams.com"]
edition = "2018" edition = "2018"

View File

@ -87,7 +87,7 @@ pub async fn handle_auth(
app: App, app: App,
form: HashMap<String, String>, form: HashMap<String, String>,
) -> Result<http::Response<String>, Error> { ) -> Result<http::Response<String>, Error> {
match form.get("token") { match form.get("password") {
Some(token) => match app.authenticate(AuthToken::from(token.clone())).await { Some(token) => match app.authenticate(AuthToken::from(token.clone())).await {
Ok(Some(session_token)) => Response::builder() Ok(Some(session_token)) => Response::builder()
.header("location", "/") .header("location", "/")
@ -134,6 +134,25 @@ pub async fn handle_upload(
} }
} }
pub async fn handle_delete(
app: App,
token: SessionToken,
id: FileId,
) -> Result<http::Response<String>, Error> {
match app.validate_session(token).await {
Ok(Some(_)) => match app.delete_file(id).await {
Ok(_) => Response::builder()
.header("location", "/")
.status(StatusCode::SEE_OTHER)
.body("".to_owned()),
Err(_) => unimplemented!(),
},
_ => Response::builder()
.status(StatusCode::UNAUTHORIZED)
.body("".to_owned()),
}
}
fn serve_file<F>( fn serve_file<F>(
info: FileInfo, info: FileInfo,
file: F, file: F,

View File

@ -74,7 +74,7 @@ impl Html for Form {
None => "".to_owned(), None => "".to_owned(),
}; };
format!( format!(
"<form action=\"{path}\" method=\"{method}\" {encoding}\n{elements}\n</form>\n", "<form action=\"{path}\" method=\"{method}\" {encoding}>\n{elements}\n</form>\n",
path = self.path, path = self.path,
method = self.method, method = self.method,
encoding = encoding, encoding = encoding,
@ -137,11 +137,6 @@ impl Input {
self self
} }
pub fn with_value(mut self, val: &str) -> Self {
self.value = Some(val.to_owned());
self
}
pub fn with_attributes<'a>( pub fn with_attributes<'a>(
mut self, mut self,
values: impl IntoIterator<Item = (&'a str, &'a str)>, values: impl IntoIterator<Item = (&'a str, &'a str)>,
@ -156,31 +151,6 @@ impl Input {
} }
} }
#[derive(Clone, Debug)]
pub struct Label {
target: String,
text: String,
}
impl Label {
pub fn new(target: &str, text: &str) -> Self {
Self {
target: target.to_owned(),
text: text.to_owned(),
}
}
}
impl Html for Label {
fn to_html_string(&self) -> String {
format!(
"<label for=\"{target}\">{text}</label>",
target = self.target,
text = self.text
)
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Button { pub struct Button {
ty: Option<String>, ty: Option<String>,
@ -236,41 +206,3 @@ impl Html for Button {
) )
} }
} }
#[derive(Clone, Debug)]
pub struct Image {
path: String,
attributes: Attributes,
}
impl Image {
pub fn new(path: &str) -> Self {
Self {
path: path.to_owned(),
attributes: Attributes::default(),
}
}
pub fn with_attributes<'a>(
mut self,
values: impl IntoIterator<Item = (&'a str, &'a str)>,
) -> Self {
self.attributes = Attributes(
values
.into_iter()
.map(|(a, b)| (a.to_owned(), b.to_owned()))
.collect::<Vec<(String, String)>>(),
);
self
}
}
impl Html for Image {
fn to_html_string(&self) -> String {
format!(
"<img src={path} {attrs} />",
path = self.path,
attrs = self.attributes.to_string()
)
}
}

View File

@ -1,6 +1,6 @@
mod store; mod store;
pub use store::{ pub use store::{
AuthDB, AuthError, AuthToken, FileHandle, FileId, FileInfo, ReadFileError, SessionToken, Store, AuthDB, AuthError, AuthToken, DeleteFileError, FileHandle, FileId, FileInfo, ReadFileError,
Username, WriteFileError, SessionToken, Store, Username, WriteFileError,
}; };

View File

@ -1,7 +1,7 @@
extern crate log; extern crate log;
use cookie::Cookie; use cookie::Cookie;
use handlers::{file, handle_auth, handle_css, handle_upload, thumbnail}; use handlers::{file, handle_auth, handle_css, handle_delete, handle_upload, thumbnail};
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
convert::Infallible, convert::Infallible,
@ -19,8 +19,8 @@ mod pages;
const MAX_UPLOAD: u64 = 15 * 1024 * 1024; const MAX_UPLOAD: u64 = 15 * 1024 * 1024;
pub use file_service::{ pub use file_service::{
AuthDB, AuthError, AuthToken, FileHandle, FileId, FileInfo, ReadFileError, SessionToken, Store, AuthDB, AuthError, AuthToken, DeleteFileError, FileHandle, FileId, FileInfo, ReadFileError,
Username, WriteFileError, SessionToken, Store, Username, WriteFileError,
}; };
pub use handlers::handle_index; pub use handlers::handle_index;
@ -64,6 +64,11 @@ impl App {
) -> Result<FileHandle, WriteFileError> { ) -> Result<FileHandle, WriteFileError> {
self.store.write().await.add_file(filename, content) self.store.write().await.add_file(filename, content)
} }
pub async fn delete_file(&self, id: FileId) -> Result<(), DeleteFileError> {
self.store.write().await.delete_file(&id)?;
Ok(())
}
} }
fn with_app(app: App) -> impl Filter<Extract = (App,), Error = Infallible> + Clone { fn with_app(app: App) -> impl Filter<Extract = (App,), Error = Infallible> + Clone {
@ -134,6 +139,12 @@ pub async fn main() {
.and(warp::multipart::form().max_length(MAX_UPLOAD)) .and(warp::multipart::form().max_length(MAX_UPLOAD))
.then(handle_upload); .then(handle_upload);
let delete_via_form = warp::path!("delete" / String)
.and(warp::post())
.and(with_app(app.clone()))
.and(with_session())
.then(|id, app, token| handle_delete(app, token, FileId::from(id)));
let thumbnail = warp::path!(String / "tn") let thumbnail = warp::path!(String / "tn")
.and(warp::get()) .and(warp::get())
.and(warp::header::optional::<String>("if-none-match")) .and(warp::header::optional::<String>("if-none-match"))
@ -150,6 +161,7 @@ pub async fn main() {
root.or(styles) root.or(styles)
.or(auth) .or(auth)
.or(upload_via_form) .or(upload_via_form)
.or(delete_via_form)
.or(thumbnail) .or(thumbnail)
.or(file) .or(file)
.with(log), .with(log),

View File

@ -1,16 +1,20 @@
use crate::html::*; use crate::html::*;
use build_html::{self, Container, ContainerType, Html, HtmlContainer}; use build_html::{self, Container, ContainerType, Html, HtmlContainer};
use file_service::{FileHandle, FileId, ReadFileError}; use file_service::{FileHandle, FileInfo, ReadFileError};
pub fn auth(_message: Option<String>) -> build_html::HtmlPage { pub fn auth(_message: Option<String>) -> build_html::HtmlPage {
build_html::HtmlPage::new() build_html::HtmlPage::new()
.with_title("Authentication") .with_title("Sign In")
.with_stylesheet("/css") .with_stylesheet("/css")
.with_container( .with_container(
Container::new(ContainerType::Div) Container::new(ContainerType::Div)
.with_attributes([("class", "authentication-page")]) .with_attributes([("class", "authentication-page")])
.with_container( .with_container(auth_form()),
Container::new(ContainerType::Div) )
}
fn auth_form() -> Container {
Container::default()
.with_attributes([("class", "card authentication-form")]) .with_attributes([("class", "card authentication-form")])
.with_html( .with_html(
Form::new() Form::new()
@ -18,18 +22,17 @@ pub fn auth(_message: Option<String>) -> build_html::HtmlPage {
.with_method("post") .with_method("post")
.with_container( .with_container(
Container::new(ContainerType::Div) Container::new(ContainerType::Div)
.with_attributes([("class", "authentication-form__label")])
.with_html(Label::new("for-token-input", "Authentication")),
)
.with_container(
Container::new(ContainerType::Div)
.with_attributes([("class", "authentication-form__input")])
.with_html( .with_html(
Input::new("token", "token") Input::new("password", "password")
.with_id("for-token-input") .with_id("for-token-input")
.with_attributes([("size", "50")]), .with_attributes([
), ("size", "50"),
), ("class", "authentication-form__input"),
]),
)
.with_html(
Button::new("Sign In")
.with_attributes([("class", "authentication-form__button")]),
), ),
), ),
) )
@ -49,14 +52,7 @@ pub fn gallery(handles: Vec<Result<FileHandle, ReadFileError>>) -> build_html::H
let mut gallery = Container::new(ContainerType::Div).with_attributes([("class", "gallery")]); let mut gallery = Container::new(ContainerType::Div).with_attributes([("class", "gallery")]);
for handle in handles { for handle in handles {
let container = match handle { let container = match handle {
Ok(ref handle) => thumbnail(&handle.id).with_html( Ok(ref handle) => thumbnail(&handle.info),
Form::new()
.with_path(&format!("/{}", *handle.id))
.with_method("post")
.with_html(Input::new("hidden", "_method").with_value("delete"))
.with_html(Button::new("Delete")),
),
Err(err) => Container::new(ContainerType::Div) Err(err) => Container::new(ContainerType::Div)
.with_attributes(vec![("class", "file")]) .with_attributes(vec![("class", "file")])
.with_paragraph(format!("{:?}", err)), .with_paragraph(format!("{:?}", err)),
@ -88,15 +84,31 @@ pub fn upload_form() -> Form {
) )
} }
pub fn thumbnail(id: &FileId) -> Container { pub fn thumbnail(info: &FileInfo) -> Container {
Container::new(ContainerType::Div) Container::new(ContainerType::Div)
.with_attributes(vec![("class", "card thumbnail")]) .with_attributes(vec![("class", "card thumbnail")])
.with_html( .with_html(
Container::new(ContainerType::Div).with_link( Container::new(ContainerType::Div).with_link(
format!("/{}", **id), format!("/{}", *info.id),
Image::new(&format!("{}/tn", **id)) Container::default()
.with_attributes([("class", "thumbnail__image")]) .with_attributes([("class", "thumbnail")])
.with_image(format!("{}/tn", *info.id), "test data")
.to_html_string(), .to_html_string(),
), ),
) )
.with_html(
Container::new(ContainerType::Div)
.with_html(
Container::new(ContainerType::UnorderedList)
.with_attributes(vec![("class", "thumbnail__metadata")])
.with_html(info.name.clone())
.with_html(format!("{}", info.created.format("%Y-%m-%d"))),
)
.with_html(
Form::new()
.with_path(&format!("/delete/{}", *info.id))
.with_method("post")
.with_html(Button::new("Delete")),
),
)
} }

View File

@ -120,8 +120,13 @@ impl FileHandle {
/// Create a new entry in the database /// Create a new entry in the database
pub fn new(filename: String, root: PathBuf) -> Result<Self, WriteFileError> { pub fn new(filename: String, root: PathBuf) -> Result<Self, WriteFileError> {
let id = FileId::from(Uuid::new_v4().hyphenated().to_string()); let id = FileId::from(Uuid::new_v4().hyphenated().to_string());
let path = PathBuf::from(filename);
let extension = PathBuf::from(filename) let name = path
.file_stem()
.and_then(|s| s.to_str().map(|s| s.to_owned()))
.ok_or(WriteFileError::InvalidPath)?;
let extension = path
.extension() .extension()
.and_then(|s| s.to_str().map(|s| s.to_owned())) .and_then(|s| s.to_str().map(|s| s.to_owned()))
.ok_or(WriteFileError::InvalidPath)?; .ok_or(WriteFileError::InvalidPath)?;
@ -138,6 +143,7 @@ impl FileHandle {
let info = FileInfo { let info = FileInfo {
id: id.clone(), id: id.clone(),
name,
size: 0, size: 0,
created: Utc::now(), created: Utc::now(),
file_type, file_type,
@ -233,6 +239,17 @@ mod test {
); );
} }
#[test]
fn it_creates_file_info() {
let tmp = TempDir::new("var").unwrap();
let handle =
FileHandle::new("rawr.png".to_owned(), PathBuf::from(tmp.path())).expect("to succeed");
assert_eq!(handle.info.name, "rawr");
assert_eq!(handle.info.size, 0);
assert_eq!(handle.info.file_type, "image/png");
assert_eq!(handle.info.extension, "png");
}
#[test] #[test]
fn it_opens_a_file() { fn it_opens_a_file() {
let tmp = TempDir::new("var").unwrap(); let tmp = TempDir::new("var").unwrap();

View File

@ -11,6 +11,12 @@ use std::{
#[derive(Clone, Debug, Serialize, Deserialize)] #[derive(Clone, Debug, Serialize, Deserialize)]
pub struct FileInfo { pub struct FileInfo {
pub id: FileId, pub id: FileId,
// Early versions of the application didn't support a name field, so it is possible that
// metadata won't contain the name. We can just default to an empty string when loading the
// metadata, as all future versions will require a filename when the file gets uploaded.
#[serde(default)]
pub name: String,
pub size: usize, pub size: usize,
pub created: DateTime<Utc>, pub created: DateTime<Utc>,
pub file_type: String, pub file_type: String,
@ -50,6 +56,7 @@ mod test {
let info = FileInfo { let info = FileInfo {
id: FileId("temp-id".to_owned()), id: FileId("temp-id".to_owned()),
name: "test-image".to_owned(),
size: 23777, size: 23777,
created, created,
file_type: "image/png".to_owned(), file_type: "image/png".to_owned(),

View File

@ -53,9 +53,6 @@ pub enum ReadFileError {
#[error("permission denied")] #[error("permission denied")]
PermissionDenied, PermissionDenied,
#[error("invalid path")]
InvalidPath,
#[error("JSON error")] #[error("JSON error")]
JSONError(#[from] serde_json::error::Error), JSONError(#[from] serde_json::error::Error),
@ -63,6 +60,36 @@ pub enum ReadFileError {
IOError(#[from] std::io::Error), IOError(#[from] std::io::Error),
} }
#[derive(Debug, Error)]
pub enum DeleteFileError {
#[error("file not found")]
FileNotFound(PathBuf),
#[error("metadata path is not a file")]
NotAFile,
#[error("cannot read metadata")]
PermissionDenied,
#[error("invalid metadata path")]
MetadataParseError(serde_json::error::Error),
#[error("IO error")]
IOError(#[from] std::io::Error),
}
impl From<ReadFileError> for DeleteFileError {
fn from(err: ReadFileError) -> Self {
match err {
ReadFileError::FileNotFound(path) => DeleteFileError::FileNotFound(path),
ReadFileError::NotAFile => DeleteFileError::NotAFile,
ReadFileError::PermissionDenied => DeleteFileError::PermissionDenied,
ReadFileError::JSONError(err) => DeleteFileError::MetadataParseError(err),
ReadFileError::IOError(err) => DeleteFileError::IOError(err),
}
}
}
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum AuthError { pub enum AuthError {
#[error("authentication token is duplicated")] #[error("authentication token is duplicated")]
@ -369,7 +396,7 @@ impl Store {
FileHandle::load(id, &self.files_root) FileHandle::load(id, &self.files_root)
} }
pub fn delete_file(&mut self, id: &FileId) -> Result<(), WriteFileError> { pub fn delete_file(&mut self, id: &FileId) -> Result<(), DeleteFileError> {
let handle = FileHandle::load(id, &self.files_root)?; let handle = FileHandle::load(id, &self.files_root)?;
handle.delete(); handle.delete();
Ok(()) Ok(())

View File

@ -29,6 +29,7 @@ body {
justify-content: center; justify-content: center;
align-items: center; align-items: center;
height: 200px; height: 200px;
margin: 8px;
} }
.authentication-form { .authentication-form {
@ -77,6 +78,10 @@ body {
border: none; border: none;
} }
.thumbnail__metadata {
list-style: none;
}
/* /*
[type="submit"] { [type="submit"] {
border-radius: 1em; border-radius: 1em;
@ -129,6 +134,16 @@ body {
.authentication-form { .authentication-form {
width: 100%; width: 100%;
display: flex;
flex-direction: column;
}
.authentication-form__input {
font-size: x-large;
}
.authentication-form__button {
font-size: x-large;
} }
.upload-form__selector { .upload-form__selector {

8
tree/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "tree"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

187
tree/src/lib.rs Normal file
View File

@ -0,0 +1,187 @@
//! This data structure is a generic tree which can contain any data. That data itself need to keep
//! track of its own tree structure.
//!
//! This surely already exists. I am created it to test my own ability to do things in Rust.
use std::{
cell::{Ref, RefCell},
collections::VecDeque,
rc::Rc,
};
#[derive(Clone, Debug, Default)]
pub enum Tree<T> {
#[default]
Empty,
Root(Node<T>),
}
impl<T> Tree<T> {
pub fn new(value: T) -> (Tree<T>, Node<T>) {
let node = Node::new(value);
let tree = Tree::Root(node.clone());
(tree, node)
}
pub fn set_value(&mut self, value: T) -> Node<T> {
let node = Node::new(value);
*self = Tree::Root(node.clone());
node
}
/// Use a breadth-first-search pattern to find a node, returning the node if found.
pub fn find_bfs<F>(&self, op: F) -> Option<Node<T>>
where
F: FnOnce(&T) -> bool + Copy,
{
let mut queue: VecDeque<Node<T>> = match self {
Tree::Empty => VecDeque::new(),
Tree::Root(node) => {
let mut queue = VecDeque::new();
queue.push_back(node.clone());
queue
}
};
while let Some(node) = queue.pop_front() {
if op(&node.value()) {
return Some(node.clone());
}
for child in node.children().iter() {
queue.push_back(child.clone())
}
}
None
}
/// Convert each node of a tree from type T to type U
pub fn map<F, U>(&self, op: F) -> Tree<U>
where
F: FnOnce(&T) -> U + Copy,
{
// A key part of this is to avoid recursion. There is no telling how deep a tree may go (Go
// game records can go hundreds of nodes deep), so we're going to just avoid recursion.
match self {
Tree::Empty => Tree::Empty,
Tree::Root(root) => {
let new_root = Node::new(op(&root.value()));
// This queue serves as a work list. Each node in the queue needs to be converted,
// and I've paired the node up with the one that it's supposed to be attached to.
// So, as we look at a node A, we make sure that all of its children gets added to
// the queue, and that the queue knows that the conversion of each child node
// should get attached to A.
let mut queue: VecDeque<(Node<T>, Node<U>)> = root
.children()
.iter()
.map(|child| (child.clone(), new_root.clone()))
.collect();
while let Some((source, dest)) = queue.pop_front() {
let res = Node::new(op(&source.value()));
dest.add_child_node(res.clone());
for child in source.children().iter() {
queue.push_back((child.clone(), res.clone()));
}
}
Tree::Root(new_root)
}
}
}
}
// By using the Rc<RefCell> container here, I'm able to make Node easily clonable while still
// having the contents be shared. This means that I can change the tree structure without having to
// make the visible objects mutable.
//
// This feels like cheating the type system.
//
// However, since I've moved the RefCell inside of the node, I can borrow the node multiple times
// in a traversal function and I can make changes to nodes that I find.
#[derive(Debug)]
pub struct Node<T>(Rc<RefCell<Node_<T>>>);
impl<T> Clone for Node<T> {
fn clone(&self) -> Self {
Node(Rc::clone(&self.0))
}
}
#[derive(Debug)]
struct Node_<T> {
value: T,
children: Vec<Node<T>>,
}
impl<T> Node<T> {
pub fn new(value: T) -> Self {
Self(Rc::new(RefCell::new(Node_ {
value,
children: vec![],
})))
}
/// Immutably retrieve the data in this node.
pub fn value(&self) -> Ref<T> {
// Ref::map is not actually a member function. I don't know why this was done, other than
// maybe to avoid conflicting with other `map` declarations. Why that is necessary when
// Option::map exists as a member, I don't know.
Ref::map(self.0.borrow(), |v| &v.value)
}
pub fn children(&self) -> Ref<Vec<Node<T>>> {
Ref::map(self.0.borrow(), |v| &v.children)
}
pub fn add_child_node(&self, child: Node<T>) {
self.0.borrow_mut().children.push(child)
}
pub fn add_child_value(&self, value: T) -> Node<T> {
let node = Node::new(value);
self.0.borrow_mut().children.push(node.clone());
node
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_find_node_in_tree() {
let mut tree = Tree::default();
tree.set_value(15);
assert!(tree.find_bfs(|val| *val == 15).is_some());
assert!(tree.find_bfs(|val| *val == 16).is_none());
let node = tree.find_bfs(|val| *val == 15).unwrap();
node.add_child_value(20);
assert!(tree.find_bfs(|val| *val == 20).is_some());
}
#[test]
fn node_can_add_children() {
let n = Node::new(15);
n.add_child_value(20);
assert_eq!(*n.value(), 15);
// assert_eq!(n.children(), vec![Rc::new(RefCell::new(Node::new(20)))]);
}
#[test]
fn it_can_map_one_tree_to_another() {
let (tree, n) = Tree::new(15);
let n = n.add_child_value(16);
let _ = n.add_child_value(17);
let tree2 = tree.map(|v| v.to_string());
assert!(tree2.find_bfs(|val| *val == "15").is_some());
assert!(tree2.find_bfs(|val| *val == "16").is_some());
assert!(tree2.find_bfs(|val| *val == "17").is_some());
}
}