#![deny(clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]
mod config;
mod entities;
mod error;
mod server;
use std::path::PathBuf;
use crate::{
error::Result,
server::{Connection, Server},
};
use actix::{Actor, AsyncContext, System};
use clap::Clap;
use displaydoc::Display;
use thiserror::Error;
use tokio::net::TcpListener;
#[derive(Error, Debug, Display)]
pub enum InitError {
TcpBind(std::io::Error),
ConfigRead(std::io::Error),
ConfigParse(toml::de::Error),
}
#[derive(Clap)]
#[clap(version = clap::crate_version!(), author = clap::crate_authors!())]
struct Opts {
#[clap(short, long)]
config: PathBuf,
}
#[actix_rt::main]
async fn main() -> Result<()> {
let opts: Opts = Opts::parse();
let config = std::fs::read(&opts.config).map_err(InitError::ConfigRead)?;
let config: config::Config = toml::from_slice(&config).map_err(InitError::ConfigParse)?;
let listener = TcpListener::bind(&config.socket_address)
.await
.map_err(InitError::TcpBind)?;
let stream = async_stream::stream! {
loop {
match listener.accept().await {
Ok((socket, remote)) => yield Connection(socket, remote),
Err(e) => eprintln!("Couldn't establish connection: {:?}", e)
}
}
};
Server::create(move |ctx| {
ctx.add_message_stream(stream);
Server::new()
});
println!("Running IRC server on {}", &config.socket_address);
tokio::signal::ctrl_c().await.expect("ctrl-c io");
System::current().stop();
Ok(())
}