52 lines
2.0 KiB
Rust
52 lines
2.0 KiB
Rust
//! One-file command-line harness for inspecting the actual lowering result.
|
|
use oa_core::{language, source_project::SourceBundle};
|
|
use std::{collections::BTreeMap, env, fs, process::ExitCode};
|
|
|
|
fn main() -> ExitCode {
|
|
let args: Vec<_> = env::args().skip(1).collect();
|
|
let rename = args.len() == 4 && args[0] == "--rename";
|
|
if args.len() != 1 && !rename {
|
|
eprintln!(
|
|
"Usage: source-authoring-experiment B_SOURCE_FILE\n source-authoring-experiment --rename B_SOURCE_FILE ALIAS NAME\nIncludes require the library's explicit SourceBundle API."
|
|
);
|
|
return ExitCode::FAILURE;
|
|
}
|
|
let result = fs::read_to_string(&args[usize::from(rename)])
|
|
.map_err(|error| error.to_string())
|
|
.and_then(|text| {
|
|
let bundle = SourceBundle {
|
|
entry: "project.arcadia".into(),
|
|
files: BTreeMap::from([("project.arcadia".into(), text)]),
|
|
};
|
|
let project = source_authoring_experiment::compile(&bundle)?;
|
|
if rename {
|
|
let id = project
|
|
.identities()
|
|
.into_iter()
|
|
.find(|(_, alias)| *alias == args[2])
|
|
.map(|(id, _)| id)
|
|
.ok_or("Unknown declaration alias")?;
|
|
let candidate = oa_core::execute_batch(
|
|
&project,
|
|
&[oa_core::Command::Rename(id, args[3].clone())],
|
|
)
|
|
.map_err(|error| error.to_string())?;
|
|
let result =
|
|
source_authoring_experiment::writeback::writeback(&bundle, &candidate)?;
|
|
Ok(result.files[&result.entry].clone())
|
|
} else {
|
|
Ok(language::format(&project))
|
|
}
|
|
});
|
|
match result {
|
|
Ok(text) => {
|
|
print!("{text}");
|
|
ExitCode::SUCCESS
|
|
}
|
|
Err(error) => {
|
|
eprintln!("{error}");
|
|
ExitCode::FAILURE
|
|
}
|
|
}
|
|
}
|