Skip to content

Commit

Permalink
Port everything to rocket v0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
cubetastic33 committed Jan 2, 2025
1 parent 5bcecca commit 7b9b9d1
Show file tree
Hide file tree
Showing 12 changed files with 2,557 additions and 1,702 deletions.
3,195 changes: 2,004 additions & 1,191 deletions Cargo.lock

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ edition = "2021"

[dependencies]
dotenv = "0.15"
regex = "1.4"
argon2 = "0.3"
rand = "0.8.3"
rocket = { version = "0.4.10", features = ["private-cookies"] }
rocket_contrib = { version = "0.4.10", features = ["tera_templates"] }
multipart = "0.17.1"
zip = { version = "0.5.9", default-features = false, features = ["deflate"] }
rusqlite = { version = "0.24.2", features = ["bundled"] }
postgres = { version = "0.19", features = ["with-chrono-0_4"] }
postgres-native-tls = "0.5"
native-tls = "0.2.7"
chrono = "0.4"
uuid = { version = "0.8.2", features = ["v4"] }
regex = "1.11"
argon2 = "0.5"
rand = "0.8.5"
sqlx = { version = "0.7", default-features = false, features = ["chrono", "sqlite"] }
rocket = { version = "0.5.1", features = ["secrets", "json"] }
rocket_dyn_templates = { version = "0.2.0", features = ["tera"] }
rocket_db_pools = { version = "0.2.0", features = ["sqlx_postgres"] }
zip = { version = "2.2.2", default-features = false, features = ["deflate"] }
async_zip = { version = "0.0.17", features = ["tokio", "tokio-fs"] }
async-compression = { version = "0.4.18", features = ["futures-io", "zstd"] }
# rusqlite = { version = "0.32.0", features = ["bundled"] }
native-tls = "0.2.12"
uuid = { version = "1.11.0", features = ["v4", "fast-rng"] }
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
reqwest = { version = "0.11", features = ["blocking", "json"] }
zstd = "0.13.0"
chrono-tz = "0.8.5"
reqwest = { version = "0.12", features = ["json"] }
zstd = "0.13"
chrono-tz = "0.10"
1 change: 0 additions & 1 deletion rust-toolchain

This file was deleted.

45 changes: 22 additions & 23 deletions src/actions.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{ErrResponse, FormatError};

use super::QuizSettings;
use postgres::Client;
use rand::prelude::*;
use rocket::request::Form;
use rocket::{form::Form, futures::TryStreamExt, http::Status, tokio::fs};
use sqlx::{PgConnection, Row};
use std::{
collections::{HashMap, HashSet},
error::Error,
num::ParseIntError,
fs,
};

pub trait Sentence {
Expand Down Expand Up @@ -61,8 +62,8 @@ impl Sentence for (String, String, String, String, HashSet<char>, Option<usize>)
}
}

pub fn fill_sentences<T: Sentence>(
client: &mut Client,
pub async fn fill_sentences<T: Sentence>(
db: &mut PgConnection,
sentences: &mut Vec<T>,
add_overrides: bool,
) -> Result<(), Box<dyn Error>> {
Expand All @@ -75,7 +76,7 @@ pub fn fill_sentences<T: Sentence>(
}
}
// Add the readings from the file
let kana_records = fs::read_to_string("kana_sentences.txt")?;
let kana_records = fs::read_to_string("kana_sentences.txt").await?;
for result in kana_records.lines() {
// Parse the values
let record: Vec<_> = result.split('\t').collect();
Expand All @@ -95,12 +96,10 @@ pub fn fill_sentences<T: Sentence>(
}

// Add the overrides
for row in client
.query(
"SELECT * FROM overrides WHERE sentence_id = ANY($1) ORDER BY primary_value DESC",
&[&queue.keys().collect::<Vec<_>>()],
)?
{
let mut rows = sqlx::query(
"SELECT * FROM overrides WHERE sentence_id = ANY($1) ORDER BY primary_value DESC"
).bind(queue.keys().copied().collect::<Vec<_>>()).fetch(db);
while let Some(row) = rows.try_next().await? {
let indices = queue.get(&row.get("sentence_id")).ok_or("Error: Sentence ID from DB not found in queue")?;
// The concept of a primary value exists only for readings
let override_type = row.get("override_type");
Expand All @@ -121,18 +120,18 @@ pub fn fill_sentences<T: Sentence>(
Ok(())
}

pub fn get_sentences(
client: &mut Client,
pub async fn get_sentences(
db: &mut PgConnection,
quiz_settings: Form<QuizSettings>,
) -> Result<Vec<[String; 4]>, Box<dyn Error>> {
let mut sentences = Vec::new();
let mut rng = thread_rng();
let mut rng = rand::rngs::OsRng::default();

let known_kanji: HashSet<_> = quiz_settings.known_kanji.chars().collect();
let known_priority_kanji: HashSet<_> = quiz_settings.known_priority_kanji.chars().collect();
// Read the sentences and shuffle the order
let sentence_records = fs::read_to_string("sentences.csv")?;
let mut sentence_records: Vec<_> = sentence_records.lines().collect();
let contents: String = fs::read_to_string("sentences.csv").await?;
let mut sentence_records: Vec<_> = contents.lines().collect();
sentence_records.shuffle(&mut rng);

// Iterate over the sentences
Expand Down Expand Up @@ -168,18 +167,18 @@ pub fn get_sentences(
}
}
// Fill the readings and overrides
fill_sentences(client, &mut sentences, true)?;
fill_sentences(db, &mut sentences, true).await?;
Ok(sentences)
}

pub fn generate_essay(client: &mut Client, quiz_settings: Form<QuizSettings>) -> Result<Vec<[String; 4]>, Box<dyn Error>> {
pub async fn generate_essay(db: &mut PgConnection, quiz_settings: Form<QuizSettings>) -> Result<Vec<[String; 4]>, ErrResponse> {
let mut essay = Vec::new();
let mut sentences = Vec::new();
let mut rng = thread_rng();
let mut rng = rand::rngs::OsRng::default();

let mut known_kanji: HashSet<_> = quiz_settings.known_kanji.chars().collect();
// Read the sentences and shuffle the order
let sentence_records = fs::read_to_string("sentences.csv")?;
let sentence_records = fs::read_to_string("sentences.csv").await.format_error("Error reading sentences.csv")?;
let sentence_records: Vec<_> = sentence_records.lines().collect();

// Filter the sentences so we're left with the ones that only have kanji the user knows
Expand Down Expand Up @@ -208,7 +207,7 @@ pub fn generate_essay(client: &mut Client, quiz_settings: Form<QuizSettings>) ->
}
}
// Fill the readings and overrides
fill_sentences(client, &mut sentences, true)?;
fill_sentences(db, &mut sentences, true).await.format_error("Error in fill_sentences")?;

// As long as we have known kanji that aren't in the essay, keep iterating
while known_kanji.len() != 0 {
Expand Down Expand Up @@ -244,7 +243,7 @@ pub fn generate_essay(client: &mut Client, quiz_settings: Form<QuizSettings>) ->
}

// Add a random sentence with a lot of known kanji to the essay
let choice = tuples.choose(&mut rng).ok_or("Error: Failed to choose from empty vector of tuples")?;
let choice = tuples.choose(&mut rng).ok_or((Status::InternalServerError, "Error: Failed to choose from empty vector of tuples".to_string()))?;
essay.push([
choice.0.to_owned(),
choice.1.to_owned(),
Expand Down
Loading

0 comments on commit 7b9b9d1

Please sign in to comment.