Merge pull request 'cli: self made error type with thiserror crate' (#23) from feature/thiserror into rc
commit
d6de6948f8
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "noxis-cli"
|
name = "noxis-cli"
|
||||||
version = "0.1.6"
|
version = "0.2.4"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
@ -8,4 +8,5 @@ anyhow = "1.0.94"
|
||||||
clap = { version = "4.5.22", features = ["derive"] }
|
clap = { version = "4.5.22", features = ["derive"] }
|
||||||
serde = { version = "1.0.215", features = ["derive"] }
|
serde = { version = "1.0.215", features = ["derive"] }
|
||||||
serde_json = "1.0.133"
|
serde_json = "1.0.133"
|
||||||
|
thiserror = "2.0.11"
|
||||||
tokio = { version = "1.42.0", features = ["full", "net"] }
|
tokio = { version = "1.42.0", features = ["full", "net"] }
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
use thiserror::Error;
|
||||||
|
use super::cli_net::NOXIS_RS_CREDS;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum NoxisCliError {
|
||||||
|
#[error("Can't send any data to {:?}. Noxis-rs daemon is disabled or can't be accessed", NOXIS_RS_CREDS)]
|
||||||
|
NoxisDaemonMissing,
|
||||||
|
#[error("Noxis CLI can't write any data to the Noxis-rs port. Check daemon and it's web-functionality")]
|
||||||
|
PortIsNotWritable,
|
||||||
|
#[error("Can't send Cli-prompt to the Noxis-rs. Check it's state")]
|
||||||
|
CliPromptCanNotBeSent,
|
||||||
|
#[error("Can't parse CLI struct and send as byte stream")]
|
||||||
|
ToStringCliParsingParsing,
|
||||||
|
}
|
||||||
|
|
@ -3,26 +3,28 @@ use tokio::io::AsyncWriteExt;
|
||||||
use tokio::time::{Duration, sleep};
|
use tokio::time::{Duration, sleep};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use super::Cli;
|
use super::Cli;
|
||||||
|
use super::cli_error::NoxisCliError;
|
||||||
|
|
||||||
|
pub const NOXIS_RS_CREDS: &str = "127.0.0.1:7753";
|
||||||
|
|
||||||
|
|
||||||
pub async fn create_tcp_stream() -> Result<TcpStream> {
|
pub async fn create_tcp_stream() -> Result<TcpStream> {
|
||||||
let stream = TcpStream::connect("127.0.0.1:7753").await?;
|
Ok(TcpStream::connect(NOXIS_RS_CREDS).await.map_err(|_| NoxisCliError::NoxisDaemonMissing)?)
|
||||||
Ok(stream)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn try_send(stream: Result<TcpStream>, params: Cli) -> Result<()> {
|
pub async fn try_send(stream: Result<TcpStream>, params: Cli) -> Result<()> {
|
||||||
use serde_json::to_string;
|
use serde_json::to_string;
|
||||||
let mut stream = stream?;
|
let mut stream = stream.map_err(|_| NoxisCliError::NoxisDaemonMissing)?;
|
||||||
loop {
|
loop {
|
||||||
if stream.writable().await.is_err() {
|
if stream.writable().await.is_err() {
|
||||||
sleep(Duration::from_millis(100)).await;
|
sleep(Duration::from_millis(100)).await;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// let msg: Cli = from_str(&format!("{:?}", params))?;
|
// let msg: Cli = from_str(&format!("{:?}", params))?;
|
||||||
let msg= to_string(¶ms)?;
|
let msg= to_string(¶ms).map_err(|_| NoxisCliError::ToStringCliParsingParsing)?;
|
||||||
// let msg = r"HTTP/1.1 POST\r\nContent-Length: 14\r\nContent-Type: text/plain\r\n\r\nHello, World!@";
|
// let msg = r"HTTP/1.1 POST\r\nContent-Length: 14\r\nContent-Type: text/plain\r\n\r\nHello, World!@";
|
||||||
|
|
||||||
stream.write_all(msg.as_bytes()).await?;
|
stream.write_all(msg.as_bytes()).await.map_err(|_| NoxisCliError::CliPromptCanNotBeSent)?;
|
||||||
// ...
|
// ...
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
mod cli;
|
mod cli;
|
||||||
|
mod cli_net;
|
||||||
|
mod cli_error;
|
||||||
|
|
||||||
pub use cli::*;
|
pub use cli::*;
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
mod cli;
|
mod cli;
|
||||||
mod net;
|
mod cli_net;
|
||||||
|
mod cli_error;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use cli::Cli;
|
use cli::Cli;
|
||||||
use net::{create_tcp_stream, try_send};
|
use cli_net::{create_tcp_stream, try_send};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue