Skip to content

Commit

Permalink
some more unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
mightyiam committed Nov 1, 2024
1 parent a66f988 commit ecd6960
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 41 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions crates/builder/src/file_specs.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use anyhow::Result;
use ssg_child::FileSpec;

use crate::{components, fonts, graphic_file_specs, pages};

pub(crate) fn get() -> Result<impl Iterator<Item = FileSpec>> {
pub(crate) fn get() -> impl Iterator<Item = FileSpec> {
let fonts = fonts::all();
let pages = pages::all()?;
let pages = pages::all();

let calendar_library = FileSpec::new(
"/fullcalendar.js",
Expand All @@ -26,9 +25,9 @@ pub(crate) fn get() -> Result<impl Iterator<Item = FileSpec>> {
),
);

Ok([calendar_library, rrule_library, fullcalendar_rrule]
[calendar_library, rrule_library, fullcalendar_rrule]
.into_iter()
.chain(fonts)
.chain(graphic_file_specs::get())
.chain(pages))
.chain(pages)
}
9 changes: 3 additions & 6 deletions crates/builder/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ mod style;
mod syn_helpers;
mod url;

use anyhow::Result;
use camino::Utf8PathBuf;
use clap::Parser;
use ssg_child::generate_static_site;
Expand All @@ -29,17 +28,15 @@ struct Cli {
}

#[tokio::main]
async fn main() -> Result<()> {
async fn main() {
let Cli { output_dir } = Cli::parse();

let file_specs = file_specs::get()?;
let file_specs = file_specs::get();
let mut generation_task = generate_static_site(output_dir.clone(), file_specs);

generation_task.set_file_result_fn(|progress_report| {
eprintln!("{progress_report:?}");
});

generation_task.await?;

Ok(())
generation_task.await.unwrap();
}
22 changes: 12 additions & 10 deletions crates/builder/src/mob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,24 @@ impl TryFrom<(String, MobFile)> for Mob {
}
}

fn read_mob(dir_entry: Result<std::fs::DirEntry, io::Error>) -> anyhow::Result<Mob> {
let data_file_path = dir_entry?.path();
fn read_mob(dir_entry: Result<std::fs::DirEntry, io::Error>) -> Mob {
let data_file_path = dir_entry.unwrap().path();
let id = data_file_path
.file_stem()
.context("no filename extension")?
.context("no filename extension")
.unwrap()
.to_str()
.context("invalid utf8")?
.context("invalid utf8")
.unwrap()
.into();

let data = std::fs::read_to_string(data_file_path.clone())?;
let data = std::fs::read_to_string(data_file_path.clone()).unwrap();

let yaml_mob: MobFile =
serde_yaml::from_str(&data).map_err(|e| anyhow!("{:?} {:?}", data_file_path, e))?;
let yaml_mob: MobFile = serde_yaml::from_str(&data)
.map_err(|e| anyhow!("{:?} {:?}", data_file_path, e))
.unwrap();

(id, yaml_mob).try_into()
(id, yaml_mob).try_into().unwrap()
}

type EventContentTemplate =
Expand Down Expand Up @@ -189,8 +192,7 @@ pub(crate) static MOBS: Lazy<Vec<Mob>> = Lazy::new(|| {
std::fs::read_dir(MOBS_PATH.as_path())
.unwrap()
.map(read_mob)
.collect::<Result<Vec<Mob>>>()
.unwrap()
.collect::<Vec<Mob>>()
});

pub(crate) fn get_all_participants() -> BTreeSet<Person> {
Expand Down
7 changes: 3 additions & 4 deletions crates/builder/src/pages.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
pub(crate) mod add;
mod index;

use anyhow::Result;
use ssg_child::FileSpec;

use crate::mob::{Mob, MOBS};

pub(crate) fn all() -> Result<impl Iterator<Item = FileSpec>> {
Ok([index::page(), add::page()?]
pub(crate) fn all() -> impl Iterator<Item = FileSpec> {
[index::page(), add::page()]
.into_iter()
.chain(MOBS.iter().cloned().map(Mob::page)))
.chain(MOBS.iter().cloned().map(Mob::page))
}
12 changes: 4 additions & 8 deletions crates/builder/src/pages/add.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::ops::Deref;

use anyhow::Result;
use indexmap::IndexMap;
use maud::Render;
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -54,22 +53,19 @@ pub(crate) static INTERNAL_TYPES_DERIVE_INPUTS: Lazy<IndexMap<TypeIdent, DeriveI
.collect()
});

pub fn page() -> Result<FileSpec> {
pub fn page() -> FileSpec {
let current_path = RelativePathBuf::from("/add.html");

let internal_types = INTERNAL_TYPES_DERIVE_INPUTS
.values()
.map(|derive_input| Type::try_from(derive_input.deref().clone()))
.collect::<Result<Vec<Type>, anyhow::Error>>()?;
.map(|derive_input| Type::try_from(derive_input.deref().clone()).unwrap())
.collect::<Vec<Type>>();

let mut expected_files = ExpectedFiles::default();
let base = components::PageBase::new(&mut expected_files, current_path.clone());
let add_page = components::add_page::AddPage::new(internal_types, base);

let bytes = add_page.render().0.into_bytes();

Ok(FileSpec::new(
current_path,
BytesSource::new(bytes, Some(expected_files)),
))
FileSpec::new(current_path, BytesSource::new(bytes, Some(expected_files)))
}
1 change: 0 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[dependencies]
anyhow = {version = "1.0.66", features = ["backtrace"]}
camino = "1.1.4"
chrono = {version = "0.4.19", features = ["serde"]}
clap = {version = "4.1.4", features = ["derive"]}
Expand Down
15 changes: 9 additions & 6 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Default for Mode {
}

#[tokio::main]
async fn main() -> anyhow::Result<()> {
async fn main() {
#[cfg(feature = "tokio_console")]
console_subscriber::init();

Expand All @@ -52,10 +52,13 @@ async fn main() -> anyhow::Result<()> {
let parent = Parent::new(OUTPUT_DIR.clone()).post_build(tailwind::execute);

match cli.mode.unwrap_or_default() {
Mode::Build => parent.build().await?,
Mode::Dev { open } => anyhow::bail!(parent.dev(open).await),
Mode::Build => {
parent.build().await.unwrap();
}
Mode::Dev { open } => {
let error = parent.dev(open).await;
panic!("{error}");
}
Mode::PrintOutputDir => print!("{}", OUTPUT_DIR.as_os_str().to_str().unwrap()),
}

Ok(())
};
}

0 comments on commit ecd6960

Please sign in to comment.