challenges macro & structs

This commit is contained in:
Ponj 2024-11-15 20:36:53 -05:00
parent 8e47f6570f
commit 09c7dcc9d7
Signed by: p6nj
GPG key ID: 6FED68D87C479A59
5 changed files with 132 additions and 1 deletions

49
Cargo.lock generated
View file

@ -658,6 +658,18 @@ dependencies = [
"wasi",
]
[[package]]
name = "getset2"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0103f4beaf538e75e289af484af18866b472a22f33b1d9697ec1849f3918a200"
dependencies = [
"proc-macro-error2",
"proc-macro2",
"quote",
"syn 2.0.87",
]
[[package]]
name = "gimli"
version = "0.31.1"
@ -1310,6 +1322,16 @@ dependencies = [
"zerocopy",
]
[[package]]
name = "prettyplease"
version = "0.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64d1ec885c64d0457d564db4ec299b2dae3f9c02808b8ad9c3a089c591b18033"
dependencies = [
"proc-macro2",
"syn 2.0.87",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -1334,6 +1356,28 @@ dependencies = [
"version_check",
]
[[package]]
name = "proc-macro-error-attr2"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "proc-macro-error2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802"
dependencies = [
"proc-macro-error-attr2",
"proc-macro2",
"quote",
"syn 2.0.87",
]
[[package]]
name = "proc-macro2"
version = "1.0.89"
@ -2601,10 +2645,15 @@ name = "typonomy"
version = "0.1.0"
dependencies = [
"amplify",
"getset2",
"markdown",
"prettyplease",
"proc-macro2",
"quote",
"rocket",
"shuttle-rocket",
"shuttle-runtime",
"syn 2.0.87",
"tokio",
]

View file

@ -4,9 +4,14 @@ version = "0.1.0"
edition = "2021"
[dependencies]
amplify = "4.8.0"
amplify = "4.8"
getset2 = "0.2"
md = {version = "1.0.0-alpha.21", package = "markdown"}
prettyplease = "0"
proc-macro2 = { version = "1", default-features = false }
quote = {version = "1", default-features = false}
rocket = {version="0", default-features = false}
shuttle-rocket = "*"
shuttle-runtime = "*"
syn = "2"
tokio = "1"

41
src/challenge.rs Normal file
View file

@ -0,0 +1,41 @@
use std::fmt::Debug;
use getset2::Getters;
use quote::ToTokens;
use syn::Expr;
pub(super) mod lists;
#[derive(Getters)]
pub(super) struct Challenge {
#[getset(get = "pub(super)")]
code: Expr,
#[getset(get = "pub(super)")]
type_name: &'static str,
}
impl Challenge {
pub(super) fn code_str(&self) -> syn::Result<String> {
Self::format_code(self.code.to_token_stream().to_string())
}
pub(super) fn into_code_str(self) -> syn::Result<String> {
Self::format_code(self.code.into_token_stream().to_string())
}
fn format_code(code: impl AsRef<str>) -> syn::Result<String> {
Ok(prettyplease::unparse(&syn::parse_file(
&["fn main()", code.as_ref()].concat(),
)?))
}
}
#[cfg(debug_assertions)]
impl Debug for Challenge {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Challenge")
.field("code", &self.code().to_token_stream().to_string())
.field("type_name", self.type_name())
.finish()
}
}

32
src/challenge/lists.rs Normal file
View file

@ -0,0 +1,32 @@
use std::collections::HashMap;
use super::Challenge;
macro_rules! challenges {
( $( $key:expr => $expr:expr ),* $(,)? ) => {{
HashMap::from([
$(
(
$key,
Challenge {
code: {
let expr: syn::Expr = syn::parse_quote!($expr);
expr
},
type_name: std::any::type_name_of_val(&($expr)),
}
),
)*
])
}};
}
pub fn all() -> HashMap<u16, Challenge> {
challenges! {
1 => {
let a: u8 = 3;
let b: u8 = 4;
a + b
}
}
}

View file

@ -1,6 +1,8 @@
use rocket::{get, routes};
mod challenge;
mod markdown;
use markdown::{Markdown, MarkdownConvertError};
type MarkdownResult = Result<Markdown, MarkdownConvertError>;
@ -22,5 +24,7 @@ fn two() -> MarkdownResult {
#[shuttle_runtime::main]
async fn main() -> shuttle_rocket::ShuttleRocket {
#[cfg(debug_assertions)]
dbg!(challenge::lists::all());
Ok(rocket::build().mount("/", routes![index, one, two]).into())
}