mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-28 11:10:44 +01:00
feat: add saving client settings to file
This commit is contained in:
26
src/settings/client_settings.rs
Normal file
26
src/settings/client_settings.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::settings::general_settings::{DEFAULT_URL, SettingsLike};
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
pub struct ClientSettings {
|
||||
pub server_url: SocketAddr,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl Default for ClientSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
server_url: DEFAULT_URL,
|
||||
name: "Player".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SettingsLike for ClientSettings {
|
||||
fn get_file_path() -> Result<std::path::PathBuf, Box<dyn core::error::Error>> {
|
||||
Ok(Self::get_config_dir()?.join("client.toml"))
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
use core::error;
|
||||
use std::path::PathBuf;
|
||||
use std::{
|
||||
net::{IpAddr, Ipv4Addr, SocketAddr},
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
use log::warn;
|
||||
use serde::{Serialize, de::DeserializeOwned};
|
||||
|
||||
pub const DEFAULT_URL: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 6767);
|
||||
|
||||
// maybe consider some better name for this
|
||||
pub trait SettingsLike: Serialize + DeserializeOwned + Default {
|
||||
fn get_config_dir() -> Result<PathBuf, Box<dyn error::Error>> {
|
||||
@@ -31,6 +36,8 @@ pub trait SettingsLike: Serialize + DeserializeOwned + Default {
|
||||
Ok(toml::from_slice(&std::fs::read(Self::get_file_path()?)?)?) // i love rust
|
||||
}
|
||||
|
||||
/// Try to create new instance of `Self` with values from config file. If there is no content
|
||||
/// defaults to Self::default. If config file is invalid overwrites it with default values
|
||||
fn new() -> Self {
|
||||
match Self::read_from_file() {
|
||||
Ok(settings) => return settings,
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
pub mod client_settings;
|
||||
pub mod general_settings;
|
||||
pub mod server_settings;
|
||||
|
||||
@@ -5,7 +5,7 @@ use std::{
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::settings::general_settings::SettingsLike;
|
||||
use crate::settings::general_settings::{DEFAULT_URL, SettingsLike};
|
||||
|
||||
// TODO! More settings!
|
||||
#[derive(Serialize, Deserialize)]
|
||||
@@ -15,19 +15,19 @@ pub struct ServerSettings {
|
||||
pub allow_spectators: bool,
|
||||
}
|
||||
|
||||
impl SettingsLike for ServerSettings {
|
||||
fn get_file_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
|
||||
Ok(Self::get_config_dir()?.join("server.toml"))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ServerSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 6767),
|
||||
addr: DEFAULT_URL,
|
||||
max_connections: 2, // Maybe this should be a const somewhere in the server code? It
|
||||
// makes no sense to have different value than this
|
||||
allow_spectators: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SettingsLike for ServerSettings {
|
||||
fn get_file_path() -> Result<PathBuf, Box<dyn std::error::Error>> {
|
||||
Ok(Self::get_config_dir()?.join("server.toml"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user