Skip to content

Commit

Permalink
fix stuff and cool build
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-thomas committed Jan 16, 2025
1 parent c78ac31 commit f8383f5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 60 deletions.
26 changes: 1 addition & 25 deletions titan-html-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use lightningcss::{printer::PrinterOptions, properties::Property};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::{parse_macro_input, Field, Fields, Ident, ItemStruct, LitStr};
use syn::{parse_macro_input, Field, Fields, ItemStruct, LitStr};
use titan_utils::validatecss::{
validate_css, validate_globalcss, CSSValidationError,
};
Expand Down Expand Up @@ -31,30 +31,6 @@ fn from_stylerules_to_tokenstream(
#(#styles_tokens),*
}
}
//
//// Helper function to find and extract variables from CSS values
//fn extract_variables(css: &str) -> Vec<(usize, usize, String)> {
// let mut variables = Vec::new();
// let mut start = 0;
// while let Some(var_start) = css[start..].find("{") {
// if let Some(var_end) = css[start + var_start..].find('}') {
// let abs_start = start + var_start;
// let abs_end = start + var_start + var_end + 1;
// let var_name = css[abs_start + 1..abs_end - 1].to_string();
// variables.push((abs_start, abs_end, var_name));
// start = abs_end;
// } else {
// break;
// }
// }
// variables
//}
//
//#[derive(Debug)]
//enum StringBit {
// Var(Ident),
// Text(String),
//}

#[proc_macro]
pub fn global_css(input: TokenStream) -> TokenStream {
Expand Down
20 changes: 6 additions & 14 deletions titan/examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use titan::{
web, Respondable,
};
use titan_html::{css, global_css, StyleRule};
use tokio::net::TcpListener;

const LINK_CSS: &[StyleRule] = css!(
"
Expand All @@ -27,9 +26,7 @@ const TESTING: &[StyleRule] = css!(
"
);

#[ssg]
fn index() -> impl Respondable {
println!("ran");
async fn index() -> impl Respondable {
Html::from((
Head::default().global_style(global_css!("")).reset_css(),
Body::default().children([
Expand All @@ -54,23 +51,18 @@ fn index() -> impl Respondable {
.with_csp("examplenonce")
}

use titan_derive::ssg;

#[ssg]
pub fn testing() -> titan_html::tags::html::Html {
pub async fn testing() -> titan_html::tags::html::Html {
println!("ran");
Html::from((Head::default(), Body::default()))
}

#[tokio::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("0.0.0.0:4000").await.unwrap();
let app = App::default()
.at("/", web::get(index))
.at("/test/testing", web::get(testing));

let app =
App::default().at("/", web::get(index)).at("/test", web::get(testing));
titan::build_static(app, std::path::PathBuf::from("./dist")).await;

titan::build::build_static(app, std::path::PathBuf::from("./dist")).await;
Ok(())

//titan::serve(listener, app).await
}
4 changes: 1 addition & 3 deletions titan/examples/nice_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ struct Queries {
test: Option<i32>,
}

use titan_derive::ssg;

#[titan::ssg]
pub fn testing() -> titan_html::tags::html::Html {
async fn testing() -> titan_html::tags::html::Html {
titan::html::tags::html::Html::from((
titan::html::tags::head::Head::default(),
titan::html::tags::Body::default(),
Expand Down
31 changes: 14 additions & 17 deletions titan/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use titan_http::body::Body;
use titan_http::header;
use titan_http::Request;

use std::fs;
use std::io::Write;
pub async fn build_static(app: crate::App, path: PathBuf) {
let inner_app = app.into_inner();

Expand All @@ -14,13 +16,11 @@ pub async fn build_static(app: crate::App, path: PathBuf) {
panic!("Error: Can't statically build a dynamic path: {}", route.0)
}

println!("Route: {}", route.0);

let request = Request::new(Box::new([]));

let response = match route.1.clone().call(request).await {
Ok(value) => value,
Err(err) => {
Err(_) => {
panic!("Handler error in path: {:?}", route.0)
}
};
Expand All @@ -38,11 +38,9 @@ pub async fn build_static(app: crate::App, path: PathBuf) {
full_body_message.into_boxed_slice()
}
};

if let Some(value) = parts.headers.get(header::CONTENT_TYPE) {
if value
!= header::HeaderValue::from_str(titan_http::mime::HTML.as_str())
.unwrap()
{
if value != header::HeaderValue::from_str("text/html").unwrap() {
panic!("only works on html for now");
}
} else {
Expand All @@ -51,21 +49,20 @@ pub async fn build_static(app: crate::App, path: PathBuf) {

let text_string = String::from_utf8(bodyv2.to_vec()).unwrap();

let mut path = route.0.to_string();
let mut path = path.clone();

path.push_str(".html");
let test = match route.0.to_string().as_str() {
"/" => "index".to_string(),
_ => route.0.to_string().replacen("/", "", 1),
};

let mut this_path = path.clone();
path.push(format!("{}.html", test));

this_path.push_str(&path);
fs::create_dir_all(path.parent().unwrap()).unwrap();

let mut nice = std::fs::OpenOptions::new()
.create(true)
.write(true)
.open(this_path)
.unwrap();
let mut nice =
fs::OpenOptions::new().create(true).write(true).open(path).unwrap();

use std::io::Write;
nice.write_all(text_string.as_bytes()).unwrap();
}
}
4 changes: 3 additions & 1 deletion titan/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod app;
pub mod build;
mod build;
pub use build::build_static;
pub mod guard;
pub mod prelude;
pub mod route;
mod utils;
pub use titan_derive::ssg;

// For titan-derive
pub use utils::lazy_static;
Expand Down

0 comments on commit f8383f5

Please sign in to comment.