use std::path::{Path, PathBuf};
use std::time::Duration;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Architecture {
#[default]
#[serde(rename = "x86_64", alias = "x86-64", alias = "amd64")]
X86_64,
#[serde(rename = "aarch64", alias = "arm64")]
Aarch64,
#[serde(rename = "riscv64")]
RiscV64,
}
impl Architecture {
pub const fn is_supported(self) -> bool {
matches!(self, Architecture::X86_64)
}
pub const fn id(self) -> &'static str {
match self {
Architecture::X86_64 => "x86_64",
Architecture::Aarch64 => "aarch64",
Architecture::RiscV64 => "riscv64",
}
}
pub const fn object_format(self) -> &'static str {
match self {
Architecture::X86_64 => "elf64",
Architecture::Aarch64 | Architecture::RiscV64 => "elf64",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Syntax {
#[default]
Nasm,
Gas,
}
impl Syntax {
pub const fn is_supported(self) -> bool {
matches!(self, Syntax::Nasm)
}
pub const fn id(self) -> &'static str {
match self {
Syntax::Nasm => "nasm",
Syntax::Gas => "gas",
}
}
pub const fn default_assembler(self) -> &'static str {
match self {
Syntax::Nasm => "nasm",
Syntax::Gas => "as",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ProjectSection {
pub name: String,
pub entry: PathBuf,
pub architecture: Architecture,
pub syntax: Syntax,
pub sources: Vec<PathBuf>,
pub include_directories: Vec<PathBuf>,
}
impl Default for ProjectSection {
fn default() -> Self {
Self {
name: "ratasm-project".to_owned(),
entry: PathBuf::from("src/main.asm"),
architecture: Architecture::default(),
syntax: Syntax::default(),
sources: Vec::new(),
include_directories: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct BuildSection {
pub assembler: String,
pub assembler_args: Vec<String>,
pub linker: String,
pub linker_args: Vec<String>,
pub objects: Vec<PathBuf>,
pub output_directory: PathBuf,
pub executable: Option<PathBuf>,
pub make: Option<bool>,
}
impl Default for BuildSection {
fn default() -> Self {
Self {
assembler: "nasm".to_owned(),
assembler_args: vec!["-f".to_owned(), "elf64".to_owned()],
linker: "ld".to_owned(),
linker_args: Vec::new(),
objects: Vec::new(),
output_directory: PathBuf::from("build"),
executable: None,
make: None,
}
}
}
impl BuildSection {
pub fn requests_debug_info(&self) -> bool {
self.assembler_args
.iter()
.any(|arg| arg == "-g" || arg.starts_with("-F") || arg.starts_with("-gdwarf"))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct RunSection {
pub args: Vec<String>,
pub timeout_ms: u64,
pub working_directory: Option<PathBuf>,
pub stdin: Option<String>,
}
impl Default for RunSection {
fn default() -> Self {
Self {
args: Vec::new(),
timeout_ms: 5_000,
working_directory: None,
stdin: None,
}
}
}
impl RunSection {
pub fn timeout(&self) -> Option<Duration> {
if self.timeout_ms == 0 {
None
} else {
Some(Duration::from_millis(self.timeout_ms))
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ProjectConfig {
pub project: ProjectSection,
pub build: BuildSection,
pub run: RunSection,
}
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ConfigError {
#[error("invalid project file: {message}")]
Parse {
message: String,
},
#[error("architecture '{0}' is recognised but not supported yet; only x86_64 is implemented")]
UnsupportedArchitecture(String),
#[error("syntax '{0}' is recognised but not supported yet; only nasm is implemented")]
UnsupportedSyntax(String),
#[error("{field} must not be empty")]
EmptyField {
field: &'static str,
},
#[error("{field} must stay inside the project directory, but '{path}' does not")]
EscapesRoot {
field: &'static str,
path: PathBuf,
},
}
impl ProjectConfig {
pub fn from_toml(text: &str) -> Result<Self, ConfigError> {
toml::from_str(text).map_err(|error| ConfigError::Parse {
message: error.message().to_owned(),
})
}
pub fn to_toml(&self) -> Result<String, ConfigError> {
toml::to_string_pretty(self).map_err(|error| ConfigError::Parse {
message: error.to_string(),
})
}
pub fn validate(&self) -> Result<(), ConfigError> {
if !self.project.architecture.is_supported() {
return Err(ConfigError::UnsupportedArchitecture(
self.project.architecture.id().to_owned(),
));
}
if !self.project.syntax.is_supported() {
return Err(ConfigError::UnsupportedSyntax(
self.project.syntax.id().to_owned(),
));
}
if self.project.name.trim().is_empty() {
return Err(ConfigError::EmptyField {
field: "project.name",
});
}
if self.build.assembler.trim().is_empty() {
return Err(ConfigError::EmptyField {
field: "build.assembler",
});
}
if self.build.linker.trim().is_empty() {
return Err(ConfigError::EmptyField {
field: "build.linker",
});
}
if self.project.entry.as_os_str().is_empty() {
return Err(ConfigError::EmptyField {
field: "project.entry",
});
}
check_contained("project.entry", &self.project.entry)?;
for source in &self.project.sources {
check_contained("project.sources", source)?;
}
check_contained("build.output_directory", &self.build.output_directory)?;
if let Some(executable) = &self.build.executable {
check_contained("build.executable", executable)?;
}
Ok(())
}
pub fn all_sources(&self) -> Vec<PathBuf> {
let mut sources = vec![self.project.entry.clone()];
for source in &self.project.sources {
if !sources.contains(source) {
sources.push(source.clone());
}
}
sources
}
}
fn check_contained(field: &'static str, path: &Path) -> Result<(), ConfigError> {
use std::path::Component;
let escapes = path.is_absolute()
|| path
.components()
.any(|component| matches!(component, Component::ParentDir));
if escapes {
return Err(ConfigError::EscapesRoot {
field,
path: path.to_path_buf(),
});
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE: &str = r#"
[project]
name = "hello"
entry = "src/main.asm"
architecture = "x86_64"
syntax = "nasm"
[build]
assembler = "nasm"
assembler_args = ["-f", "elf64"]
linker = "ld"
[run]
args = []
timeout_ms = 5000
"#;
#[test]
fn the_documented_example_parses() {
let config = ProjectConfig::from_toml(EXAMPLE).expect("parse");
assert_eq!(config.project.name, "hello");
assert_eq!(config.project.entry, PathBuf::from("src/main.asm"));
assert_eq!(config.project.architecture, Architecture::X86_64);
assert_eq!(config.project.syntax, Syntax::Nasm);
assert_eq!(config.build.assembler, "nasm");
assert_eq!(config.build.assembler_args, ["-f", "elf64"]);
assert_eq!(config.build.linker, "ld");
assert_eq!(config.run.timeout_ms, 5000);
config.validate().expect("valid");
}
#[test]
fn an_empty_file_yields_working_defaults() {
let config = ProjectConfig::from_toml("").expect("parse");
assert_eq!(config.build.assembler, "nasm");
assert_eq!(config.build.assembler_args, ["-f", "elf64"]);
assert_eq!(config.build.linker, "ld");
assert_eq!(config.project.entry, PathBuf::from("src/main.asm"));
config.validate().expect("defaults must be valid");
}
#[test]
fn a_partial_section_keeps_the_other_defaults() {
let config = ProjectConfig::from_toml("[build]\nlinker = \"ld.lld\"\n").expect("parse");
assert_eq!(config.build.linker, "ld.lld");
assert_eq!(
config.build.assembler, "nasm",
"untouched field keeps default"
);
}
#[test]
fn a_misspelled_field_is_rejected_rather_than_ignored() {
let error =
ProjectConfig::from_toml("[build]\nassember = \"nasm\"\n").expect_err("must fail");
assert!(matches!(error, ConfigError::Parse { .. }));
}
#[test]
fn malformed_toml_is_reported_not_panicked() {
let error = ProjectConfig::from_toml("[project\nname =").expect_err("must fail");
assert!(matches!(error, ConfigError::Parse { .. }));
}
#[test]
fn architecture_aliases_are_accepted() {
for spelling in ["x86_64", "x86-64", "amd64"] {
let text = format!("[project]\narchitecture = \"{spelling}\"\n");
let config = ProjectConfig::from_toml(&text)
.unwrap_or_else(|error| panic!("{spelling}: {error}"));
assert_eq!(config.project.architecture, Architecture::X86_64);
}
}
#[test]
fn an_unimplemented_architecture_gives_a_clear_message() {
let config =
ProjectConfig::from_toml("[project]\narchitecture = \"aarch64\"\n").expect("parse");
let error = config.validate().expect_err("must not validate");
assert!(matches!(error, ConfigError::UnsupportedArchitecture(_)));
assert!(error.to_string().contains("not supported yet"));
}
#[test]
fn an_unimplemented_syntax_gives_a_clear_message() {
let config = ProjectConfig::from_toml("[project]\nsyntax = \"gas\"\n").expect("parse");
let error = config.validate().expect_err("must not validate");
assert!(matches!(error, ConfigError::UnsupportedSyntax(_)));
}
#[test]
fn an_unknown_architecture_fails_to_parse() {
assert!(ProjectConfig::from_toml("[project]\narchitecture = \"pdp11\"\n").is_err());
}
#[test]
fn empty_required_fields_are_rejected() {
for (text, field) in [
("[project]\nname = \" \"\n", "project.name"),
("[build]\nassembler = \"\"\n", "build.assembler"),
("[build]\nlinker = \"\"\n", "build.linker"),
("[project]\nentry = \"\"\n", "project.entry"),
] {
let config = ProjectConfig::from_toml(text).expect("parse");
let error = config.validate().expect_err("must not validate");
assert_eq!(error, ConfigError::EmptyField { field });
}
}
#[test]
fn a_path_escaping_the_project_root_is_rejected() {
for text in [
"[project]\nentry = \"../../etc/passwd\"\n",
"[project]\nentry = \"/etc/passwd\"\n",
"[build]\noutput_directory = \"../elsewhere\"\n",
"[build]\nexecutable = \"/usr/bin/evil\"\n",
] {
let config = ProjectConfig::from_toml(text).expect("parse");
let error = config.validate().expect_err("must reject {text}");
assert!(
matches!(error, ConfigError::EscapesRoot { .. }),
"wrong error for {text}: {error}"
);
}
}
#[test]
fn a_nested_relative_path_is_allowed() {
let config =
ProjectConfig::from_toml("[project]\nentry = \"src/boot/main.asm\"\n").expect("parse");
config.validate().expect("nested paths are fine");
}
#[test]
fn a_zero_timeout_means_no_limit() {
let config = ProjectConfig::from_toml("[run]\ntimeout_ms = 0\n").expect("parse");
assert_eq!(config.run.timeout(), None);
}
#[test]
fn a_positive_timeout_converts_to_a_duration() {
let config = ProjectConfig::from_toml("[run]\ntimeout_ms = 250\n").expect("parse");
assert_eq!(config.run.timeout(), Some(Duration::from_millis(250)));
}
#[test]
fn sources_start_with_the_entry_point_and_are_deduplicated() {
let config = ProjectConfig::from_toml(
"[project]\nentry = \"src/main.asm\"\nsources = [\"src/util.asm\", \"src/main.asm\"]\n",
)
.expect("parse");
assert_eq!(
config.all_sources(),
vec![PathBuf::from("src/main.asm"), PathBuf::from("src/util.asm")]
);
}
#[test]
fn configuration_round_trips_through_toml() {
let original = ProjectConfig::from_toml(EXAMPLE).expect("parse");
let rendered = original.to_toml().expect("serialise");
let reparsed = ProjectConfig::from_toml(&rendered).expect("reparse");
assert_eq!(original, reparsed);
}
#[test]
fn debug_flags_are_detected_so_they_are_not_added_twice() {
let mut build = BuildSection::default();
assert!(!build.requests_debug_info());
build.assembler_args.push("-g".to_owned());
assert!(build.requests_debug_info());
let mut build = BuildSection::default();
build.assembler_args.push("-gdwarf".to_owned());
assert!(build.requests_debug_info());
}
#[test]
fn architecture_and_syntax_identifiers_are_stable() {
assert_eq!(Architecture::X86_64.id(), "x86_64");
assert_eq!(Architecture::X86_64.object_format(), "elf64");
assert_eq!(Syntax::Nasm.id(), "nasm");
assert_eq!(Syntax::Nasm.default_assembler(), "nasm");
assert_eq!(Syntax::Gas.default_assembler(), "as");
}
}