134 lines
3.2 KiB
Rust
134 lines
3.2 KiB
Rust
pub mod priprava3;
|
|
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
use std::fmt;
|
|
use std::fs;
|
|
|
|
#[derive(Default, Serialize, Deserialize, PartialEq, Clone)]
|
|
pub enum Zaner {
|
|
Akcia,
|
|
Komedia,
|
|
Horor,
|
|
#[default]
|
|
Drama,
|
|
Scifi,
|
|
Dokumentarny
|
|
}
|
|
|
|
impl fmt::Display for Zaner {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Zaner::Akcia => write!(f, "Akcia"),
|
|
Zaner::Komedia => write!(f, "Komedia"),
|
|
Zaner::Horor => write!(f, "Horor"),
|
|
Zaner::Drama => write!(f, "Drama"),
|
|
Zaner::Scifi => write!(f, "Scifi"),
|
|
Zaner::Dokumentarny => write!(f, "Dokumentarny"),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Default, Serialize, Deserialize, Clone)]
|
|
pub struct Film {
|
|
nazov: String,
|
|
reziser: String,
|
|
rok: u16,
|
|
zaner: Zaner,
|
|
hodnotenie: f32
|
|
}
|
|
|
|
impl fmt::Display for Film {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
write!(f, "({}, {}, {}, {}, {})",
|
|
self.nazov,
|
|
self.reziser,
|
|
self.rok,
|
|
self.zaner,
|
|
self.hodnotenie
|
|
)
|
|
}
|
|
}
|
|
|
|
#[derive(Default, Serialize, Deserialize)]
|
|
pub struct Filmoteka {
|
|
filmy: Vec<Film>,
|
|
}
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, PartialEq)]
|
|
pub enum Stav {
|
|
Aktivny,
|
|
Pozastaveny { dovod: String },
|
|
Ukonceny { datum: String, hodnotenie: u8 },
|
|
}
|
|
|
|
impl fmt::Display for Stav {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
match self {
|
|
Stav::Aktivny => write!(f, "Aktivny"),
|
|
Stav::Pozastaveny { dovod } => write!(f, "Pozastaveny: {}", dovod),
|
|
Stav::Ukonceny { datum, hodnotenie } => write!(f, "Ukončený dňa {} s hodnotením {}", datum, hodnotenie),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Filmoteka {
|
|
pub fn nacitaj_zo_suboru(cesta: &std::path::PathBuf) -> Option<Filmoteka> {
|
|
let raw = fs::read_to_string(cesta).ok()?;
|
|
let result = serde_json::from_str(&raw).ok();
|
|
result
|
|
}
|
|
|
|
fn nacitaj(cesta: &str) -> Result<String, std::io::Error> {
|
|
std::fs::read_to_string(cesta)
|
|
}
|
|
|
|
fn nacitaj2(cesta: &str) -> bool {
|
|
std::fs::read_to_string(cesta).is_ok()
|
|
}
|
|
|
|
fn nacitaj3(cesta: &str) -> Result<String, String> {
|
|
match std::fs::read_to_string(cesta) {
|
|
Ok(obsah) => Ok(obsah),
|
|
Err(err) => Err(err.to_string()),
|
|
}
|
|
}
|
|
|
|
fn parsuj_cislo(text: &str) -> Option<i32> {
|
|
let x = text.parse::<i32>();
|
|
x.ok()
|
|
}
|
|
|
|
fn parsuj_cislo2(text: &str) -> Option<i32> {
|
|
text.parse::<i32>().ok()
|
|
}
|
|
|
|
pub fn uloz_do_suboru(&self, cesta: &std::path::PathBuf) -> bool {
|
|
let Ok(json) = serde_json::to_string_pretty(&self) else {
|
|
return false;
|
|
};
|
|
fs::write(cesta, json).is_ok()
|
|
}
|
|
|
|
}
|
|
|
|
impl Film {
|
|
pub fn new(
|
|
nazov: &str,
|
|
reziser: &str,
|
|
rok: u16,
|
|
zaner: Zaner,
|
|
hodnotenie: f32
|
|
) -> Option<Self> {
|
|
if hodnotenie < 0.0 || hodnotenie > 10.0 {
|
|
return None;
|
|
};
|
|
return Some(Self {
|
|
nazov: nazov.to_string(),
|
|
reziser: reziser.to_string(),
|
|
rok, zaner, hodnotenie
|
|
})
|
|
}
|
|
}
|