Dev server

main
Ashelyn Dawn 1 year ago
parent ef3bddbee4
commit f1c975e51a

4
.gitignore vendored

@ -1,6 +1,6 @@
test-archive-ashe.tar.gz
temp/
test-archive.tar.gz
out/
dist/
# Added by cargo
/target

2
Cargo.lock generated

@ -8,11 +8,13 @@ version = "0.1.0"
dependencies = [
"chrono",
"clap",
"console_error_panic_hook",
"flate2",
"futures",
"serde",
"serde_json",
"tar",
"wasm-bindgen",
"yew",
]

@ -13,4 +13,6 @@ futures = { version = "0.3.26", features = ["executor"] }
serde = {version ="1.0.152", features = ["derive"]}
serde_json = "1.0.93"
tar = "0.4.38"
yew = { version = "0.20.0", features = ["ssr"] }
yew = { version = "0.20.0", features = ["ssr", "csr"] }
console_error_panic_hook = { version ="0.1" }
wasm-bindgen = {version = "0.2" }

@ -0,0 +1,9 @@
[build]
release = false
dist = "dist"
public_url = "/"
[serve]
address = "0.0.0.0"
port = 3000

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Trunk | Yew | YBC</title>
<base data-trunk-public-url/>
</head>
<body>
<link data-trunk rel="rust" href="Cargo.toml" />
</body>
</html>

@ -1,4 +1,4 @@
use std::fmt;
use std::{convert::Infallible, fmt};
#[derive(Debug)]
pub enum Ærror {
@ -44,3 +44,9 @@ impl From<serde_json::Error> for Ærror {
Ærror::ParseError(error.to_string())
}
}
impl From<Infallible> for Ærror {
fn from(_error: Infallible) -> Self {
panic!("Infallible failed");
}
}

@ -5,11 +5,16 @@ use serde_json;
use std::{fs, fs::File, io::Read, path::Path, str::FromStr};
use tar::Archive;
#[cfg(debug_assertions)]
use crate::render::render_client;
#[cfg(not(debug_assertions))]
use crate::render::render_ssr;
use crate::{
cli::GeneratorOptions,
data::{Outbox, Person},
error::Ærror,
render::render,
};
mod cli;
@ -27,15 +32,37 @@ fn main() {
}
fn generate() -> Result<(), Ærror> {
let args = GeneratorOptions::parse();
let args;
#[cfg(not(debug_assertions))]
{
args = GeneratorOptions::parse();
setup_dirs(&args)?;
}
#[cfg(debug_assertions)]
{
args = GeneratorOptions {
archive_file: String::from_str("")?,
title: String::from_str("")?,
output_dir: String::from_str("")?,
overwrite: true,
};
}
setup_dirs(&args)?;
let (outbox_text, actor_text, archive_time) = read_archive(&args)?;
let (outbox, author) = parse_json(outbox_text, actor_text)?;
let output_str = render(outbox, author, archive_time)?;
#[cfg(debug_assertions)]
{
render_client(outbox, author, archive_time)?;
}
#[cfg(not(debug_assertions))]
{
let output_str = render_ssr(outbox, author, archive_time)?;
println!("{}", output_str);
println!("{}", output_str);
}
Ok(())
}
@ -62,10 +89,24 @@ fn setup_dirs(args: &GeneratorOptions) -> Result<(), crate::error::Ærror> {
fn read_archive(
args: &GeneratorOptions,
) -> Result<(String, String, DateTime<chrono::Utc>), crate::error::Ærror> {
#[cfg(not(debug_assertions))]
let path = &args.archive_file;
let tar_gz = File::open(path)?;
let tar = GzDecoder::new(tar_gz);
let tar;
#[cfg(not(debug_assertions))]
{
let tar_gz = File::open(path)?;
tar = GzDecoder::new(tar_gz);
}
#[cfg(debug_assertions)]
{
let tar_bytes = include_bytes!("../test-archive.tar.gz");
let tar_gz: &[u8] = tar_bytes.as_slice();
tar = GzDecoder::new(tar_gz);
}
let mut archive = Archive::new(tar);
let mut outbox_text = String::new();
@ -90,6 +131,7 @@ fn read_archive(
}
if path_str.starts_with("media_attachments") {
#[cfg(not(debug_assertions))]
file.unpack_in(&args.output_dir)?;
} else {
match path_str.as_str() {
@ -99,10 +141,13 @@ fn read_archive(
"actor.json" => {
file.read_to_string(&mut actor_text)?;
}
"avatar.png" => {
#[cfg(not(debug_assertions))]
file.unpack_in(&args.output_dir)?;
}
"header.png" => {
#[cfg(not(debug_assertions))]
file.unpack_in(&args.output_dir)?;
}
_ => (),

@ -1,6 +1,11 @@
use chrono::DateTime;
use futures::executor;
use yew::{function_component, html, Html, ServerRenderer};
use yew::{function_component, html, Html, Renderer, ServerRenderer};
#[cfg(debug_assertions)]
use console_error_panic_hook::set_once as set_panic_hook;
#[cfg(debug_assertions)]
use wasm_bindgen::prelude::*;
use crate::{
data::{Outbox, Person},
@ -14,7 +19,8 @@ struct Props {
pub archive_time: DateTime<chrono::Utc>,
}
pub fn render(
#[cfg(not(debug_assertions))]
pub fn render_ssr(
outbox: Outbox,
author: Person,
archive_time: DateTime<chrono::Utc>,
@ -24,6 +30,23 @@ pub fn render(
Ok(output_string)
}
#[cfg(debug_assertions)]
pub fn render_client(
outbox: Outbox,
author: Person,
archive_time: DateTime<chrono::Utc>,
) -> Result<(), Ærror> {
set_panic_hook();
Renderer::<Layout>::with_props(Props {
outbox,
author,
archive_time,
})
.render();
Ok(())
}
async fn render_async(
outbox: Outbox,
author: Person,

Loading…
Cancel
Save