use clap::{Args, ValueEnum};
use std::io::{self, Write};
#[derive(Clone, ValueEnum)]
pub enum Kind {
State,
Wire,
Errors,
Postgres,
ChangesPostgres,
RecordsPostgres,
Changes,
Exchange,
Profile,
Grants,
Knowledge,
Skill,
Upkeep,
Governance,
}
#[derive(Args)]
pub struct Schema {
kind: Kind,
#[arg(long)]
schema: Option<String>,
#[arg(long, value_parser = clap::value_parser!(u8).range(1..=2))]
version: Option<u8>,
}
pub fn run(args: &Schema, out: &mut impl Write, err: &mut impl Write) -> io::Result<u8> {
if args.schema.is_some()
&& !matches!(
args.kind,
Kind::Postgres | Kind::ChangesPostgres | Kind::RecordsPostgres
)
{
return crate::cli::usage(false, "--schema applies only to postgres", out, err);
}
if args.version.is_some() && !matches!(args.kind, Kind::RecordsPostgres) {
return crate::cli::usage(
false,
"--version applies only to records-postgres",
out,
err,
);
}
let text = match args.kind {
Kind::Exchange => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/exchange-v1.schema.json"
))
.to_string(),
Kind::Changes => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/changes-v1.schema.json"
))
.to_string(),
Kind::Governance => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/governance-v1.schema.json"
))
.to_string(),
Kind::State => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/state-v1.schema.json"
))
.to_string(),
Kind::Wire => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/wire-v6.openapi.json"
))
.to_string(),
Kind::Errors => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/error-codes.json"
))
.to_string(),
Kind::Profile => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/profile-v1.schema.json"
))
.to_string(),
Kind::Grants => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/grants-v1.schema.json"
))
.to_string(),
Kind::Skill => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/skill-v1.schema.json"
))
.to_string(),
Kind::Upkeep => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/upkeep-v1.schema.json"
))
.to_string(),
Kind::Knowledge => include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/contracts/knowledge-v1.schema.json"
))
.to_string(),
Kind::Postgres | Kind::ChangesPostgres | Kind::RecordsPostgres => {
let name = args.schema.as_deref().unwrap_or("neosian");
if let Err(error) = crate::postgres::validate_schema(name) {
return crate::cli::usage(false, &error.message, out, err);
}
match args.kind {
Kind::ChangesPostgres => {
include_str!(concat!(env!("NEOSIAN_ASSETS"), "/sql/changes-postgres.sql"))
}
Kind::RecordsPostgres if args.version == Some(2) => {
include_str!(concat!(
env!("NEOSIAN_ASSETS"),
"/sql/records-postgres-v2.sql"
))
}
Kind::RecordsPostgres => {
include_str!(concat!(env!("NEOSIAN_ASSETS"), "/sql/records-postgres.sql"))
}
_ => include_str!(concat!(env!("NEOSIAN_ASSETS"), "/sql/postgres.sql")),
}
.replace("{{schema}}", &format!("\"{name}\""))
.replace("{{version}}", "4")
}
};
write!(out, "{text}")?;
Ok(0)
}