Use configuration file (config.json)

This commit is contained in:
2026-01-14 18:36:01 +02:00
parent f0b12f673d
commit 62e14798b6
5 changed files with 156 additions and 2 deletions

100
Cargo.lock generated
View File

@@ -2,6 +2,106 @@
# It is not intended for manual editing.
version = 4
[[package]]
name = "itoa"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2"
[[package]]
name = "memchr"
version = "2.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273"
[[package]]
name = "proc-macro2"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9695f8df41bb4f3d222c95a67532365f569318332d03d5f3f67f37b20e6ebdf0"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rs-irc"
version = "0.1.0"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
"serde_derive",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3084b546a1dd6289475996f182a22aba973866ea8e8b02c51d9f46b1336a22da"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "syn"
version = "2.0.113"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678faa00651c9eb72dd2020cbdf275d92eccb2400d568e419efdd64838145cb4"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
[[package]]
name = "zmij"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30e0d8dffbae3d840f64bda38e28391faef673a7b5a6017840f2a106c8145868"

View File

@@ -5,3 +5,5 @@ edition = "2024"
authors = ["icedragon"]
[dependencies]
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1"

6
config.json Normal file
View File

@@ -0,0 +1,6 @@
{
"nick": "RustBot",
"ident": "icedragon",
"gecos": "IceDragon's Rust client thingy",
"server": "irc.quickfox.net:6667"
}

36
src/config.rs Normal file
View File

@@ -0,0 +1,36 @@
use std::fs;
use std::io;
use serde::Deserialize;
//// Config Example
// {
// "nick": "RustBot",
// "ident": "icedragon",
// "gecos": "IceDragon's Rust client thingy",
// "server": "irc.quickfox.net:6667"
// }
#[derive(Debug, Deserialize)]
pub struct IrcConfig {
pub nick: String, // "RustBot"
pub ident: String, // "icedragon"
pub gecos: String, // "IceDragon's Rust Bot Thing"
pub server: String, // "irc.quickfox.net:6667"
}
/// Load IRC configuration from a given file:
///
/// # Examples
///
/// ```rust,nodoc
/// let conf = config::load_config("config.json").expect("failed to load config");
///
/// ```
pub fn load_config(path: &str) -> io::Result<IrcConfig> {
let json_string = fs::read_to_string(path)?;
match serde_json::from_str(&json_string) {
Ok(conf) => Ok(conf),
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidInput, e.to_string())),
}
}

View File

@@ -2,6 +2,8 @@ use std::net::TcpStream;
use std::io::{self, Read, Write};
use std::ops::ControlFlow;
mod config;
#[derive(Debug)]
#[allow(dead_code)]
enum IrcClientMessage<'msg> {
@@ -280,7 +282,15 @@ impl Client {
}
fn main() -> io::Result<()> {
let mut client = Client::new("RustBot", "icedragon", "IceDragon's Rust client thingy");
client.connect("irc.quickfox.net:6667")?;
const CONFIG_FILE: &str = "config.json";
// load configuration
println!("[++] Loading configuration from {CONFIG_FILE}...");
let config = config::load_config(CONFIG_FILE)?;
// create & connect client
println!("[++] Creating & connecting bot client (nick: {})", config.nick);
let mut client = Client::new(&config.nick, &config.ident, &config.gecos);
client.connect(&config.server)?;
client.serve()
}