mirror of
https://github.com/mikedilger/chorus.git
synced 2026-08-01 07:21:39 +00:00
Config, and loading at startup
This commit is contained in:
parent
57425f824b
commit
4d9de16eba
30
Cargo.lock
generated
30
Cargo.lock
generated
@ -38,12 +38,27 @@ dependencies = [
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.21.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.5.0"
|
||||
@ -69,6 +84,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
name = "chorus"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"ron",
|
||||
"serde",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
@ -200,7 +216,19 @@ version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4722d768eff46b75989dd134e5c353f0d6296e5aaa3132e776cbdb56be7731aa"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"bitflags 1.3.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ron"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bitflags 2.4.1",
|
||||
"serde",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@ -8,6 +8,7 @@ repository = "https://github.com/mikedilger/chorus"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
ron = "0.8"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
thiserror = "1.0"
|
||||
tokio = { version = "1", features = [ "full" ] }
|
||||
|
||||
4
sample/sample.config.ron
Normal file
4
sample/sample.config.ron
Normal file
@ -0,0 +1,4 @@
|
||||
Config(
|
||||
ip_address: "127.0.0.1",
|
||||
port: 8080,
|
||||
)
|
||||
16
src/config.rs
Normal file
16
src/config.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub ip_address: String,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Config {
|
||||
Config {
|
||||
ip_address: "127.0.0.1".to_string(),
|
||||
port: 80,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,10 @@ use thiserror::Error;
|
||||
/// Errors that can occur in the chorus crate
|
||||
#[derive(Error, Debug)]
|
||||
pub enum Error {
|
||||
// Config
|
||||
#[error("Config: {0}")]
|
||||
Config(#[from] ron::error::SpannedError),
|
||||
|
||||
// I/O Error
|
||||
#[error("I/O: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
|
||||
20
src/main.rs
20
src/main.rs
@ -1,9 +1,29 @@
|
||||
|
||||
pub mod config;
|
||||
pub mod error;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::error::Error;
|
||||
use std::env;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Read;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error> {
|
||||
// Get args (config path)
|
||||
let mut args = env::args();
|
||||
if args.len() <= 1 {
|
||||
panic!("USAGE: chorus <config_path>");
|
||||
}
|
||||
let _ = args.next(); // ignore program name
|
||||
let config_path = args.next().unwrap();
|
||||
|
||||
// Read config file
|
||||
let mut file = OpenOptions::new().read(true).open(config_path)?;
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
let config: Config = ron::from_str(&contents)?;
|
||||
|
||||
println!("No main yet.");
|
||||
|
||||
Ok(())
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user