Debian Codegraph @

src/scores/scores.rs

raw symbols SCIP
1 /*
2 * Copyright 2024 Andrey Kutejko <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 *
17 * For more details see the file COPYING.
18 */
19
20 use superrust-analyzer cargo gnome-robots 50.0.0 scores/::ROWS_TO_DISPLAYrust-analyzer cargo gnome-robots 50.0.0 scores/ROWS_TO_DISPLAY.;
21 use superrust-analyzer cargo gnome-robots 50.0.0 scores/::new_score_dialogrust-analyzer cargo gnome-robots 50.0.0 scores/new_score_dialog/::new_score_dialogrust-analyzer cargo gnome-robots 50.0.0 scores/new_score_dialog/new_score_dialog().;
22 use superrust-analyzer cargo gnome-robots 50.0.0 scores/::scores_dialogrust-analyzer cargo gnome-robots 50.0.0 scores/scores_dialog/::scores_dialogrust-analyzer cargo gnome-robots 50.0.0 scores/scores_dialog/scores_dialog().;
23 use craterust-analyzer cargo gnome-robots 50.0.0 crate/::configrust-analyzer cargo gnome-robots 50.0.0 config/::PACKAGE;
24 use craterust-analyzer cargo gnome-robots 50.0.0 crate/::gamerust-analyzer cargo gnome-robots 50.0.0 game/::MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#;
25 use craterust-analyzer cargo gnome-robots 50.0.0 crate/::utilsrust-analyzer cargo gnome-robots 50.0.0 utils/::{list_directoryrust-analyzer cargo gnome-robots 50.0.0 utils/list_directory()., nowrust-analyzer cargo gnome-robots 50.0.0 utils/now().};
26 use gettextrsrust-analyzer cargo gettext-rs 0.7.7 crate/::gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().;
27 use gtkrust-analyzer cargo gtk4 0.11.1 crate/::{glibrust-analyzer cargo glib 0.22.3 crate/, preluderust-analyzer cargo gtk4 0.11.1 prelude/::*};
28 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Reverserust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Reverse#;
29 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#;
30 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/;
31 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#};
32 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#};
33
34 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
35 pub struct Categoryrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category# {
36 pub keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
37 pub safetyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#safety.: MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#,
38 }
39
40 impl Categoryrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category# {
41 fn from_game_typerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category]from_game_type().(game_type: &str) -> Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category] {
42 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(key) = game_type.strip_suffixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_suffix().("-super-safe") {
43 Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category] {
44 keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key.: key.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(),
45 safetyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#safety.: MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::SuperSaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#SuperSafe#,
46 }
47 } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(key) = game_type.strip_suffixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_suffix().("-safe") {
48 Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category] {
49 keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key.: key.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(),
50 safetyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#safety.: MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Saferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Safe#,
51 }
52 } else {
53 Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category] {
54 keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key.: game_type.to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(),
55 safetyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#safety.: MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Unsaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Unsafe#,
56 }
57 }
58 }
59
60 fn game_typerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category]game_type().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
61 match self.safetyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#safety. {
62 MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Unsaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Unsafe# => self.keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key..replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(' ', "_").clonerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][Clone]clone().(),
63 MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Saferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Safe# => formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}-safe", self.keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key..replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(' ', "_")),
64 MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::SuperSaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#SuperSafe# => formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}-super-safe", self.keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key..replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(' ', "_")),
65 }
66 }
67
68 pub fn namerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category]name().(&self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> {
69 match (self.keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key..as_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]as_str().(), self.safetyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#safety.) {
70 ("classic_robots", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Unsaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Unsafe#) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Classic robots")),
71 ("classic_robots", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Saferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Safe#) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Classic robots with safe moves")),
72 ("classic_robots", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::SuperSaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#SuperSafe#) => {
73 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Classic robots with super-safe moves"))
74 }
75 ("nightmare", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Unsaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Unsafe#) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Nightmare")),
76 ("nightmare", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Saferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Safe#) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Nightmare with safe moves")),
77 ("nightmare", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::SuperSaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#SuperSafe#) => {
78 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Nightmare with super-safe moves"))
79 }
80 ("robots2", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Unsaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Unsafe#) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Robots2")),
81 ("robots2", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Saferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Safe#) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Robots2 with safe moves")),
82 ("robots2", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::SuperSaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#SuperSafe#) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Robots2 with super-safe moves")),
83 ("robots2_easy", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Unsaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Unsafe#) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Robots2 easy")),
84 ("robots2_easy", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Saferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Safe#) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Robots2 easy with safe moves")),
85 ("robots2_easy", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::SuperSaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#SuperSafe#) => {
86 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Robots2 easy with super-safe moves"))
87 }
88 ("robots_with_safe_teleport", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Unsaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Unsafe#) => {
89 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Robots with safe teleport"))
90 }
91 ("robots_with_safe_teleport", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::Saferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#Safe#) => {
92 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Robots with safe teleport with safe moves"))
93 }
94 ("robots_with_safe_teleport", MoveSafetyrust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#::SuperSaferust-analyzer cargo gnome-robots 50.0.0 game/MoveSafety#SuperSafe#) => {
95 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(gettextrust-analyzer cargo gettext-rs 0.7.7 gettext().("Robots with safe teleport with super-safe moves"))
96 }
97 _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
98 }
99 }
100 }
101
102 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
103 pub struct Scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score# {
104 pub scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#score.: i64,
105 pub timerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#time.: u64,
106 pub userrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#user.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#,
107 }
108
109 impl Scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score# {
110 fn from_linerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score]from_line().(line: &str, fallback_user: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score]> {
111 let mut tokens = line.splitnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]splitn().(3, ' ');
112
113 let score = tokens.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitN<'a, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
114 let time = tokens.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitN<'a, P>`][Iterator]next().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch()..parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
115 let user = tokens.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitN<'a, P>`][Iterator]next().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(fallback_user).to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().();
116
117 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score] { scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#score., timerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#time., userrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#user. })
118 }
119
120 fn to_linerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score]to_line().(&self) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
121 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{} {} {}", self.scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#score., self.timerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#time., self.userrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#user.)
122 }
123
124 fn renamerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score]rename().(self, user: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score] {
125 Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score] { userrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#user., ..self }
126 }
127 }
128
129 fn save_score_to_filerust-analyzer cargo gnome-robots 50.0.0 scores/scores/save_score_to_file().(scores_dir: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, category: &Categoryrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#, score: &Scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
130 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::create_dir_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/create_dir_all().(scores_dir)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
131
132 let filename = scores_dir.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(category.game_typerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category]game_type().());
133 let mut file = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::OpenOptionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/OpenOptions#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]new().()
134 .createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]create().(true)
135 .appendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]append().(true)
136 .openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]open().(&filename)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
137 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(file, "{}", score.to_linerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score]to_line().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
138 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
139 }
140
141 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
142 pub struct ScoreListrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList# {
143 pub scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList#scores.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#>,
144 }
145
146 impl ScoreListrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList# {
147 pub fn loadrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[ScoreList]load().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, fallback_user: &str) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[ScoreList]> {
148 let mut scores = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
149 let file = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::OpenOptionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/OpenOptions#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]new().().readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]read().(true).openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]open().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
150 for line in iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReaderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/BufReader#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/buffered/bufreader/impl#[`BufReader<R>`]new().(file).linesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#lines().() {
151 let line = line?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
152 match Scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#::from_linerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score]from_line().(&line, fallback_user) {
153 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(score) => scores.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(score),
154 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
155 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!(
156 "Failed to read malformed score {} in {}.",
157 line,
158 path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()
159 )
160 }
161 }
162 }
163 scores.sort_by_keyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by_key().(|s| Reverserust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Reverse#(s.scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#score.));
164 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[ScoreList] { scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList#scores. })
165 }
166
167 pub fn high_scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[ScoreList]high_scores().(&self, n: usize) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&Scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#> {
168 self.scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList#scores..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(n).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
169 }
170
171 pub fn addrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[ScoreList]add().(&mut self, score: Scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#) {
172 self.scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList#scores..pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(score);
173 self.scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList#scores..sort_by_keyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]sort_by_key().(|s| Reverserust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Reverse#(s.scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#score.));
174 }
175 }
176
177 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)]
178 pub struct Scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Scores# {
179 pub scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Scores#scores.: BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<Categoryrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#, ScoreListrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList#>,
180 }
181
182 impl Scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Scores# {
183 fn loadrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Scores]load().(dir: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Scores]> {
184 let mut scores = BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V>`]new().();
185
186 let user = glibrust-analyzer cargo glib 0.22.3 crate/::real_namerust-analyzer cargo glib 0.22.3 auto/functions/real_name().();
187 let user = user.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().();
188
189 let files = list_directoryrust-analyzer cargo gnome-robots 50.0.0 utils/list_directory().(dir)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
190 for file in files {
191 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(file_name) = file.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(|s| s.to_strrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_str().()) else {
192 continue;
193 };
194
195 let category = Categoryrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#::from_game_typerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category]from_game_type().(file_name);
196
197 if category.namerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category]name().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
198 continue;
199 }
200
201 let scores_of_single_category = ScoreListrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList#::loadrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[ScoreList]load().(&file, &user)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
202
203 scores.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().(category, scores_of_single_category);
204 }
205
206 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Scores] { scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Scores#scores. })
207 }
208 }
209
210 fn scores_dirrust-analyzer cargo gnome-robots 50.0.0 scores/scores/scores_dir().() -> PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf# {
211 glibrust-analyzer cargo glib 0.22.3 crate/::user_data_dirrust-analyzer cargo glib 0.22.3 auto/functions/user_data_dir().().joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(PACKAGE).joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("scores")
212 }
213
214 pub fn show_scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/show_scores().(category: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&Categoryrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#>, parent_window: &impl IsArust-analyzer cargo glib 0.22.3 object/IsA#<gtkrust-analyzer cargo gtk4 0.11.1 crate/::Windowrust-analyzer cargo gtk4 0.11.1 auto/window/Window#>) {
215 let scores_dir = scores_dirrust-analyzer cargo gnome-robots 50.0.0 scores/scores/scores_dir().();
216 let scores = Scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Scores#::loadrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Scores]load().(&scores_dir).unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|e| {
217 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("Failed to load scores: {}", e);
218 Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Scores][Default]default().()
219 });
220 let dialog = scores_dialogrust-analyzer cargo gnome-robots 50.0.0 scores/scores_dialog/scores_dialog().(scores, category, parent_window.upcast_refrust-analyzer cargo glib 0.22.3 object/Cast#upcast_ref().());
221 dialog.presentrust-analyzer cargo gtk4 0.11.1 auto/window/GtkWindowExt#present().();
222 }
223
224 pub async fn add_scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/add_score().(category: &Categoryrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#, score: i64, parent_window: &gtkrust-analyzer cargo gtk4 0.11.1 crate/::Windowrust-analyzer cargo gtk4 0.11.1 auto/window/Window#) {
225 if category.namerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category]name().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
226 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("Failed to add score for unknown game '{:?}'.", category);
227 };
228
229 let user = glibrust-analyzer cargo glib 0.22.3 crate/::real_namerust-analyzer cargo glib 0.22.3 auto/functions/real_name().();
230 let user = user.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]to_string_lossy().();
231
232 let score = Scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score# {
233 scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#score.,
234 timerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#time.: nowrust-analyzer cargo gnome-robots 50.0.0 utils/now().(),
235 userrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#user.: user.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
236 };
237
238 let scores_dir = scores_dirrust-analyzer cargo gnome-robots 50.0.0 scores/scores/scores_dir().();
239 let mut score_list = ScoreListrust-analyzer cargo gnome-robots 50.0.0 scores/scores/ScoreList#::loadrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[ScoreList]load().(&scores_dir.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(category.game_typerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Category]game_type().()), &user)
240 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
241 eprintlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprintln!!("Failed to read scores for {}: {}", category.keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key., err);
242 err
243 })
244 .unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_default().();
245
246 let high_score_gained = {
247 let best_scores = score_list.high_scoresrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[ScoreList]high_scores().(ROWS_TO_DISPLAYrust-analyzer cargo gnome-robots 50.0.0 scores/ROWS_TO_DISPLAY.);
248 best_scores.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().ROWS_TO_DISPLAYrust-analyzer cargo gnome-robots 50.0.0 scores/ROWS_TO_DISPLAY. || score.scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#score. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i64][`PartialOrd<Self>`]gt().best_scores.lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().scorerust-analyzer cargo gnome-robots 50.0.0 scores/scores/Score#score.
249 };
250
251 if high_score_gained {
252 score_list.addrust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[ScoreList]add().(score.clonerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score][Clone]clone().());
253
254 let new_name = new_score_dialogrust-analyzer cargo gnome-robots 50.0.0 scores/new_score_dialog/new_score_dialog().(&score, category, &score_list, parent_window).rust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().awaitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core future/future/Future#poll().;
255 if let Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) = save_score_to_filerust-analyzer cargo gnome-robots 50.0.0 scores/scores/save_score_to_file().(&scores_dir, category, &score.renamerust-analyzer cargo gnome-robots 50.0.0 scores/scores/impl#[Score]rename().(new_name)) {
256 eprintrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/eprint!!("Failed to save scores for {}: {}", category.keyrust-analyzer cargo gnome-robots 50.0.0 scores/scores/Category#key., error);
257 }
258 }
259 }