forge/forge-client/src/main.rs

109 lines
2.6 KiB
Rust
Raw Normal View History

2023-04-02 23:22:41 +00:00
use paris::{error, info, success};
2023-04-02 22:07:23 +00:00
use serde::{Deserialize, Serialize};
2023-04-02 20:33:58 +00:00
use std::error::Error;
2023-04-02 22:07:23 +00:00
use std::fmt;
2023-04-02 20:33:58 +00:00
use tokio::io::AsyncWriteExt;
use tokio::net::TcpStream;
2023-04-02 22:07:23 +00:00
use url::Url;
2023-04-02 23:01:47 +00:00
use uuid::Uuid;
2023-04-02 22:07:23 +00:00
#[derive(Serialize, Deserialize, Debug)]
enum BuildSystem {
Cargo,
Make,
Custom(String),
}
impl fmt::Display for BuildSystem {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
BuildSystem::Cargo => write!(f, "cargo"),
BuildSystem::Make => write!(f, "make"),
BuildSystem::Custom(s) => write!(f, "{}", s),
}
}
}
#[derive(Serialize, Deserialize, Debug)]
enum Subcommand {
Run,
Build,
Install,
Custom(String),
}
impl fmt::Display for Subcommand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Subcommand::Run => write!(f, "run"),
Subcommand::Build => write!(f, "build"),
Subcommand::Install => write!(f, "install"),
Subcommand::Custom(s) => write!(f, "{}", s),
}
}
}
#[derive(Serialize, Deserialize)]
enum Tag {
Release,
Debug,
}
impl fmt::Display for Tag {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Tag::Release => write!(f, "--release"),
Tag::Debug => write!(f, "--debug"),
}
}
}
#[derive(Serialize, Deserialize)]
struct Command {
build_system: BuildSystem,
subcommand: Subcommand,
features: Option<Vec<String>>,
tag: Tag,
2023-04-02 23:01:47 +00:00
}
#[derive(Serialize, Deserialize)]
struct Message {
uuid: Uuid,
pre_exec: Option<String>,
profile: bool,
command: Command,
2023-04-02 22:07:23 +00:00
repository: Url,
2023-04-02 23:01:47 +00:00
basename: String,
2023-04-02 22:07:23 +00:00
}
2023-04-02 20:33:58 +00:00
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Connect to a peer
let mut stream = TcpStream::connect("127.0.0.1:9134").await?;
2023-04-02 23:22:41 +00:00
success!("Connected to: <green>127.0.0.1:9134<//>");
2023-04-02 20:33:58 +00:00
2023-04-02 22:07:23 +00:00
let test_command = Command {
build_system: BuildSystem::Cargo,
subcommand: Subcommand::Build,
features: Some(vec!["one".to_string(), "two".to_string()]),
tag: Tag::Debug,
2023-04-02 23:01:47 +00:00
};
let test_message = Message {
uuid: Uuid::new_v4(),
pre_exec: None,
profile: false,
command: test_command,
2023-04-02 22:07:23 +00:00
repository: Url::parse("https://lib.rs/crates/serde_json").unwrap(),
2023-04-02 23:01:47 +00:00
basename: "serde_json".to_string(),
2023-04-02 22:07:23 +00:00
};
2023-04-02 23:01:47 +00:00
let j = serde_json::to_string(&test_message)?;
2023-04-02 22:07:23 +00:00
stream.write_all(&j.as_bytes()).await?;
2023-04-02 23:22:41 +00:00
info!(
"Sent JSON:\n<bright-white>{}<//>",
serde_json::to_string_pretty(&test_message)?
);
2023-04-02 20:33:58 +00:00
Ok(())
2023-04-02 18:01:11 +00:00
}