first commit

This commit is contained in:
Your Name 2025-05-15 01:47:25 +02:00
commit 75b31e233d
4 changed files with 545 additions and 0 deletions

320
Cargo.lock generated Normal file
View file

@ -0,0 +1,320 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "base64"
version = "0.21.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9"
[[package]]
name = "bincode"
version = "1.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad"
dependencies = [
"serde",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
dependencies = [
"serde",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "dirs"
version = "5.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44c45a9d03d6676652bcb5e724c7e988de1acad23a711b5217ab9cbecbec2225"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "520f05a5cbd335fae5a99ff7a6ab8627577660ee5cfd6a94a6a929b52ff0321c"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys",
]
[[package]]
name = "getrandom"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe9006bed769170c11f845cf00c7c1e9092aeb3f268e007c3e760ac68008070f"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
[[package]]
name = "libredox"
version = "0.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85c833ca1e66078851dba29046874e38f08b2c883700aa29a03ddd3b23814ee8"
dependencies = [
"bitflags 2.4.1",
"libc",
"redox_syscall",
]
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a293318316cf6478ec1ad2a21c49390a8d5b5eae9fab736467d93fbc0edc29c5"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quizlib"
version = "0.1.0"
dependencies = [
"bincode",
"dirs",
"rand",
"ron",
"serde",
]
[[package]]
name = "quote"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "redox_syscall"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "redox_users"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4"
dependencies = [
"getrandom",
"libredox",
"thiserror",
]
[[package]]
name = "ron"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94"
dependencies = [
"base64",
"bitflags 2.4.1",
"serde",
"serde_derive",
]
[[package]]
name = "serde"
version = "1.0.163"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2113ab51b87a539ae008b5c6c02dc020ffa39afd2d83cffcb3f4eb2722cebec2"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.163"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c805777e3930c8883389c602315a24224bcc738b63905ef87cd1420353ea93e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "syn"
version = "2.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee659fb5f3d355364e1f3e5bc10fb82068efbf824a1e9d1c9504244a6469ad53"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2cd5904763bad08ad5513ddbb12cf2ae273ca53fa9f68e843e236ec6dfccc09"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.53"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dcf4a824cce0aeacd6f38ae6f24234c8e80d68632338ebaa1443b5df9e29e19"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-ident"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

12
Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "quizlib"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = {features = ["derive"] , version ="*"}
rand = "*"
dirs = "*"
ron = "*"

1
README.md Normal file
View file

@ -0,0 +1 @@
# quizlib

212
src/lib.rs Normal file
View file

@ -0,0 +1,212 @@
use rand::prelude::IteratorRandom;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Default, Clone, PartialEq, Deserialize, Debug, Serialize, Eq)]
pub struct Frage {
pub text: Option<String>,
pub extras: FragenExtra,
}
#[derive(Default, Clone, PartialEq, Deserialize, Debug, Serialize, Eq)]
pub enum FragenExtra {
#[default]
None,
Video(PathBuf),
Image(PathBuf),
Audio(PathBuf),
}
#[derive(Default, Clone, PartialEq, Deserialize, Debug, Serialize, Eq)]
pub enum Awnsers {
#[default]
None,
Free(String),
Multiple(Vec<(String, bool)>),
}
#[derive(Default, Clone, PartialEq, Deserialize, Debug, Serialize, Eq)]
pub struct Kategory {
pub kat: Vec<u16>,
pub name: String,
}
#[derive(Default, Clone, PartialEq, Deserialize, Debug, Serialize, Eq)]
pub struct Question {
pub question: Frage,
pub awnsers: Awnsers,
pub id: Vec<u16>,
pub index: usize,
}
#[derive(Default, Clone, PartialEq, Deserialize, Debug, Serialize, Eq)]
pub struct Db {
pub name: String,
pub questions: Vec<Question>,
pub kategorys: Vec<Kategory>,
}
pub fn get_viedeo(_path: PathBuf) -> Vec<u8> {
vec![]
}
pub fn get_audio(_path: PathBuf) -> Vec<u8> {
vec![]
}
pub fn get_image(_path: PathBuf) -> Vec<u8> {
vec![]
}
pub fn format_kat(vec: &Vec<u16>) -> String {
let mut vec2 = vec.clone();
vec2.pop();
let mut st = vec2
.into_iter()
.filter(|t| if t > &0 { true } else { false })
.map(|t| format!("{}.", t))
.collect::<String>();
st.push_str(&format!("{}", vec.last().unwrap()));
st
}
pub fn format_kat_kat(vec: &Vec<u16>) -> String {
vec.clone()
.into_iter()
.filter(|t| if t > &0 { true } else { false })
.map(|t| format!("{}.", t))
.collect::<String>()
}
pub fn compare_kat(kat: &Vec<u16>, test: &Vec<u16>) -> bool {
if kat.len() >= (test.len() + 2) {
kat.starts_with(test) && kat[test.len() + 1] == 0 && kat[test.len()] != 0
} else if kat.len() >= (test.len() + 1) {
kat.starts_with(test) && kat[test.len()] != 0
} else {
false
}
}
pub fn compare_results(result: &Vec<bool>, sol: &Vec<(String, bool)>) -> Vec<bool> {
result
.iter()
.zip(sol.iter())
.map(|(res, (_, sol))| res == sol)
.collect()
}
#[derive(Default, Clone, PartialEq, Deserialize, Debug, Serialize, Eq)]
pub struct Modul {
pub desc: Modul2,
pub path: PathBuf,
}
#[derive(Default, Clone, PartialEq, Deserialize, Debug, Serialize, Eq)]
pub struct Modul2 {
pub name: String,
pub version: String,
pub count: u32,
pub time: u32,
}
#[derive(Default, Serialize, Deserialize, Debug, Clone)]
pub struct TSafe {
pub sol: Vec<Sol>,
pub current: usize,
}
impl TSafe {
pub fn add_solution(&mut self, question: &Question, is_checked: &Vec<bool>) {
if let Some(index) = self.sol.iter().position(|sol| sol.index == question.index) {
self.sol[index].awm.push((
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs(),
if let Awnsers::Multiple(vec) = &question.awnsers {
compare_results(is_checked, vec)
} else {
vec![]
},
));
} else {
self.sol.push(Sol {
index: question.index.clone(),
id: question.id.clone(),
awm: vec![(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs(),
if let Awnsers::Multiple(vec) = &question.awnsers {
compare_results(is_checked, vec)
} else {
vec![]
},
)],
});
self.current = question.index.clone() + 1;
}
}
}
#[derive(Serialize, Deserialize, Debug, Default, Clone, PartialEq, Eq)]
pub struct Sol {
pub id: Vec<u16>,
pub index: usize,
pub awm: Vec<(u64, Vec<bool>)>,
}
fn _get_false_questions(safe: &TSafe, elf: &Db) -> Vec<Question> {
let _temps: Vec<&Question> = elf.questions[..safe.current]
.iter()
.filter(move |x| {
(safe
.sol
.iter()
.find(|y| y.index == x.index)
.unwrap()
.awm
.iter()
.map(|c| c.1.clone())
.flatten()
.count())
> 2
})
.choose_multiple(&mut rand::rng(), 20);
let temps: Vec<&Question> = elf.questions[..safe.current]
.iter()
.filter(move |x| x.id.is_empty())
.choose_multiple(&mut rand::rng(), 10);
temps.iter().copied().map(|a| a.clone()).collect()
}
pub fn get_test(se: &Vec<Question>) -> Vec<Question> {
let mut temp_vec: Vec<&Question> = vec![];
let mut kats = se
.iter()
.map(|kat| kat.id.first().unwrap())
.collect::<Vec<&u16>>();
kats.dedup();
for i in kats {
let temps: Vec<&Question> = se
.iter()
.filter(move |x| x.id.first().unwrap() == i)
.choose_multiple(&mut rand::rng(), 20);
temp_vec.extend(temps);
}
temp_vec.iter().copied().map(|m| m.clone()).collect()
}
impl Db {
pub fn get_questions(&self, safe: &TSafe) -> Vec<Question> {
let vec = self.questions[(safe.current)..].to_vec();
//vec.extend(get_false_questions(&safe,self ));
//(
vec //,self.questions.len()-safe.current)
}
pub fn get_kategory(&self, kat: &Vec<u16>) -> Vec<Question> {
self.questions
.iter()
.filter(|ele| {
if ele.id.starts_with(&kat) {
//println!("{:?}",ele);
true
} else {
false
}
})
.map(|ele| ele.clone())
.collect()
}
}