problem set generator
1
2use genpdf::fonts::FontData;
3use hyphenation::{Load, Standard};
4
5mod util;
6use crate::util::{build_document_base, build_response};
7
8mod problems;
9
10use axum::{Router, extract::{Path, State}, response::IntoResponse, routing::get};
11
12#[derive(Clone)]
13pub struct GenpdfState {
14 pub font_family:genpdf::fonts::FontFamily<FontData>,
15 pub hyphenator: Standard
16}
17
18
19async fn send_pdf(
20 State(state):State<GenpdfState>,
21 key: Option<Path<i32>>)
22-> impl IntoResponse {
23
24 let nelem_p = problems::nelem::build_nelem();
25
26 let len = match key {
27 Some(Path(a)) => a,
28 None => 1,
29 };
30
31 let mut doc = build_document_base(state);
32
33 for i in 0..len {
34
35 doc.push(genpdf::elements::Text::new(format!("question {i}"))) ;
36 let (text,answer) = nelem_p.generate_question();
37 for l in text.lines() {
38 doc.push(
39 genpdf::elements::Text::new(
40 l
41 ));
42 }
43
44 doc.push(genpdf::elements::Text::new("Answer:"));
45 doc.push(genpdf::elements::Text::new(answer));
46 doc.push(genpdf::elements::Text::new(""));
47
48 };
49
50
51 build_response(doc)
52}
53
54
55#[tokio::main]
56async fn main() {
57
58 let en_us = Standard::from_embedded(hyphenation::Language::EnglishUS).unwrap();
59 let font_family = match genpdf::fonts::from_files("./fonts", "Roboto", None) {
60 Ok(a) => a,
61 Err(err) => panic!("errpr {err}")
62 };
63
64 let genpdf_state = GenpdfState {
65 font_family:font_family,
66 hyphenator:en_us
67 };
68
69 let routes = Router::new()
70 .route("/{key}", get(send_pdf))
71 .route("/", get(send_pdf))
72 .with_state(genpdf_state);
73
74
75
76 let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
77 axum::serve(listener,routes).await.unwrap();
78
79
80}
81
82
83
84
85//#[test]
86//fn test_string_to_question_parse() {
87// let test_question = Question {
88// subject:util::Subject::Math,
89// theme:util::Theme::Math(util::MathTheme::ParametricEquations),
90// text: "\n\n{{0}} + y = 12".to_string(),
91// var_conditions:vec![(1,4)],
92// ans_expression: None};
93// let test_string: &str = "Math;
94//ParametricEquations;
95//
96//
97//{{0}} + y = 12;
98//1,4";
99//
100// let text = Question::from_str(test_string).unwrap().generate_question();
101// println!("{}\n",text);
102// assert_eq!(Question::from_str(test_string).unwrap(),test_question);
103//
104//}