1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- use anyhow::Result;
- use clap::Clap;
- use std::fs::File;
- use std::io::Read;
- #[derive(Debug, Clap)]
- pub struct Opts {
- #[clap(subcommand)]
- pub command: Command,
- }
- #[derive(Debug, Clap)]
- pub enum Command {
- /// Outputs an interface definition file.
- Idl {
- /// Path to the program's interface definition.
- #[clap(short, long)]
- file: String,
- /// Output file for the idl (stdout if not specified).
- #[clap(short, long)]
- out: Option<String>,
- },
- /// Generates a client module.
- Gen {
- /// Path to the program's interface definition.
- #[clap(short, long, required_unless_present("idl"))]
- file: Option<String>,
- /// Output file (stdout if not specified).
- #[clap(short, long)]
- out: Option<String>,
- #[clap(short, long)]
- idl: Option<String>,
- },
- }
- fn main() -> Result<()> {
- let opts = Opts::parse();
- match opts.command {
- Command::Idl { file, out } => idl(file, out),
- Command::Gen { file, out, idl } => gen(file, out, idl),
- }
- }
- fn idl(file: String, out: Option<String>) -> Result<()> {
- let file = shellexpand::tilde(&file);
- let idl = anchor_syn::parser::file::parse(&file)?;
- let idl_json = serde_json::to_string_pretty(&idl)?;
- if let Some(out) = out {
- std::fs::write(out, idl_json);
- return Ok(());
- }
- println!("{}", idl_json);
- Ok(())
- }
- fn gen(file: Option<String>, out: Option<String>, idl: Option<String>) -> Result<()> {
- // TODO. Generate clients in any language.
- Ok(())
- }
|