main.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. use anyhow::Result;
  2. use clap::Clap;
  3. use std::fs::File;
  4. use std::io::Read;
  5. #[derive(Debug, Clap)]
  6. pub struct Opts {
  7. #[clap(subcommand)]
  8. pub command: Command,
  9. }
  10. #[derive(Debug, Clap)]
  11. pub enum Command {
  12. /// Outputs an interface definition file.
  13. Idl {
  14. /// Path to the program's interface definition.
  15. #[clap(short, long)]
  16. file: String,
  17. /// Output file for the idl (stdout if not specified).
  18. #[clap(short, long)]
  19. out: Option<String>,
  20. },
  21. /// Generates a client module.
  22. Gen {
  23. /// Path to the program's interface definition.
  24. #[clap(short, long, required_unless_present("idl"))]
  25. file: Option<String>,
  26. /// Output file (stdout if not specified).
  27. #[clap(short, long)]
  28. out: Option<String>,
  29. #[clap(short, long)]
  30. idl: Option<String>,
  31. },
  32. }
  33. fn main() -> Result<()> {
  34. let opts = Opts::parse();
  35. match opts.command {
  36. Command::Idl { file, out } => idl(file, out),
  37. Command::Gen { file, out, idl } => gen(file, out, idl),
  38. }
  39. }
  40. fn idl(file: String, out: Option<String>) -> Result<()> {
  41. let file = shellexpand::tilde(&file);
  42. let idl = anchor_syn::parser::file::parse(&file)?;
  43. let idl_json = serde_json::to_string_pretty(&idl)?;
  44. if let Some(out) = out {
  45. std::fs::write(out, idl_json);
  46. return Ok(());
  47. }
  48. println!("{}", idl_json);
  49. Ok(())
  50. }
  51. fn gen(file: Option<String>, out: Option<String>, idl: Option<String>) -> Result<()> {
  52. // TODO. Generate clients in any language.
  53. Ok(())
  54. }