Skip to content

Commit

Permalink
Add cmd args for json generation
Browse files Browse the repository at this point in the history
  • Loading branch information
morr0ne committed Sep 4, 2024
1 parent 9f80a58 commit 2724941
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 23 deletions.
187 changes: 187 additions & 0 deletions gen/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ publish = false

[dependencies]
anyhow = "1.0.86"
clap = { version = "4.5.16", features = ["derive"] }
heck = "0.5.0"
proc-macro2 = { version = "1.0.86", default-features = false }
quick-xml = { version = "0.36.1", features = ["serialize", "overlapped-lists"] }
Expand Down
61 changes: 38 additions & 23 deletions gen/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use anyhow::Result;
use clap::Parser;
use parser::Protocol;
use std::{fs::OpenOptions, io::Write as _};
use tracing::info;

mod client;
mod parser;
Expand Down Expand Up @@ -79,37 +81,50 @@ const PROTOCOLS: &[&str] = &[
"wlr-protocols/unstable/wlr-virtual-pointer-unstable-v1.xml",
];

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long)]
json: bool,
}

fn main() -> Result<()> {
tracing_subscriber::fmt::init();

let Args { json } = Args::parse();

let protocols = PROTOCOLS
.iter()
.map(Protocol::from_path)
.collect::<Result<Vec<Protocol>>>()?;

let mut server_path = OpenOptions::new()
.truncate(true)
.write(true)
.create(true)
.open("src/server/protocol.rs")?;

write!(&mut server_path, "{}", generate_server_code(&protocols))?;

let mut client_path = OpenOptions::new()
.truncate(true)
.write(true)
.create(true)
.open("src/client/protocol.rs")?;

write!(&mut client_path, "{}", generate_client_code(&protocols))?;

let mut json_path = OpenOptions::new()
.truncate(true)
.write(true)
.create(true)
.open("protocols.json")?;

serde_json::to_writer(&mut json_path, &protocols)?;
if json {
info!("Generating json file");

let mut json_path = OpenOptions::new()
.truncate(true)
.write(true)
.create(true)
.open("protocols.json")?;

serde_json::to_writer(&mut json_path, &protocols)?;
} else {
let mut server_path = OpenOptions::new()
.truncate(true)
.write(true)
.create(true)
.open("src/server/protocol.rs")?;

write!(&mut server_path, "{}", generate_server_code(&protocols))?;

let mut client_path = OpenOptions::new()
.truncate(true)
.write(true)
.create(true)
.open("src/client/protocol.rs")?;

write!(&mut client_path, "{}", generate_client_code(&protocols))?;
}

Ok(())
}

0 comments on commit 2724941

Please sign in to comment.