xcrab/src/msg/main.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

2022-06-27 02:43:22 -05:00
// Copyright (C) 2022 Infoshock Tech
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2022-07-10 06:44:47 -05:00
#![warn(clippy::pedantic)]
2022-06-27 00:53:40 -05:00
mod config;
2022-07-11 02:33:45 -05:00
use std::error::Error;
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
2022-06-27 01:01:43 -05:00
use tokio::net::UnixStream;
2022-07-11 02:33:45 -05:00
type Result<T> = std::result::Result<T, Box<dyn Error>>;
struct CustomError(String);
impl Debug for CustomError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str(&self.0)
}
}
impl Display for CustomError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str(&self.0)
}
}
impl Error for CustomError {}
2022-06-27 00:53:40 -05:00
#[tokio::main]
async fn main() -> Result<()> {
2022-06-27 01:33:06 -05:00
let msg = std::env::args().skip(1).collect::<Vec<String>>().join(" ");
2022-07-02 13:33:57 -05:00
let conf = config::load_file();
2022-06-27 00:53:40 -05:00
2022-07-03 12:53:35 -05:00
let path = conf.msg.socket_path;
2022-06-27 01:01:43 -05:00
let stream = UnixStream::connect(path).await?;
let (mut read, mut write) = stream.into_split();
2022-06-27 01:01:43 -05:00
write.write_all(msg.as_bytes()).await?;
drop(write); // Shutdown the writer half so that the write actually goes through
// "Don't cross the streams!""
let mut buf = String::new();
read.read_to_string(&mut buf).await?;
if !buf.is_empty() {
2022-07-11 02:33:45 -05:00
return Err(CustomError(buf).into());
}
2022-06-27 01:01:43 -05:00
2022-06-27 00:53:40 -05:00
Ok(())
}