This commit is contained in:
Priec
2026-02-15 23:56:52 +01:00
parent f8814e7e1a
commit 7fe89b0361
2 changed files with 97 additions and 1 deletions

View File

@@ -78,6 +78,30 @@ impl Filmoteka {
result 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 { pub fn uloz_do_suboru(&self, cesta: &std::path::PathBuf) -> bool {
let Ok(json) = serde_json::to_string_pretty(&self) else { let Ok(json) = serde_json::to_string_pretty(&self) else {
return false; return false;
@@ -86,3 +110,22 @@ impl Filmoteka {
} }
} }
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
})
}
}

View File

@@ -1,5 +1,58 @@
fn main() { fn main() {
println!("Hello, world!"); match std::fs::read_to_string("subor.txt") {
Ok(x) => print!("{x}"),
Err(y) => print!("{y}"),
};
} }
fn nacitaj_a_parsuj(cesta: &str) -> Result<i32, Box<dyn std::error::Error>> {
let obsah = match std::fs::read_to_string(cesta) {
Ok(s) => s,
Err(e) => return Err(Box::new(e)),
};
let cislo = match obsah.trim().parse::<i32>() {
Ok(n) => n,
Err(e) => return Err(Box::new(e)),
};
Ok(cislo)
}
fn nacitaj_a_parsuj2(cesta: &str) -> Result<i32, Box<dyn std::error::Error>> {
let obsah = std::fs::read_to_string(cesta)?;
let cislo = obsah.trim().parse::<i32>()?;
Ok(cislo)
}
fn prvy_sused(vektor: &[i32], index: usize) -> Option<i32> {
if index == 0 {
return None;
}
let predchadzajuci = index.checked_sub(1);
match predchadzajuci {
Some(i) => {
if i < vektor.len() {
Some(vektor[i])
} else {
None
}
}
None => None,
}
}
fn prvy_sused2(vektor: &[i32], index: usize) -> Option<i32> {
if index == 0 {
return None;
}
let predchadzajuci = index.checked_sub(1)?;
if predchadzajuci < vektor.len() {
return Some(vektor[predchadzajuci]);
}
None
}
fn nacitaj_json(cesta: &str) -> Option<Vec<String>> {
let nacitane = std::fs::read_to_string(cesta).ok()?;
let vec = serde_json::from_str::<Vec<String>>(nacitane.as_str());
Some(vec.ok()?)
}