serde json
This commit is contained in:
1031
priprava/rust_priprava9.md
Normal file
1031
priprava/rust_priprava9.md
Normal file
File diff suppressed because it is too large
Load Diff
123
riesenie1/Cargo.lock
generated
Normal file
123
riesenie1/Cargo.lock
generated
Normal file
@@ -0,0 +1,123 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.14.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8ca58f447f06ed17d5fc4043ce1b10dd205e060fb3ce5b979b8ed8e59ff3f79"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.106"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "21b2ebcf727b7760c461f091f9f0f539b77b8e87f2fd88131e7f1b433b3cece4"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "riesenie1"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.149"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8848ee67ecc8aedbaf3e4122217aff892639231befc6a1b58d29fff4c2cabaa"
|
||||
9
riesenie1/Cargo.toml
Normal file
9
riesenie1/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "riesenie1"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
itertools = "0.14.0"
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.149"
|
||||
118
riesenie1/src/lib.rs
Normal file
118
riesenie1/src/lib.rs
Normal file
@@ -0,0 +1,118 @@
|
||||
// src/lib.rs
|
||||
use std::fmt;
|
||||
use std::fs::File;
|
||||
use std::fs;
|
||||
use std::collections::HashMap;
|
||||
use std::io::Write;
|
||||
|
||||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Kniha {
|
||||
autori: Vec<String>,
|
||||
nazov: String,
|
||||
vydavatelstvo: String,
|
||||
zaner: String,
|
||||
pocet_stran: usize,
|
||||
isbn: String,
|
||||
rok_vydania: String,
|
||||
pozicana: bool,
|
||||
stav_knihy: Stav,
|
||||
}
|
||||
|
||||
impl fmt::Display for Kniha {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "({:#?}, {}, {}, {}, {}, {}, {}, {}, {:#?})",
|
||||
self.autori,
|
||||
self.nazov,
|
||||
self.vydavatelstvo,
|
||||
self.zaner,
|
||||
self.pocet_stran,
|
||||
self.isbn,
|
||||
self.rok_vydania,
|
||||
self.pozicana,
|
||||
self.stav_knihy,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, serde::Serialize, serde::Deserialize, PartialEq)]
|
||||
pub enum Stav {
|
||||
#[default]
|
||||
Nova,
|
||||
Pouzivana,
|
||||
Poskodena,
|
||||
Vyradena
|
||||
}
|
||||
|
||||
impl fmt::Display for Stav {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Stav::Nova => write!(f, "Nova"),
|
||||
Stav::Pouzivana => write!(f, "Pouzivana"),
|
||||
Stav::Poskodena => write!(f, "Poskodena"),
|
||||
Stav::Vyradena => write!(f, "Vyradena"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, serde::Serialize, serde::Deserialize)]
|
||||
pub struct Kniznica {
|
||||
knihy: Vec<Kniha>
|
||||
}
|
||||
|
||||
impl Kniznica {
|
||||
pub fn nacitaj_zo_suboru(r: &std::path::PathBuf) -> Option<Kniznica> {
|
||||
let d: String = std::fs::read_to_string(r).ok()?;
|
||||
let r: Option<Kniznica> = serde_json::from_str(&d).ok()?;
|
||||
r
|
||||
}
|
||||
|
||||
pub fn uloz_do_suboru(&self, z: &std::path::PathBuf) -> bool {
|
||||
let Ok(f) = File::create(z) else{
|
||||
return false;
|
||||
};
|
||||
serde_json::to_writer_pretty(f, &self).is_ok()
|
||||
}
|
||||
|
||||
pub fn pridaj_knihu(&mut self, k: Kniha) -> Result<(), ()> {
|
||||
if self.knihy.iter().map(|x| x.isbn.clone()).any(|y| y == k.isbn) {
|
||||
return Err(());
|
||||
}
|
||||
self.knihy.push(k);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn odstran_knihu(&mut self, r: &str) -> Result<Kniha, ()> {
|
||||
let Some(k) = self.knihy.iter().position(|x| x.isbn == r) else {
|
||||
return Err(());
|
||||
};
|
||||
let t = self.knihy.remove(k);
|
||||
Ok(t)
|
||||
}
|
||||
|
||||
pub fn daj_knihu_podla_isbn(&self, i: &str) -> Option<&Kniha> {
|
||||
self.knihy.iter().find(|x| x.isbn == i)
|
||||
}
|
||||
|
||||
pub fn daj_knihy_autora(&self, a: &str) -> Vec<&Kniha> {
|
||||
self.knihy.iter().filter(|y| y.autori.contains(&a.to_string())).collect()
|
||||
}
|
||||
|
||||
pub fn daj_knihy_podla_stavu(&self, s: Stav) -> Vec<&Kniha> {
|
||||
self.knihy.iter().filter(|x| x.stav_knihy == s).collect()
|
||||
}
|
||||
|
||||
pub fn vypis_vydavatelstva_a_pocet_knih(&self) {
|
||||
let mut h: HashMap<String, usize> = HashMap::new();
|
||||
for x in self.knihy.iter() {
|
||||
h.entry(x.vydavatelstvo.clone()).and_modify(|x| *x += 1).or_insert(1);
|
||||
}
|
||||
// self.knihy.iter().counts();
|
||||
for y in h.iter() {
|
||||
println!("{}: {} knih", y.0, y.1)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn vypis_knihy_daneho_zanru(&self, zk: &str) {
|
||||
self.knihy.iter().filter(|&x| x.zaner == zk).map(|x| println!("{:#?}", x));
|
||||
}
|
||||
}
|
||||
3
riesenie1/src/main.rs
Normal file
3
riesenie1/src/main.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
fn main() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user