diff --git a/src/dom/mod.rs b/src/dom/mod.rs index 64a335c..5e7e3a3 100644 --- a/src/dom/mod.rs +++ b/src/dom/mod.rs @@ -1,3 +1,6 @@ +/// Contains most things related to a virtual DOM. pub mod document; +/// Contains anything related to creating and modifying an Element. pub mod element; +/// The head of a document pub mod head; diff --git a/src/filesystem.rs b/src/filesystem.rs new file mode 100644 index 0000000..60ea306 --- /dev/null +++ b/src/filesystem.rs @@ -0,0 +1,33 @@ +use std::fs::File; +use std::io::Error; +use std::path::{Path, PathBuf}; + +pub trait Fileify { + fn get_file(&self) -> Result; + + fn ensure_file(&self) -> Result; +} + +impl Fileify for Path { + fn get_file(&self) -> Result { + self.to_path_buf().get_file() + } + + fn ensure_file(&self) -> Result { + self.to_path_buf().ensure_file() + } +} + +impl Fileify for PathBuf { + fn get_file(&self) -> Result { + File::open(self) + } + + fn ensure_file(&self) -> Result { + let mut f: std::io::Result = File::open(self); + if let Err(_) = f { + f = File::create(self); + } + f + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index d7710be..6a7102e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,13 @@ pub mod dom; /// All sorts of general use macros pub mod macros; +/// Filesystem things +pub mod filesystem; + #[cfg(test)] mod tests { + use crate::filesystem::Fileify; + #[cfg(feature = "dom")] #[test] fn dom() { @@ -23,4 +28,11 @@ mod tests { let foo = "foo"; assert_eq!(String::from("foo"), into!(String, foo)); } + + #[test] + fn fs() { + if let Ok(_) = std::path::Path::new("./file").ensure_file() { + let _ = std::fs::remove_file("./file"); + } + } } diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 812ae8d..0000000 --- a/src/main.rs +++ /dev/null @@ -1,53 +0,0 @@ -use milly::dom::element::Element; -use milly::into; - -macro_rules! doc { - ($title: expr, $lang: expr) => { - { - use milly::dom::document::Document; - use milly::dom::element::Element; - let mut d = Document::new($lang); - d.head - .set_title($title) - .add_css("https://cdn.jsdelivr.net/npm/@picocss/pico@1/css/pico.min.css") - .add_element( - Element::new("meta") - .set_attribute("name", "viewport") - .set_attribute("content", "width=device-width,initial-scale=1") - .clone() - ); - d.body - .append_child( - Element::new("nav") - .add_class("container-fluid") - .append_child( - Element::new("ul") - .set_id("nav-titles") - .append_child(Element::new("li").append_child( - $title - )) - ) - ) - .append_child( - Element::new("main") - .set_id("main") - .add_class("container") - ); - d - } - } -} - -fn main() { - let mut doc = doc!("Main", "en"); - - doc.body - .get_element_by_id("main") - .unwrap() - .append_child( - Element::new("h1") - .append_child("Hello") - ); - - println!("{}", into!(Element, doc).to_string()); -}