Compare commits
2 Commits
e461cb9908
...
8c3ce0c911
Author | SHA1 | Date |
---|---|---|
Savanni D'Gerinel | 8c3ce0c911 | |
Savanni D'Gerinel | 2443a434c5 |
|
@ -816,7 +816,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "file-service"
|
||||
version = "0.2.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"base64ct",
|
||||
"build_html",
|
||||
|
@ -4060,10 +4060,6 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "efd1f82c56340fdf16f2a953d7bda4f8fdffba13d93b00844c25572110b26079"
|
||||
|
||||
[[package]]
|
||||
name = "tree"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "try-lock"
|
||||
version = "0.2.4"
|
||||
|
|
|
@ -20,5 +20,4 @@ members = [
|
|||
"result-extended",
|
||||
"screenplay",
|
||||
"sgf",
|
||||
"tree",
|
||||
]
|
||||
|
|
1
build.sh
1
build.sh
|
@ -23,7 +23,6 @@ RUST_ALL_TARGETS=(
|
|||
"result-extended"
|
||||
"screenplay"
|
||||
"sgf"
|
||||
"tree"
|
||||
)
|
||||
|
||||
build_rust_targets() {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "file-service"
|
||||
version = "0.2.0"
|
||||
version = "0.1.1"
|
||||
authors = ["savanni@luminescent-dreams.com"]
|
||||
edition = "2018"
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ pub async fn handle_auth(
|
|||
app: App,
|
||||
form: HashMap<String, String>,
|
||||
) -> Result<http::Response<String>, Error> {
|
||||
match form.get("password") {
|
||||
match form.get("token") {
|
||||
Some(token) => match app.authenticate(AuthToken::from(token.clone())).await {
|
||||
Ok(Some(session_token)) => Response::builder()
|
||||
.header("location", "/")
|
||||
|
@ -134,25 +134,6 @@ 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>(
|
||||
info: FileInfo,
|
||||
file: F,
|
||||
|
|
|
@ -74,7 +74,7 @@ impl Html for Form {
|
|||
None => "".to_owned(),
|
||||
};
|
||||
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,
|
||||
method = self.method,
|
||||
encoding = encoding,
|
||||
|
@ -137,6 +137,11 @@ impl Input {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn with_value(mut self, val: &str) -> Self {
|
||||
self.value = Some(val.to_owned());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_attributes<'a>(
|
||||
mut self,
|
||||
values: impl IntoIterator<Item = (&'a str, &'a str)>,
|
||||
|
@ -151,6 +156,31 @@ 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)]
|
||||
pub struct Button {
|
||||
ty: Option<String>,
|
||||
|
@ -206,3 +236,41 @@ 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()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
mod store;
|
||||
|
||||
pub use store::{
|
||||
AuthDB, AuthError, AuthToken, DeleteFileError, FileHandle, FileId, FileInfo, ReadFileError,
|
||||
SessionToken, Store, Username, WriteFileError,
|
||||
AuthDB, AuthError, AuthToken, FileHandle, FileId, FileInfo, ReadFileError, SessionToken, Store,
|
||||
Username, WriteFileError,
|
||||
};
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
extern crate log;
|
||||
|
||||
use cookie::Cookie;
|
||||
use handlers::{file, handle_auth, handle_css, handle_delete, handle_upload, thumbnail};
|
||||
use handlers::{file, handle_auth, handle_css, handle_upload, thumbnail};
|
||||
use std::{
|
||||
collections::{HashMap, HashSet},
|
||||
convert::Infallible,
|
||||
|
@ -19,8 +19,8 @@ mod pages;
|
|||
const MAX_UPLOAD: u64 = 15 * 1024 * 1024;
|
||||
|
||||
pub use file_service::{
|
||||
AuthDB, AuthError, AuthToken, DeleteFileError, FileHandle, FileId, FileInfo, ReadFileError,
|
||||
SessionToken, Store, Username, WriteFileError,
|
||||
AuthDB, AuthError, AuthToken, FileHandle, FileId, FileInfo, ReadFileError, SessionToken, Store,
|
||||
Username, WriteFileError,
|
||||
};
|
||||
pub use handlers::handle_index;
|
||||
|
||||
|
@ -64,11 +64,6 @@ impl App {
|
|||
) -> Result<FileHandle, WriteFileError> {
|
||||
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 {
|
||||
|
@ -139,12 +134,6 @@ pub async fn main() {
|
|||
.and(warp::multipart::form().max_length(MAX_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")
|
||||
.and(warp::get())
|
||||
.and(warp::header::optional::<String>("if-none-match"))
|
||||
|
@ -161,7 +150,6 @@ pub async fn main() {
|
|||
root.or(styles)
|
||||
.or(auth)
|
||||
.or(upload_via_form)
|
||||
.or(delete_via_form)
|
||||
.or(thumbnail)
|
||||
.or(file)
|
||||
.with(log),
|
||||
|
|
|
@ -1,20 +1,16 @@
|
|||
use crate::html::*;
|
||||
use build_html::{self, Container, ContainerType, Html, HtmlContainer};
|
||||
use file_service::{FileHandle, FileInfo, ReadFileError};
|
||||
use file_service::{FileHandle, FileId, ReadFileError};
|
||||
|
||||
pub fn auth(_message: Option<String>) -> build_html::HtmlPage {
|
||||
build_html::HtmlPage::new()
|
||||
.with_title("Sign In")
|
||||
.with_title("Authentication")
|
||||
.with_stylesheet("/css")
|
||||
.with_container(
|
||||
Container::new(ContainerType::Div)
|
||||
.with_attributes([("class", "authentication-page")])
|
||||
.with_container(auth_form()),
|
||||
)
|
||||
}
|
||||
|
||||
fn auth_form() -> Container {
|
||||
Container::default()
|
||||
.with_container(
|
||||
Container::new(ContainerType::Div)
|
||||
.with_attributes([("class", "card authentication-form")])
|
||||
.with_html(
|
||||
Form::new()
|
||||
|
@ -22,17 +18,18 @@ fn auth_form() -> Container {
|
|||
.with_method("post")
|
||||
.with_container(
|
||||
Container::new(ContainerType::Div)
|
||||
.with_html(
|
||||
Input::new("password", "password")
|
||||
.with_id("for-token-input")
|
||||
.with_attributes([
|
||||
("size", "50"),
|
||||
("class", "authentication-form__input"),
|
||||
]),
|
||||
.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(
|
||||
Button::new("Sign In")
|
||||
.with_attributes([("class", "authentication-form__button")]),
|
||||
Input::new("token", "token")
|
||||
.with_id("for-token-input")
|
||||
.with_attributes([("size", "50")]),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
@ -52,7 +49,14 @@ pub fn gallery(handles: Vec<Result<FileHandle, ReadFileError>>) -> build_html::H
|
|||
let mut gallery = Container::new(ContainerType::Div).with_attributes([("class", "gallery")]);
|
||||
for handle in handles {
|
||||
let container = match handle {
|
||||
Ok(ref handle) => thumbnail(&handle.info),
|
||||
Ok(ref handle) => thumbnail(&handle.id).with_html(
|
||||
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)
|
||||
.with_attributes(vec![("class", "file")])
|
||||
.with_paragraph(format!("{:?}", err)),
|
||||
|
@ -84,31 +88,15 @@ pub fn upload_form() -> Form {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn thumbnail(info: &FileInfo) -> Container {
|
||||
pub fn thumbnail(id: &FileId) -> Container {
|
||||
Container::new(ContainerType::Div)
|
||||
.with_attributes(vec![("class", "card thumbnail")])
|
||||
.with_html(
|
||||
Container::new(ContainerType::Div).with_link(
|
||||
format!("/{}", *info.id),
|
||||
Container::default()
|
||||
.with_attributes([("class", "thumbnail")])
|
||||
.with_image(format!("{}/tn", *info.id), "test data")
|
||||
format!("/{}", **id),
|
||||
Image::new(&format!("{}/tn", **id))
|
||||
.with_attributes([("class", "thumbnail__image")])
|
||||
.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")),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -120,13 +120,8 @@ impl FileHandle {
|
|||
/// Create a new entry in the database
|
||||
pub fn new(filename: String, root: PathBuf) -> Result<Self, WriteFileError> {
|
||||
let id = FileId::from(Uuid::new_v4().hyphenated().to_string());
|
||||
let path = 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
|
||||
let extension = PathBuf::from(filename)
|
||||
.extension()
|
||||
.and_then(|s| s.to_str().map(|s| s.to_owned()))
|
||||
.ok_or(WriteFileError::InvalidPath)?;
|
||||
|
@ -143,7 +138,6 @@ impl FileHandle {
|
|||
|
||||
let info = FileInfo {
|
||||
id: id.clone(),
|
||||
name,
|
||||
size: 0,
|
||||
created: Utc::now(),
|
||||
file_type,
|
||||
|
@ -239,17 +233,6 @@ 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]
|
||||
fn it_opens_a_file() {
|
||||
let tmp = TempDir::new("var").unwrap();
|
||||
|
|
|
@ -11,12 +11,6 @@ use std::{
|
|||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct FileInfo {
|
||||
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 created: DateTime<Utc>,
|
||||
pub file_type: String,
|
||||
|
@ -56,7 +50,6 @@ mod test {
|
|||
|
||||
let info = FileInfo {
|
||||
id: FileId("temp-id".to_owned()),
|
||||
name: "test-image".to_owned(),
|
||||
size: 23777,
|
||||
created,
|
||||
file_type: "image/png".to_owned(),
|
||||
|
|
|
@ -53,6 +53,9 @@ pub enum ReadFileError {
|
|||
#[error("permission denied")]
|
||||
PermissionDenied,
|
||||
|
||||
#[error("invalid path")]
|
||||
InvalidPath,
|
||||
|
||||
#[error("JSON error")]
|
||||
JSONError(#[from] serde_json::error::Error),
|
||||
|
||||
|
@ -60,36 +63,6 @@ pub enum ReadFileError {
|
|||
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)]
|
||||
pub enum AuthError {
|
||||
#[error("authentication token is duplicated")]
|
||||
|
@ -396,7 +369,7 @@ impl Store {
|
|||
FileHandle::load(id, &self.files_root)
|
||||
}
|
||||
|
||||
pub fn delete_file(&mut self, id: &FileId) -> Result<(), DeleteFileError> {
|
||||
pub fn delete_file(&mut self, id: &FileId) -> Result<(), WriteFileError> {
|
||||
let handle = FileHandle::load(id, &self.files_root)?;
|
||||
handle.delete();
|
||||
Ok(())
|
||||
|
|
|
@ -29,7 +29,6 @@ body {
|
|||
justify-content: center;
|
||||
align-items: center;
|
||||
height: 200px;
|
||||
margin: 8px;
|
||||
}
|
||||
|
||||
.authentication-form {
|
||||
|
@ -78,10 +77,6 @@ body {
|
|||
border: none;
|
||||
}
|
||||
|
||||
.thumbnail__metadata {
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
/*
|
||||
[type="submit"] {
|
||||
border-radius: 1em;
|
||||
|
@ -134,16 +129,6 @@ body {
|
|||
|
||||
.authentication-form {
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.authentication-form__input {
|
||||
font-size: x-large;
|
||||
}
|
||||
|
||||
.authentication-form__button {
|
||||
font-size: x-large;
|
||||
}
|
||||
|
||||
.upload-form__selector {
|
||||
|
|
|
@ -1,8 +0,0 @@
|
|||
[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
187
tree/src/lib.rs
|
@ -1,187 +0,0 @@
|
|||
//! 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());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue