use libyay::{
encode, format_yay, parse, parse_json, parse_shon, parse_with_filename, parse_yson, scan_shon,
Format, Value,
};
use std::fs;
use std::io::{self, Read, Write};
use std::path::Path;
use std::process;
mod markdown;
mod transcode;
fn is_format_name(s: &str) -> bool {
matches!(
s,
"meh"
| "yay"
| "json"
| "yson"
| "js"
| "javascript"
| "go"
| "python"
| "py"
| "rust"
| "rs"
| "c"
| "java"
| "scheme"
| "scm"
| "yaml"
| "yml"
| "toml"
| "cbor"
| "diag"
| "md"
| "yd"
)
}
fn main() {
let args: Vec<String> = std::env::args().collect();
let mut from_format: Option<&str> = None;
let mut to_format: Option<&str> = None;
let mut write_back = false;
let mut output_file: Option<&str> = None;
let mut check_only = false;
let mut round_trip = false;
let mut input_path: Option<&str> = None;
let mut shon_start: Option<usize> = None;
let mut shon_stdin_reads = 0;
let mut stdin_named = false;
let mut i = 1;
while i < args.len() {
match args[i].as_str() {
"-h" | "--help" => {
print_help();
return;
}
"-V" | "--version" => {
println!("yay {}", env!("CARGO_PKG_VERSION"));
return;
}
"-f" | "--from" => {
i += 1;
if i >= args.len() {
eprintln!("Error: -f requires a format argument");
process::exit(1);
}
if !is_format_name(&args[i]) {
eprintln!("Error: Unknown format: {}", args[i]);
process::exit(1);
}
from_format = Some(&args[i]);
}
"-t" | "--to" => {
i += 1;
if i >= args.len() {
eprintln!("Error: -t requires a format argument");
process::exit(1);
}
if !is_format_name(&args[i]) {
eprintln!("Error: Unknown format: {}", args[i]);
process::exit(1);
}
to_format = Some(&args[i]);
}
"-w" | "--write" => {
write_back = true;
}
"-o" | "--output" => {
i += 1;
if i >= args.len() {
eprintln!("Error: --output requires an argument");
process::exit(1);
}
output_file = Some(&args[i]);
}
"--check" => {
check_only = true;
}
"-" => {
stdin_named = true;
}
"[" | "[]" | "[--]" | "-x" | "-b" | "-s" | "-S" | "-u" => {
if shon_start.is_some() {
eprintln!("Error: Multiple SHON expressions not supported");
process::exit(1);
}
if input_path.is_some() {
eprintln!("Error: Cannot combine input file with SHON expression");
process::exit(1);
}
match scan_shon(&args[i..]) {
Ok((consumed, reads)) => {
shon_start = Some(i);
shon_stdin_reads = reads;
i += consumed;
continue;
}
Err(e) => {
eprintln!("Error: {}", e);
process::exit(1);
}
}
}
arg if arg.starts_with('-') => {
eprintln!("Error: Unknown option: {}", arg);
process::exit(1);
}
_ => {
if shon_start.is_some() {
eprintln!("Error: Cannot combine input file with SHON expression");
process::exit(1);
}
if input_path.is_some() {
eprintln!("Error: Multiple input paths not supported");
process::exit(1);
}
input_path = Some(&args[i]);
}
}
i += 1;
}
if shon_stdin_reads > 1 {
eprintln!(
"Error: stdin can be read only once, but '-' is named {} times after -s or -b",
shon_stdin_reads
);
eprintln!("Hint: a file named '-' is ./-");
process::exit(1);
}
if shon_stdin_reads > 0 && stdin_named {
eprintln!("Error: stdin can be read only once, but both -s/-b - and the input - read it");
process::exit(1);
}
if shon_start.is_some() && from_format.is_some() {
eprintln!("Error: Cannot use -f/--from with SHON input");
process::exit(1);
}
let from_format = from_format.unwrap_or(if check_only { "yay" } else { "meh" });
if check_only {
if let Some(to) = to_format {
if !round_trip_pair(from_format, to) {
eprintln!(
"Error: --check -f {} -t {}: {} output is never the same bytes as {} input",
from_format, to, to, from_format
);
eprintln!(
"Hint: --check with -t compares the output with the input; it takes -f yay or meh with -t yay, and -f md or yd with -t md or yd. Without -t, --check checks validity."
);
process::exit(1);
}
round_trip = true;
}
}
if write_back && output_file.is_some() {
eprintln!("Error: --write and --output are mutually exclusive");
process::exit(1);
}
let output_format_str = to_format.unwrap_or("yay");
let output_format = parse_format(output_format_str);
if let Some(start) = shon_start {
let value = match parse_shon(&args[start..]) {
Ok((value, _)) => value,
Err(e) => {
eprintln!("Error: {}", e);
process::exit(1);
}
};
if check_only {
println!("ok");
return;
}
let exit_code = output_value(
&value,
output_format_str,
output_format,
output_file,
write_back,
None,
);
process::exit(exit_code);
}
if let Some(path) = input_path {
let path_ref = Path::new(path);
if path_ref.is_dir() {
if output_file.is_some() {
eprintln!("Error: --output cannot be used with directory input");
process::exit(1);
}
process_directory(
path,
from_format,
output_format_str,
output_format,
write_back,
check_only,
round_trip,
);
return;
}
}
let raw_bytes: Vec<u8> = match input_path {
Some(path) => match fs::read(path) {
Ok(bytes) => bytes,
Err(e) => {
eprintln!("Error reading {}: {}", path, e);
process::exit(1);
}
},
None => {
let mut buffer = Vec::new();
if let Err(e) = io::stdin().read_to_end(&mut buffer) {
eprintln!("Error reading stdin: {}", e);
process::exit(1);
}
buffer
}
};
let is_binary_input = from_format == "cbor";
let input: String = if is_binary_input {
String::new()
} else {
match String::from_utf8(raw_bytes.clone()) {
Ok(s) => s,
Err(e) => {
eprintln!("Error: input is not valid UTF-8: {}", e);
process::exit(1);
}
}
};
let input_bytes: Option<&[u8]> = if is_binary_input {
Some(&raw_bytes)
} else {
None
};
let exit_code = process_input(
&input,
input_bytes,
input_path,
from_format,
output_format_str,
output_format,
output_file,
write_back,
check_only,
round_trip,
);
process::exit(exit_code);
}
fn is_markdown(format: &str) -> bool {
matches!(format, "md" | "yd")
}
fn parse_format(s: &str) -> Format {
match s {
"yay" | "meh" | "md" | "yd" => Format::Yay,
"json" => Format::Json,
"yson" => Format::Yson,
"js" | "javascript" => Format::JavaScript,
"go" => Format::Go,
"python" | "py" => Format::Python,
"rust" | "rs" => Format::Rust,
"c" => Format::C,
"java" => Format::Java,
"scheme" | "scm" => Format::Scheme,
"yaml" | "yml" => Format::Yaml,
"toml" => Format::Toml,
"cbor" => Format::Cbor,
"diag" => Format::CborDiag,
_ => {
eprintln!("Error: Unknown format: {}", s);
process::exit(1);
}
}
}
fn format_extension(format: Format) -> &'static str {
match format {
Format::Yay => "yay",
Format::Json => "json",
Format::Yson => "yson",
Format::JavaScript => "js",
Format::Go => "go",
Format::Python => "py",
Format::Rust => "rs",
Format::C => "c",
Format::Java => "java",
Format::Scheme => "scm",
Format::Yaml => "yaml",
Format::Toml => "toml",
Format::Cbor => "cbor",
Format::CborDiag => "diag",
}
}
fn process_directory(
dir_path: &str,
from_format: &str,
output_format_str: &str,
output_format: Format,
write_back: bool,
check_only: bool,
round_trip: bool,
) {
let entries = match fs::read_dir(dir_path) {
Ok(e) => e,
Err(e) => {
eprintln!("Error reading directory {}: {}", dir_path, e);
process::exit(1);
}
};
let mut had_errors = false;
for entry in entries.flatten() {
let path = entry.path();
if path.extension().map(|e| e == "yay").unwrap_or(false) {
let path_str = path.to_string_lossy();
let input = match fs::read_to_string(&path) {
Ok(content) => content,
Err(e) => {
eprintln!("Error reading {}: {}", path_str, e);
had_errors = true;
continue;
}
};
let exit_code = process_input(
&input,
None,
Some(&path_str),
from_format,
output_format_str,
output_format,
None,
write_back,
check_only,
round_trip,
);
if exit_code != 0 {
had_errors = true;
}
}
}
process::exit(if had_errors { 1 } else { 0 });
}
#[allow(clippy::too_many_arguments)]
fn process_input(
input: &str,
input_bytes: Option<&[u8]>,
input_file: Option<&str>,
from_format: &str,
output_format_str: &str,
output_format: Format,
output_file: Option<&str>,
write_back: bool,
check_only: bool,
round_trip: bool,
) -> i32 {
let filename = input_file.map(|p| {
Path::new(p)
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_else(|| p.to_string())
});
if from_format == "md" || from_format == "yd" {
return process_markdown(
input,
input_file,
filename.as_deref(),
from_format == "yd",
output_format_str,
output_format,
output_file,
write_back,
check_only,
round_trip,
);
}
if from_format == "yay" {
match parse_with_filename(input, filename.as_deref()) {
Ok(_) => {
}
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Parse error: {}", e);
}
return 1;
}
}
}
if check_only {
if from_format == "yay" && !round_trip {
if let Some(path) = input_file {
println!("{}: ok", path);
}
return 0;
}
if from_format == "yay" || from_format == "meh" {
match format_yay(input) {
Ok(formatted) => {
if round_trip {
if let Some(e) = first_difference(input, &formatted) {
report_difference(input_file, &e);
return 1;
}
}
if let Some(path) = input_file {
println!("{}: ok", path);
}
return 0;
}
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("{}", e);
}
return 1;
}
}
}
if from_format == "json" || from_format == "yson" {
let parsed = if from_format == "json" {
parse_json(input)
} else {
parse_yson(input)
};
match parsed {
Ok(_) => {
if let Some(path) = input_file {
println!("{}: ok", path);
}
return 0;
}
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("{}", e);
}
return 1;
}
}
}
if from_format == "yaml" || from_format == "yml" {
match transcode::yaml::decode(input) {
Ok(_) => {
if let Some(path) = input_file {
println!("{}: ok", path);
}
return 0;
}
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("{}", e);
}
return 1;
}
}
}
if from_format == "toml" {
match transcode::toml::decode(input) {
Ok(_) => {
if let Some(path) = input_file {
println!("{}: ok", path);
}
return 0;
}
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("{}", e);
}
return 1;
}
}
}
if from_format == "cbor" {
let bytes = input_bytes.unwrap_or(input.as_bytes());
match transcode::cbor::decode(bytes) {
Ok(_) => {
if let Some(path) = input_file {
println!("{}: ok", path);
}
return 0;
}
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("{}", e);
}
return 1;
}
}
}
}
if (from_format == "yay" || from_format == "meh") && output_format_str == "yay" {
let output = match format_yay(input) {
Ok(s) => s,
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Format error: {}", e);
}
return 1;
}
};
write_text_output(&output, output_file, write_back, input_file, output_format);
return 0;
}
let value: Value = match from_format {
"yay" => match parse(input) {
Ok(v) => v,
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Parse error: {}", e);
}
return 1;
}
},
"meh" => {
let formatted = match format_yay(input) {
Ok(s) => s,
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Format error: {}", e);
}
return 1;
}
};
match parse(&formatted) {
Ok(v) => v,
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Parse error: {}", e);
}
return 1;
}
}
}
"json" => match parse_json(input) {
Ok(v) => v,
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Parse error: {}", e);
}
return 1;
}
},
"yson" => match parse_yson(input) {
Ok(v) => v,
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Parse error: {}", e);
}
return 1;
}
},
"yaml" | "yml" => match transcode::yaml::decode(input) {
Ok(v) => v,
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Parse error: {}", e);
}
return 1;
}
},
"toml" => match transcode::toml::decode(input) {
Ok(v) => v,
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Parse error: {}", e);
}
return 1;
}
},
"cbor" => {
let bytes = input_bytes.unwrap_or(input.as_bytes());
match transcode::cbor::decode(bytes) {
Ok(v) => v,
Err(e) => {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Parse error: {}", e);
}
return 1;
}
}
}
_ => {
eprintln!("Error: Unknown input format: {}", from_format);
return 1;
}
};
if check_only {
if let Some(path) = input_file {
println!("{}: ok", path);
}
return 0;
}
if is_markdown(output_format_str) {
return output_markdown(&value, output_file, write_back, input_file);
}
if output_format == Format::Json {
if let Some(reason) = value.json_incompatibility() {
eprintln!(
"Error: Cannot convert to JSON because the document contains {}.",
reason
);
eprintln!("Hint: Try using YSON format instead (-t yson), which supports these types.");
return 1;
}
}
match output_format {
Format::Yaml => match transcode::yaml::encode(&value) {
Ok(output) => {
write_text_output(&output, output_file, write_back, input_file, output_format);
}
Err(e) => {
eprintln!("Error: Cannot convert to YAML: {}", e);
return 1;
}
},
Format::Toml => match transcode::toml::encode(&value) {
Ok(output) => {
write_text_output(&output, output_file, write_back, input_file, output_format);
}
Err(e) => {
eprintln!("Error: Cannot convert to TOML: {}", e);
return 1;
}
},
Format::Cbor => match transcode::cbor::encode(&value) {
Ok(bytes) => {
write_binary_output(&bytes, output_file, write_back, input_file, output_format);
}
Err(e) => {
eprintln!("Error: Cannot convert to CBOR: {}", e);
return 1;
}
},
Format::CborDiag => {
match transcode::cbor::encode(&value) {
Ok(bytes) => match transcode::cbor::diagnostic(&bytes) {
Ok(output) => {
write_text_output(
&output,
output_file,
write_back,
input_file,
output_format,
);
}
Err(e) => {
eprintln!("Error: Cannot render CBOR diagnostic notation: {}", e);
return 1;
}
},
Err(e) => {
eprintln!("Error: Cannot convert to CBOR: {}", e);
return 1;
}
}
}
_ => {
let output = encode(&value, output_format);
write_text_output(&output, output_file, write_back, input_file, output_format);
}
}
0
}
fn output_value(
value: &Value,
output_format_str: &str,
output_format: Format,
output_file: Option<&str>,
write_back: bool,
input_file: Option<&str>,
) -> i32 {
if is_markdown(output_format_str) {
return output_markdown(value, output_file, write_back, input_file);
}
if output_format_str == "yay" {
let output = encode(value, Format::Yay);
write_text_output(&output, output_file, write_back, input_file, output_format);
return 0;
}
if output_format == Format::Json {
if let Some(reason) = value.json_incompatibility() {
eprintln!(
"Error: Cannot convert to JSON because the document contains {}.",
reason
);
eprintln!("Hint: Try using YSON format instead (-t yson), which supports these types.");
return 1;
}
}
match output_format {
Format::Yaml => match transcode::yaml::encode(value) {
Ok(output) => {
write_text_output(&output, output_file, write_back, input_file, output_format);
}
Err(e) => {
eprintln!("Error: Cannot convert to YAML: {}", e);
return 1;
}
},
Format::Toml => match transcode::toml::encode(value) {
Ok(output) => {
write_text_output(&output, output_file, write_back, input_file, output_format);
}
Err(e) => {
eprintln!("Error: Cannot convert to TOML: {}", e);
return 1;
}
},
Format::Cbor => match transcode::cbor::encode(value) {
Ok(bytes) => {
write_binary_output(&bytes, output_file, write_back, input_file, output_format);
}
Err(e) => {
eprintln!("Error: Cannot convert to CBOR: {}", e);
return 1;
}
},
Format::CborDiag => match transcode::cbor::encode(value) {
Ok(bytes) => match transcode::cbor::diagnostic(&bytes) {
Ok(output) => {
write_text_output(&output, output_file, write_back, input_file, output_format);
}
Err(e) => {
eprintln!("Error: Cannot render CBOR diagnostic notation: {}", e);
return 1;
}
},
Err(e) => {
eprintln!("Error: Cannot convert to CBOR: {}", e);
return 1;
}
},
_ => {
let output = encode(value, output_format);
write_text_output(&output, output_file, write_back, input_file, output_format);
}
}
0
}
#[allow(clippy::too_many_arguments)]
fn process_markdown(
input: &str,
input_file: Option<&str>,
filename: Option<&str>,
strict: bool,
output_format_str: &str,
output_format: Format,
output_file: Option<&str>,
write_back: bool,
check_only: bool,
round_trip: bool,
) -> i32 {
let report = |e: &dyn std::fmt::Display| {
if let Some(path) = input_file {
eprintln!("{}: {}", path, e);
} else {
eprintln!("Parse error: {}", e);
}
};
let read = match markdown::read(input, filename, strict) {
Ok(r) => r,
Err(e) => {
report(&e);
return 1;
}
};
if check_only {
if round_trip {
let out = markdown::normalize(&read);
if let Some(e) = first_difference(input, &out) {
report_difference(input_file, &e);
return 1;
}
}
if let Some(path) = input_file {
println!("{}: ok", path);
}
return 0;
}
if is_markdown(output_format_str) {
let out = markdown::normalize(&read);
return write_markdown(&out, output_file, write_back, input_file);
}
output_value(
&read.value,
output_format_str,
output_format,
output_file,
write_back,
input_file,
)
}
fn output_markdown(
value: &Value,
output_file: Option<&str>,
write_back: bool,
input_file: Option<&str>,
) -> i32 {
match markdown::from_value(value) {
Ok(out) => write_markdown(&out, output_file, write_back, input_file),
Err(e) => {
eprintln!("Error: Cannot convert to markdown: {}", e);
1
}
}
}
fn write_markdown(
output: &str,
output_file: Option<&str>,
write_back: bool,
input_file: Option<&str>,
) -> i32 {
let target = if let Some(path) = output_file {
Some(path.to_string())
} else if write_back {
match input_file {
Some(p) => Some(
Path::new(p)
.with_extension("md")
.to_string_lossy()
.to_string(),
),
None => {
eprintln!("Error: --write requires an input file");
return 1;
}
}
} else {
None
};
match target {
Some(path) => {
if let Err(e) = write_file(Path::new(&path), output.as_bytes()) {
eprintln!("Error writing {}: {}", path, e);
return 1;
}
}
None => {
print!("{}", output);
}
}
0
}
fn write_file(path: &Path, contents: &[u8]) -> io::Result<()> {
let target = match fs::canonicalize(path) {
Ok(real) => real,
Err(e) if e.kind() == io::ErrorKind::NotFound => path.to_path_buf(),
Err(e) => return Err(e),
};
if let Ok(meta) = fs::metadata(&target) {
if !meta.is_file() {
return fs::write(&target, contents);
}
}
let dir = match target.parent() {
Some(d) if !d.as_os_str().is_empty() => d.to_path_buf(),
_ => Path::new(".").to_path_buf(),
};
let name = target
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_default();
let temp = dir.join(format!(".{}.yay-{}.tmp", name, process::id()));
let result = (|| {
let mut file = fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&temp)?;
file.write_all(contents)?;
if let Ok(meta) = fs::metadata(&target) {
file.set_permissions(meta.permissions())?;
}
file.sync_all()?;
drop(file);
fs::rename(&temp, &target)
})();
if result.is_err() {
let _ = fs::remove_file(&temp);
}
result
}
fn write_text_output(
output: &str,
output_file: Option<&str>,
write_back: bool,
input_file: Option<&str>,
format: Format,
) {
if let Some(path) = output_file {
if let Err(e) = write_file(Path::new(path), output.as_bytes()) {
eprintln!("Error writing {}: {}", path, e);
process::exit(1);
}
} else if write_back {
if let Some(input_path) = input_file {
let ext = format_extension(format);
let output_path = Path::new(input_path).with_extension(ext);
if let Err(e) = write_file(&output_path, output.as_bytes()) {
eprintln!("Error writing {}: {}", output_path.display(), e);
process::exit(1);
}
} else {
eprintln!("Error: --write requires an input file");
process::exit(1);
}
} else {
print!("{}", output);
if !output.ends_with('\n') {
println!();
}
}
}
fn write_binary_output(
output: &[u8],
output_file: Option<&str>,
write_back: bool,
input_file: Option<&str>,
format: Format,
) {
if let Some(path) = output_file {
if let Err(e) = write_file(Path::new(path), output) {
eprintln!("Error writing {}: {}", path, e);
process::exit(1);
}
} else if write_back {
if let Some(input_path) = input_file {
let ext = format_extension(format);
let output_path = Path::new(input_path).with_extension(ext);
if let Err(e) = write_file(&output_path, output) {
eprintln!("Error writing {}: {}", output_path.display(), e);
process::exit(1);
}
} else {
eprintln!("Error: --write requires an input file");
process::exit(1);
}
} else {
let stdout = io::stdout();
let mut handle = stdout.lock();
if let Err(e) = handle.write_all(output) {
eprintln!("Error writing to stdout: {}", e);
process::exit(1);
}
}
}
fn round_trip_pair(from: &str, to: &str) -> bool {
match from {
"yay" | "meh" => to == "yay",
"md" | "yd" => is_markdown(to),
_ => false,
}
}
fn first_difference(actual: &str, formatted: &str) -> Option<String> {
if actual == formatted {
return None;
}
let mut a = actual.split_inclusive('\n');
let mut f = formatted.split_inclusive('\n');
let mut n = 0;
loop {
n += 1;
match (a.next(), f.next()) {
(Some(x), Some(y)) if x == y => continue,
(Some(x), Some(y)) if !x.ends_with('\n') && y == format!("{}\n", x) => {
return Some(format!(
"not formatted at line {}: the last line must end with a newline",
n
))
}
(Some(_), Some("\n")) => {
return Some(format!(
"not formatted at line {}: expected a blank line (after a block string)",
n
))
}
(Some("\n"), _) => {
return Some(format!(
"not formatted at line {}: unexpected blank line",
n
))
}
(Some(x), Some(y)) => {
return Some(format!(
"not formatted at line {}: expected {:?}, found {:?}",
n,
y.trim_end_matches('\n'),
x.trim_end_matches('\n')
))
}
(Some(_), None) => {
return Some(format!(
"not formatted at line {}: expected the end of the document",
n
))
}
(None, Some(y)) => {
return Some(format!(
"not formatted at line {}: expected {:?}",
n,
y.trim_end_matches('\n')
))
}
(None, None) => {
return Some(format!("not formatted at line {}", n - 1));
}
}
}
}
fn report_difference(input_file: Option<&str>, e: &str) {
match input_file {
Some(path) => eprintln!("{}: {}", path, e),
None => eprintln!("{}", e),
}
}
fn print_help() {
println!(
"yay - YAY command-line tool
USAGE:
yay [OPTIONS] [FILE|DIR]
ARGS:
[FILE|DIR] Input file or directory (reads from stdin if not provided)
When a directory is given, processes all .yay files in it
OPTIONS:
-f, --from <FORMAT> Input format [default: meh, or yay when --check]
Supported: meh, yay, json, yson, yaml, toml, cbor,
md (Mehdown: markdown, front matter read as MEH),
yd (Yaydown: markdown, strict YAY front matter)
'meh' (default) accepts loose formatting and reformats
it as YAY. 'yay' enforces strict YAY syntax
before transformation.
When --check is used, the default flips to 'yay'
(strict). Use --from meh to check lenient syntax.
-t, --to <FORMAT> Output format
Supported: yay, json, yson, js, go, python, rust, c,
java, scheme, yaml, toml, cbor, diag, md, yd
-w, --write Write output to file with inferred extension
-o, --output <FILE> Write output to specified file (not valid with directory input)
--check What it checks depends on whether -t is given.
Without -t: check that input is valid (exit 0 if
valid, 1 if invalid). Defaults to strict YAY input;
use --from meh for lenient.
With -t: check that input is valid and that writing
it as -t would leave it unchanged, byte for byte; on
a difference, report the first line that differs.
Takes -f yay or meh with -t yay, and -f md or yd
with -t md or yd; refuses any other pair.
-h, --help Print help
-V, --version Print version
EXAMPLES:
# Reformat a MEH file as YAY (default behavior)
yay config.meh
# Strictly validate a YAY file (--check defaults to strict)
yay --check config.yay
# Validate with lenient parsing (meh)
yay --check --from meh config.yay
# Validate all YAY files in a directory strictly
yay --check ./configs/
# Check that a YAY file is valid and in the formatter's form:
# that -t yay would write it unchanged
yay --check -t yay config.yay
# The same for a Yaydown document
yay --check -f yd -t yd notes.md
# Convert YAY to JSON (lenient input)
yay -t json config.yay
# Convert YAY to JSON (strict input)
yay -f yay -t json config.yay
# Convert JSON to YAY
yay -f json -t yay data.json
# Convert YAY to YAML
yay -t yaml config.yay
# Convert YAML to YAY
yay -f yaml -t yay config.yaml
# Convert YAY to TOML
yay -t toml config.yay
# Convert TOML to YAY
yay -f toml -t yay config.toml
# Convert YAY to CBOR (binary)
yay -t cbor config.yay -o config.cbor
# Convert CBOR to YAY
yay -f cbor -t yay config.cbor
# View CBOR in diagnostic notation (RFC 8949 ยง8)
yay -f cbor -t diag config.cbor
# Generate Go code from YAY
yay -t go config.yay > config.go
# Convert all YAY files in a directory to JSON
yay -t json -w ./configs/
# Convert YAY to YSON (JSON with YAY extensions)
yay -t yson config.yay -o config.yson
# SHON: construct data from command-line arguments
yay [ --name hello --count 42 ]
yay -t json [ --x 1.0 --y 2.0 ]
yay -t yson -x cafe
yay -b image.png -o image.yay
yay -s message.txt
echo hi | yay [ --content -s - ] -t md # -s - and -b - read stdin
"
);
}