Skip to content

Commit e422286

Browse files
committed
Put all external paths in one place
Making it easy to modify and to change using conditional compilation if needed.
1 parent 420eb82 commit e422286

File tree

5 files changed

+29
-21
lines changed

5 files changed

+29
-21
lines changed

src/config.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ pub static SETTINGS: Lazy<OfflinePuzzlesConfig> = Lazy::new(|| {
1313
pub const CHESS_ALPHA_BYTES: &[u8] = include_bytes!("../font/Alpha.ttf");
1414
pub const CHESS_ALPHA: Font = iced::Font::with_name("Chess Alpha");
1515

16+
//pub const FONT_DIRECTORY: &str = "font/";
17+
pub const PUZZLES_DIRECTORY: &str = "puzzles/";
18+
pub const TRANSLATIONS_DIRECTORY: &str = "./translations/";
19+
pub const SETTINGS_FILE: &str = "settings.json";
20+
pub const PIECES_DIRECTORY: &str = "pieces/";
21+
1622
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1723
pub enum GameMode {
1824
Puzzle,
@@ -53,7 +59,7 @@ impl ::std::default::Default for OfflinePuzzlesConfig {
5359
window_width: 1010.,
5460
window_height: 680.,
5561
maximized: false,
56-
puzzle_db_location: String::from("puzzles/lichess_db_puzzle.csv"),
62+
puzzle_db_location: String::from(PUZZLES_DIRECTORY) + "lichess_db_puzzle.csv",
5763
piece_theme: styles::PieceTheme::Cburnett,
5864
search_results_limit: 20000,
5965
play_sound: true,
@@ -76,7 +82,7 @@ impl ::std::default::Default for OfflinePuzzlesConfig {
7682

7783
pub fn load_config() -> OfflinePuzzlesConfig {
7884
let config;
79-
let file = std::fs::File::open("settings.json");
85+
let file = std::fs::File::open(SETTINGS_FILE);
8086
match file {
8187
Ok(file) => {
8288
let reader = std::io::BufReader::new(file);

src/download_db.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ use std::fs::OpenOptions;
99
use zstd;
1010

1111
use crate::Message;
12-
13-
pub const LICHESS_ZST_FILE : &str = "puzzles/lichess_db_puzzle.csv.zst";
12+
use crate::config::PUZZLES_DIRECTORY;
1413

1514
pub enum DownloadState {
1615
StartDownload {
@@ -31,7 +30,7 @@ pub fn download_lichess_db(url: String, path: String) -> Subscription<Message> {
3130
}
3231

3332
fn download_stream(url: String, path: String) -> impl Stream<Item = Message> {
34-
33+
let lichess_zst_file = String::from(PUZZLES_DIRECTORY) + "lichess_db_puzzle.csv.zst";
3534
stream::channel(100,
3635
|mut output| async move {
3736
let mut state = DownloadState::StartDownload{ url , path: path.clone()};
@@ -43,7 +42,7 @@ fn download_stream(url: String, path: String) -> impl Stream<Item = Message> {
4342
match response {
4443
Ok(response) => {
4544
if let Some(total) = response.content_length() {
46-
let file = OpenOptions::new().append(true).read(true).create(true).open(LICHESS_ZST_FILE).expect("Unable to create lichess db archive.");
45+
let file = OpenOptions::new().append(true).read(true).create(true).open(&lichess_zst_file).expect("Unable to create lichess db archive.");
4746
state = DownloadState::DownloadInProgress {
4847
response,
4948
file,
@@ -80,7 +79,7 @@ fn download_stream(url: String, path: String) -> impl Stream<Item = Message> {
8079
let target = std::fs::File::create(path.clone()).expect(&("Error creating file ".to_owned() + &path));
8180
zstd::stream::copy_decode(file, target).unwrap();
8281

83-
let _ = std::fs::remove_file(LICHESS_ZST_FILE);
82+
let _ = std::fs::remove_file(&lichess_zst_file);
8483
let _ = output.send(Message::DBDownloadFinished).await;
8584

8685
state = DownloadState::Finished;

src/lang.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ use unic_langid::langid;
44
use std::io::Read;
55
use once_cell::sync::Lazy;
66

7+
use crate::config::TRANSLATIONS_DIRECTORY;
8+
79
static BUNDLE_ENUS: Lazy<FluentBundle<FluentResource, intl_memoizer::concurrent::IntlLangMemoizer>> = Lazy::new(|| {
8-
let file = std::fs::File::open("./translations/en-US/ocp.ftl").unwrap();
10+
let file = std::fs::File::open(TRANSLATIONS_DIRECTORY.to_owned() + "en-US/ocp.ftl").unwrap();
911
let mut reader = std::io::BufReader::new(file);
1012
let mut source = String::new();
1113
reader.read_to_string(&mut source).expect("Failed to read en-US translation file");
@@ -16,7 +18,7 @@ static BUNDLE_ENUS: Lazy<FluentBundle<FluentResource, intl_memoizer::concurrent:
1618
});
1719

1820
static BUNDLE_PTBR: Lazy<FluentBundle<FluentResource, intl_memoizer::concurrent::IntlLangMemoizer>> = Lazy::new(|| {
19-
let file = std::fs::File::open("./translations/pt-BR/ocp.ftl").unwrap();
21+
let file = std::fs::File::open(TRANSLATIONS_DIRECTORY.to_owned() + "pt-BR/ocp.ftl").unwrap();
2022
let mut reader = std::io::BufReader::new(file);
2123
let mut source = String::new();
2224
reader.read_to_string(&mut source).expect("Failed to read pt-BR translation file");
@@ -27,7 +29,7 @@ static BUNDLE_PTBR: Lazy<FluentBundle<FluentResource, intl_memoizer::concurrent:
2729
});
2830

2931
static BUNDLE_ES: Lazy<FluentBundle<FluentResource, intl_memoizer::concurrent::IntlLangMemoizer>> = Lazy::new(|| {
30-
let file = std::fs::File::open("./translations/es/ocp.ftl").unwrap();
32+
let file = std::fs::File::open(TRANSLATIONS_DIRECTORY.to_owned() + "es/ocp.ftl").unwrap();
3133
let mut reader = std::io::BufReader::new(file);
3234
let mut source = String::new();
3335
reader.read_to_string(&mut source).expect("Failed to read ES translation file");
@@ -38,7 +40,7 @@ static BUNDLE_ES: Lazy<FluentBundle<FluentResource, intl_memoizer::concurrent::I
3840
});
3941

4042
static BUNDLE_FR: Lazy<FluentBundle<FluentResource, intl_memoizer::concurrent::IntlLangMemoizer>> = Lazy::new(|| {
41-
let file = std::fs::File::open("./translations/fr/ocp.ftl").unwrap();
43+
let file = std::fs::File::open(TRANSLATIONS_DIRECTORY.to_owned() + "fr/ocp.ftl").unwrap();
4244
let mut reader = std::io::BufReader::new(file);
4345
let mut source = String::new();
4446
reader.read_to_string(&mut source).expect("Failed to read FR translation file");

src/search_tab.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io::BufReader;
66

77
use iced_aw::TabLabel;
88
use chess::{Piece, PROMOTION_PIECES};
9-
use crate::config::load_config;
9+
use crate::config::{load_config, SETTINGS_FILE, PIECES_DIRECTORY};
1010
use crate::styles::PieceTheme;
1111
use crate::{Tab, Message, config, styles, lang, db, openings};
1212

@@ -197,11 +197,11 @@ pub fn gen_piece_vec(theme: &PieceTheme) -> Vec<Handle> {
197197
let mut handles = Vec::<Handle>::with_capacity(5);
198198
let theme_str = &theme.to_string();
199199
// this first entry won't be used, it's there just to fill the vec, so we can index by the Piece
200-
handles.insert(0, Handle::from_path("pieces/cburnett/wP.svg"));
201-
handles.insert(Piece::Knight.to_index(), Handle::from_path(String::from("pieces/") + &theme_str + "/wN.svg"));
202-
handles.insert(Piece::Bishop.to_index(), Handle::from_path(String::from("pieces/") + &theme_str + "/wB.svg"));
203-
handles.insert(Piece::Rook.to_index(), Handle::from_path(String::from("pieces/") + &theme_str + "/wR.svg"));
204-
handles.insert(Piece::Queen.to_index(), Handle::from_path(String::from("pieces/") + &theme_str + "/wQ.svg"));
200+
handles.insert(0, Handle::from_path(String::from(PIECES_DIRECTORY) + "cburnett/wP.svg"));
201+
handles.insert(Piece::Knight.to_index(), Handle::from_path(String::from(PIECES_DIRECTORY) + &theme_str + "/wN.svg"));
202+
handles.insert(Piece::Bishop.to_index(), Handle::from_path(String::from(PIECES_DIRECTORY) + &theme_str + "/wB.svg"));
203+
handles.insert(Piece::Rook.to_index(), Handle::from_path(String::from(PIECES_DIRECTORY) + &theme_str + "/wR.svg"));
204+
handles.insert(Piece::Queen.to_index(), Handle::from_path(String::from(PIECES_DIRECTORY) + &theme_str + "/wQ.svg"));
205205
handles
206206
}
207207

@@ -297,7 +297,7 @@ impl SearchTab {
297297
}
298298

299299
pub fn save_search_settings(min_rating: i32, max_rating: i32, min_popularity: i32, theme: TacticalThemes, opening: Openings, variation: Variation, op_side: Option<OpeningSide>) {
300-
let file = std::fs::File::open("settings.json");
300+
let file = std::fs::File::open(SETTINGS_FILE);
301301
if let Ok(file) = file {
302302
let buf_reader = BufReader::new(file);
303303
if let Ok(mut config) = serde_json::from_reader::<std::io::BufReader<std::fs::File>, config::OfflinePuzzlesConfig>(buf_reader) {
@@ -309,7 +309,7 @@ impl SearchTab {
309309
config.last_variation = variation;
310310
config.last_opening_side = op_side;
311311

312-
let file = std::fs::File::create("settings.json");
312+
let file = std::fs::File::create(SETTINGS_FILE);
313313
if let Ok(file) = file {
314314
if serde_json::to_writer_pretty(file, &config).is_err() {
315315
println!("Error saving search options.");

src/settings.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use iced_aw::TabLabel;
66
use rfd::AsyncFileDialog;
77

88
use crate::{Message, Tab, config, styles, lang, lang::PickListWrapper};
9+
use crate::config::SETTINGS_FILE;
910

1011
#[derive(Debug, Clone)]
1112
pub enum SettingsMessage {
@@ -158,7 +159,7 @@ impl SettingsTab {
158159
last_variation: self.saved_configs.last_variation.clone(),
159160
last_opening_side: self.saved_configs.last_opening_side,
160161
};
161-
let file = std::fs::File::create("settings.json");
162+
let file = std::fs::File::create(SETTINGS_FILE);
162163
match file {
163164
Ok(file) => {
164165
if serde_json::to_writer_pretty(file, &config).is_ok() {
@@ -183,7 +184,7 @@ impl SettingsTab {
183184
config.window_width = self.window_width;
184185
config.window_height = self.window_height;
185186
config.maximized = self.maximized;
186-
let file = std::fs::File::create("settings.json");
187+
let file = std::fs::File::create(SETTINGS_FILE);
187188
match file {
188189
Ok(file) => {
189190
if serde_json::to_writer_pretty(file, &config).is_err() {

0 commit comments

Comments
 (0)