feat: add filesystem stuff

This commit is contained in:
Strix 2023-10-14 22:39:16 +02:00
parent 44075cee1f
commit 5ede522eeb
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
4 changed files with 48 additions and 53 deletions

View file

@ -1,3 +1,6 @@
/// Contains most things related to a virtual DOM.
pub mod document; pub mod document;
/// Contains anything related to creating and modifying an Element.
pub mod element; pub mod element;
/// The head of a document
pub mod head; pub mod head;

33
src/filesystem.rs Normal file
View file

@ -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<File, Error>;
fn ensure_file(&self) -> Result<File, Error>;
}
impl Fileify for Path {
fn get_file(&self) -> Result<File, Error> {
self.to_path_buf().get_file()
}
fn ensure_file(&self) -> Result<File, Error> {
self.to_path_buf().ensure_file()
}
}
impl Fileify for PathBuf {
fn get_file(&self) -> Result<File, Error> {
File::open(self)
}
fn ensure_file(&self) -> Result<File, Error> {
let mut f: std::io::Result<File> = File::open(self);
if let Err(_) = f {
f = File::create(self);
}
f
}
}

View file

@ -5,8 +5,13 @@ pub mod dom;
/// All sorts of general use macros /// All sorts of general use macros
pub mod macros; pub mod macros;
/// Filesystem things
pub mod filesystem;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::filesystem::Fileify;
#[cfg(feature = "dom")] #[cfg(feature = "dom")]
#[test] #[test]
fn dom() { fn dom() {
@ -23,4 +28,11 @@ mod tests {
let foo = "foo"; let foo = "foo";
assert_eq!(String::from("foo"), into!(String, 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");
}
}
} }

View file

@ -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());
}