use std::cmp::min;
use std::collections::HashSet;
use std::io::{BufReader, BufWriter, Cursor, Error, Read, Seek, SeekFrom, Write};
use std::iter::Sum;
use std::path::PathBuf;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use crate::error;
use crate::error::{ConfigParseErrorExt, ErrorExt, printable};
use crate::eval::{EvalValue, eval, exec};
use crate::io::{ReadExt, WriteExt, compressed_int_len};
use crate::preprocess::{IncludeFolders, MAX_NESTING_DEPTH, nesting_error, preprocess_in};
use crate::warning::{Warning, WarningSink};
fn text_name(name: &str, what: &str) -> Result<(), Error> {
if name.chars().any(char::is_control) {
return Err(error!(
"{what} name \"{}\" cannot be written as config text.",
printable(name)
));
}
Ok(())
}
#[derive(Default)]
pub(crate) struct EvalCtx {
vars: std::collections::HashMap<String, EvalValue>,
error: Option<Error>,
}
impl From<EvalValue> for ConfigEntry {
fn from(v: EvalValue) -> ConfigEntry {
match v {
EvalValue::Str(s) => ConfigEntry::StringEntry(s),
EvalValue::Number(n)
if matches!(n.fract().classify(), std::num::FpCategory::Zero)
&& n.abs() < 2_147_483_648.0 =>
{
#[expect(
clippy::cast_possible_truncation,
reason = "guarded by fract() == 0 and abs() < 2^31"
)]
{
ConfigEntry::IntEntry(n as i32)
}
}
EvalValue::Number(n) => ConfigEntry::FloatEntry(n),
}
}
}
impl From<EvalValue> for ConfigArrayElement {
fn from(v: EvalValue) -> ConfigArrayElement {
match v {
EvalValue::Str(s) => ConfigArrayElement::StringElement(s),
EvalValue::Number(n)
if matches!(n.fract().classify(), std::num::FpCategory::Zero)
&& n.abs() < 2_147_483_648.0 =>
{
#[expect(
clippy::cast_possible_truncation,
reason = "guarded by fract() == 0 and abs() < 2^31"
)]
{
ConfigArrayElement::IntElement(n as i32)
}
}
EvalValue::Number(n) => ConfigArrayElement::FloatElement(n),
}
}
}
pub(crate) struct ConfigWarnings(pub(crate) Vec<(usize, String, Option<&'static str>)>);
impl ConfigWarnings {
fn push(&mut self, warning: (usize, String, Option<&'static str>)) {
self.0.push(warning);
}
}
peg::parser! {
pub(crate) grammar config_grammar(warnings: &mut ConfigWarnings, ctx: &mut EvalCtx) for str {
rule whitespace() = quiet!{ [' ' | '\r' | '\n' | '\t']+ }
rule float() -> f32 = f:$((['-' | '+'])? ['0'..='9']* "." ['0'..='9']+) {?
f.parse::<f32>()
.map_err(|e| { ctx.error.get_or_insert(error!("Invalid float \"{f}\": {e}")); "a valid float" })
}
rule integer() -> i32 = i:$((['-' | '+'])? (("0x" ['0'..='9' | 'a'..='f' | 'A'..='F']+) / ['0'..='9']+)) {?
if i.contains("0x") {
i32::from_str_radix(&i.replace("0x", ""), 16)
.map_err(|e| { ctx.error.get_or_insert(error!("Invalid integer \"{i}\": {e}")); "a valid integer" })
} else {
i.parse::<i32>()
.map_err(|e| { ctx.error.get_or_insert(error!("Invalid integer \"{i}\": {e}")); "a valid integer" })
}
}
rule doublequoted_string() -> String = "\"" s:$(("\"\"" / !['"'] [_])*) "\"" {
s.to_string().replace("\"\"", "\"")
}
rule singlequoted_string() -> String = "'" s:$(("''" / !['\''] [_])*) "'" {
s.to_string().replace("''", "'")
}
rule unquoted_string() -> String = pos:position!() s:$((![';' | '}' | '\r' | '\n'] [_])*) {
let result = s.to_string().trim().to_string();
if !result.is_empty() {
warnings.push((pos, format!("String value \"{}\" is not quoted properly.", printable(&result)), Some("unquoted-string")));
}
result
}
rule unquoted_string_array() -> String = pos:position!() s:$( (![',' | '}' | ' ' | '\t' | '\r' | '\n'] [_]) (![',' | '}' | '\r' | '\n'] [_])* ) {
let result = s.to_string().trim().to_string();
warnings.push((pos, format!("String array element \"{}\" is not quoted properly.", printable(&result)), Some("unquoted-string")));
result
}
rule nesting_ok(d: usize) = {?
if d >= MAX_NESTING_DEPTH {
ctx.error.get_or_insert(nesting_error());
Err("nesting within the limit")
} else {
Ok(())
}
}
rule eval_body(d: usize) = ("(" nesting_ok(d) eval_body(d + 1) ")" / "\"" (!"\"" [_])* "\"" / !['(' | ')'] [_])*
rule eval_value(d: usize) -> EvalValue = "__EVAL" whitespace()? "(" b:$(eval_body(d)) ")" {?
match eval(b, &mut ctx.vars) {
Ok(v) => Ok(v),
Err(e) => { ctx.error.get_or_insert(e); Err("a supported __EVAL expression") }
}
}
rule exec_entry(d: usize) = "__EXEC" whitespace()? "(" b:$(eval_body(d)) ")" whitespace()? ";" {?
match exec(b, &mut ctx.vars) {
Ok(()) => Ok(()),
Err(e) => { ctx.error.get_or_insert(e); Err("a supported __EXEC statement") }
}
}
rule string() -> String = doublequoted_string() / singlequoted_string()
rule array_element(d: usize) -> ConfigArrayElement =
f:float() &(whitespace()? [',' | '}']) { ConfigArrayElement::FloatElement(f) } /
i:integer() &(whitespace()? [',' | '}']) { ConfigArrayElement::IntElement(i) } /
a:array(d) &(whitespace()? [',' | '}']) { ConfigArrayElement::ArrayElement(a) } /
s:string() &(whitespace()? [',' | '}']) { ConfigArrayElement::StringElement(s) } /
v:eval_value(d) &(whitespace()? [',' | '}']) { v.into() } /
s:unquoted_string_array() &(whitespace()? [',' | '}']) { ConfigArrayElement::StringElement(s) }
rule array_elements(d: usize) -> Vec<ConfigArrayElement> = array_element(d) ** (whitespace()? "," whitespace()?)
rule array(d: usize) -> ConfigArray = "{" nesting_ok(d) whitespace()? elems:array_elements(d + 1) whitespace()? ","? whitespace()? "}" {
ConfigArray {
is_expansion: false,
elements: elems
}
}
rule var(d: usize) -> ConfigEntry =
f:float() { ConfigEntry::FloatEntry(f) } /
i:integer() { ConfigEntry::IntEntry(i) } /
s:string() { ConfigEntry::StringEntry(s) } /
v:eval_value(d) { v.into() }
rule var_entry(d: usize) -> (String, ConfigEntry) = n:name() whitespace()? "=" whitespace()? ce:var(d) {
(n, ce)
}
rule unquoted_string_entry() -> (String, ConfigEntry) = pos:position!() n:name() whitespace()? "=" whitespace()? s:unquoted_string() {
if s.is_empty() {
warnings.push((pos, format!("Entry \"{}\" has no value; it is stored as an empty string.", printable(&n)), Some("empty-value")));
}
(n, ConfigEntry::StringEntry(s))
}
rule array_entry(d: usize) -> (String, ConfigEntry) = n:name() whitespace()? "[" whitespace()? "]" whitespace()? "=" whitespace()? a:array(d) {
(n, ConfigEntry::ArrayEntry(a))
}
rule array_expansion_entry(d: usize) -> (String, ConfigEntry) = n:name() whitespace()? "[" whitespace()? "]" whitespace()? "+=" whitespace()? a:array(d) {
(n, ConfigEntry::ArrayEntry(ConfigArray {
is_expansion: true,
..a
}))
}
rule entry(d: usize) -> (String, ConfigEntry) =
e:(class(d) / array_entry(d) / array_expansion_entry(d) / var_entry(d)) whitespace()? (";" / &"}") { e } /
e:unquoted_string_entry() (";" / &"}" / &['\r' | '\n']) { e }
rule entry_or_exec(d: usize) -> Option<(String, ConfigEntry)> = exec_entry(d) { None } / e:entry(d) { Some(e) }
rule entries(d: usize) -> Vec<(String, ConfigEntry)> = v:(entry_or_exec(d) ** (whitespace()?)) { v.into_iter().flatten().collect() }
rule name() -> String = n:$(['a'..='z' | 'A'..='Z' | '0'..='9' | '_']+) {
n.to_string()
}
rule parent() -> String = whitespace()? ":" whitespace()? n:name() {
n
}
rule regular_class(d: usize) -> (String, ConfigEntry) = "class" whitespace()+ n:name() p:parent()? whitespace()? "{" nesting_ok(d) whitespace()? e:entries(d + 1) whitespace()? "}" {
let parent = p.unwrap_or_default();
(n, ConfigEntry::ClassEntry(ConfigClass {
parent,
is_external: false,
is_deletion: false,
entries: Some(e)
}))
}
rule external_class() -> (String, ConfigEntry) = "class" whitespace()+ n:name() {
(n, ConfigEntry::ClassEntry(ConfigClass {
parent: String::new(),
is_external: true,
is_deletion: false,
entries: None
}))
}
rule deleted_class() -> (String, ConfigEntry) = "delete" whitespace()+ n:name() {
(n, ConfigEntry::ClassEntry(ConfigClass {
parent: String::new(),
is_external: false,
is_deletion: true,
entries: None
}))
}
rule class(d: usize) -> (String, ConfigEntry) = regular_class(d) / external_class() / deleted_class()
pub rule config() -> Config = whitespace()? e:entries(0) whitespace()? ![_] {
Config {
root_body: ConfigClass {
parent: String::new(),
is_external: false,
is_deletion: false,
entries: Some(e)
}
}
}
}
}
#[must_use]
pub fn is_rapified(data: &[u8]) -> bool {
data.starts_with(b"\0raP")
}
fn compressed_int_len_of(len: usize) -> usize {
compressed_int_len(u32::try_from(len).unwrap_or(u32::MAX))
}
#[derive(Debug)]
pub struct Config {
root_body: ConfigClass,
}
#[derive(Debug)]
pub struct ConfigClass {
parent: String,
is_external: bool,
is_deletion: bool,
entries: Option<Vec<(String, ConfigEntry)>>,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ConfigEntry {
StringEntry(String),
FloatEntry(f32),
IntEntry(i32),
ArrayEntry(ConfigArray),
ClassEntry(ConfigClass),
}
#[derive(Debug)]
pub struct ConfigArray {
is_expansion: bool,
elements: Vec<ConfigArrayElement>,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ConfigArrayElement {
StringElement(String),
FloatElement(f32),
IntElement(i32),
ArrayElement(ConfigArray),
}
impl ConfigArrayElement {
fn rapified_length(&self) -> usize {
match self {
ConfigArrayElement::StringElement(s) => s.len() + 2,
ConfigArrayElement::FloatElement(_f) => 5,
ConfigArrayElement::IntElement(_i) => 5,
ConfigArrayElement::ArrayElement(a) => {
1 + compressed_int_len_of(a.elements.len())
+ usize::sum(a.elements.iter().map(ConfigArrayElement::rapified_length))
}
}
}
}
const MAX_RAPIFIED_DEPTH: u32 = 129;
const MAX_RAPIFIED_ENTRIES: u64 = 4_000_000;
struct ReadBudget {
depth: u32,
remaining: u64,
open_bodies: Vec<u32>,
seen_bodies: HashSet<u32>,
}
impl ReadBudget {
fn new() -> Self {
ReadBudget {
depth: 0,
remaining: MAX_RAPIFIED_ENTRIES,
open_bodies: Vec::new(),
seen_bodies: HashSet::new(),
}
}
fn enter(&mut self) -> Result<(), Error> {
if self.depth >= MAX_RAPIFIED_DEPTH {
return Err(error!(
"Rapified config nesting is deeper than {MAX_RAPIFIED_DEPTH} levels."
));
}
self.depth += 1;
Ok(())
}
fn leave(&mut self) {
self.depth = self.depth.saturating_sub(1);
}
fn take_entry(&mut self) -> Result<(), Error> {
self.remaining = self
.remaining
.checked_sub(1)
.ok_or_else(|| error!("Rapified config has too many entries."))?;
Ok(())
}
}
impl ConfigArray {
#[must_use]
pub fn elements(&self) -> &[ConfigArrayElement] {
&self.elements
}
#[must_use]
pub fn is_expansion(&self) -> bool {
self.is_expansion
}
fn write<O: Write>(&self, output: &mut O) -> Result<(), Error> {
output.write_all(b"{")?;
for (key, value) in self.elements.iter().enumerate() {
match value {
ConfigArrayElement::ArrayElement(a) => {
a.write(output)?;
}
ConfigArrayElement::StringElement(s) => {
output.write_all(
format!(
"\"{}\"",
s.replace('\r', "\\r")
.replace('\n', "\\n")
.replace('"', "\"\"")
)
.as_bytes(),
)?;
}
ConfigArrayElement::FloatElement(f) => {
output.write_all(format!("{f:?}").as_bytes())?;
}
ConfigArrayElement::IntElement(i) => {
output.write_all(format!("{i}").as_bytes())?;
}
}
if key < self.elements.len() - 1 {
output.write_all(b", ")?;
}
}
output.write_all(b"}")?;
Ok(())
}
fn write_rapified<O: Write>(&self, output: &mut O) -> Result<usize, Error> {
let num_elements = u32::try_from(self.elements.len())
.map_err(|_| error!("too many array elements: {}", self.elements.len()))?;
let mut written = output.write_compressed_int(num_elements)?;
for (index, element) in self.elements.iter().enumerate() {
match element {
ConfigArrayElement::StringElement(s) => {
output.write_all(&[0])?;
output
.write_cstring(s)
.prepend_error(format!("Array element {index}:"))?;
written += s.len() + 2;
}
ConfigArrayElement::FloatElement(f) => {
output.write_all(&[1])?;
output.write_f32::<LittleEndian>(*f)?;
written += 5;
}
ConfigArrayElement::IntElement(i) => {
output.write_all(&[2])?;
output.write_i32::<LittleEndian>(*i)?;
written += 5;
}
ConfigArrayElement::ArrayElement(a) => {
output.write_all(&[3])?;
written += 1 + a
.write_rapified(output)
.prepend_error(format!("Array element {index}:"))?;
}
}
}
Ok(written)
}
fn read_rapified<I: Read + Seek>(
input: &mut I,
budget: &mut ReadBudget,
) -> Result<ConfigArray, Error> {
budget.enter()?;
let result = ConfigArray::read_rapified_elements(input, budget);
budget.leave();
result
}
fn read_rapified_elements<I: Read + Seek>(
input: &mut I,
budget: &mut ReadBudget,
) -> Result<ConfigArray, Error> {
let num_elements: u32 = input.read_compressed_int()?;
let mut elements: Vec<ConfigArrayElement> =
Vec::with_capacity(num_elements.min(65536) as usize);
for _i in 0..num_elements {
budget.take_entry()?;
let mut buf = [0u8; 1];
input.read_exact(&mut buf)?;
let element_type = buf[0];
if element_type == 0 {
elements.push(ConfigArrayElement::StringElement(input.read_cstring()?));
} else if element_type == 1 {
elements.push(ConfigArrayElement::FloatElement(
input.read_f32::<LittleEndian>()?,
));
} else if element_type == 2 {
elements.push(ConfigArrayElement::IntElement(
input.read_i32::<LittleEndian>()?,
));
} else if element_type == 3 {
elements.push(ConfigArrayElement::ArrayElement(
ConfigArray::read_rapified(input, budget)?,
));
} else {
return Err(error!("Unrecognized array element type: {}", element_type));
}
}
Ok(ConfigArray {
is_expansion: false,
elements,
})
}
}
impl ConfigEntry {
fn rapified_length(&self) -> usize {
match self {
ConfigEntry::StringEntry(s) => s.len() + 3,
ConfigEntry::FloatEntry(_f) => 6,
ConfigEntry::IntEntry(_i) => 6,
ConfigEntry::ArrayEntry(a) => {
let len = 1
+ compressed_int_len_of(a.elements.len())
+ usize::sum(a.elements.iter().map(ConfigArrayElement::rapified_length));
if a.is_expansion { len + 4 } else { len }
}
ConfigEntry::ClassEntry(c) => {
if c.is_external || c.is_deletion {
1
} else {
5
}
}
}
}
}
impl ConfigClass {
#[must_use]
pub fn parent(&self) -> Option<&str> {
if self.parent.is_empty() {
None
} else {
Some(&self.parent)
}
}
#[must_use]
pub fn is_external(&self) -> bool {
self.is_external
}
#[must_use]
pub fn is_deletion(&self) -> bool {
self.is_deletion
}
pub fn entries(&self) -> impl Iterator<Item = (&str, &ConfigEntry)> {
self.entries
.iter()
.flatten()
.map(|(name, entry)| (name.as_str(), entry))
}
#[must_use]
pub fn get(&self, name: &str) -> Option<&ConfigEntry> {
self.entries()
.find(|(n, _)| n.eq_ignore_ascii_case(name))
.map(|(_, entry)| entry)
}
fn write<O: Write>(&self, output: &mut O, level: usize, indent: &str) -> Result<(), Error> {
if let Some(entries) = &self.entries {
if level > 0 && !entries.is_empty() {
output.write_all(b"\n")?;
}
for (key, value) in entries {
text_name(key, "Entry")?;
output.write_all(indent.repeat(level).as_bytes())?;
match value {
ConfigEntry::ClassEntry(c) => {
if c.is_deletion {
output.write_all(format!("delete {key};\n").as_bytes())?;
} else if c.is_external {
output.write_all(format!("class {key};\n").as_bytes())?;
} else {
let parent = if c.parent.is_empty() {
String::new()
} else {
text_name(&c.parent, "Parent class")?;
format!(": {}", c.parent)
};
match &c.entries {
Some(entries) => {
if entries.is_empty() {
output.write_all(
format!("class {key}{parent} {{}};\n").as_bytes(),
)?;
} else {
output.write_all(
format!("class {key}{parent} {{").as_bytes(),
)?;
c.write(output, level + 1, indent)?;
output.write_all(indent.repeat(level).as_bytes())?;
output.write_all(b"};\n")?;
}
}
None => {
output.write_all(
format!("class {key}{parent} {{}};\n").as_bytes(),
)?;
}
}
}
}
ConfigEntry::StringEntry(s) => {
output.write_all(
format!(
"{} = \"{}\";\n",
key,
s.replace('\r', "\\r")
.replace('\n', "\\n")
.replace('"', "\"\"")
)
.as_bytes(),
)?;
}
ConfigEntry::FloatEntry(f) => {
output.write_all(format!("{key} = {f:?};\n").as_bytes())?;
}
ConfigEntry::IntEntry(i) => {
output.write_all(format!("{key} = {i};\n").as_bytes())?;
}
ConfigEntry::ArrayEntry(a) => {
if a.is_expansion {
output.write_all(format!("{key}[] += ").as_bytes())?;
} else {
output.write_all(format!("{key}[] = ").as_bytes())?;
}
a.write(output)?;
output.write_all(b";\n")?;
}
}
}
}
Ok(())
}
fn rapified_length(&self) -> usize {
match &self.entries {
Some(entries) => {
self.parent.len()
+ 1
+ compressed_int_len_of(entries.len())
+ usize::sum(entries.iter().map(|(k, v)| {
k.len()
+ 1
+ v.rapified_length()
+ match v {
ConfigEntry::ClassEntry(c) => c.rapified_length(),
_ => 0,
}
}))
}
None => 0,
}
}
fn write_rapified<O: Write>(&self, output: &mut O, offset: usize) -> Result<usize, Error> {
let mut written = 0;
match &self.entries {
Some(entries) => {
output.write_cstring(&self.parent)?;
written += self.parent.len() + 1;
let num_entries = u32::try_from(entries.len())
.map_err(|_| error!("too many class entries: {}", entries.len()))?;
written += output.write_compressed_int(num_entries)?;
let entries_len = usize::sum(
entries
.iter()
.map(|(k, v)| k.len() + 1 + v.rapified_length()),
);
let mut class_offset = offset + written + entries_len;
let mut class_bodies: Vec<Cursor<Box<[u8]>>> = Vec::new();
let pre_entries = written;
for (name, entry) in entries {
let pre_write = written;
match entry {
ConfigEntry::StringEntry(s) => {
output.write_all(&[1, 0])?;
output
.write_cstring(name)
.and_then(|()| output.write_cstring(s))
.prepend_error(format!("Entry \"{}\":", printable(name)))?;
written += name.len() + s.len() + 4;
}
ConfigEntry::FloatEntry(f) => {
output.write_all(&[1, 1])?;
output.write_cstring(name)?;
output.write_f32::<LittleEndian>(*f)?;
written += name.len() + 7;
}
ConfigEntry::IntEntry(i) => {
output.write_all(&[1, 2])?;
output.write_cstring(name)?;
output.write_i32::<LittleEndian>(*i)?;
written += name.len() + 7;
}
ConfigEntry::ArrayEntry(a) => {
output.write_all(if a.is_expansion { &[5] } else { &[2] })?;
if a.is_expansion {
output.write_all(&[1, 0, 0, 0])?;
written += 4;
}
output.write_cstring(name)?;
written += name.len()
+ 2
+ a.write_rapified(output)
.prepend_error(format!("Entry \"{}\":", printable(name)))?;
}
ConfigEntry::ClassEntry(c) => {
if c.is_external || c.is_deletion {
output.write_all(if c.is_deletion { &[4] } else { &[3] })?;
output.write_cstring(name)?;
written += name.len() + 2;
} else {
output.write_all(&[0])?;
output.write_cstring(name)?;
let class_offset_u32 =
u32::try_from(class_offset).map_err(|_| {
error!("class body offset out of range: {class_offset}")
})?;
output.write_u32::<LittleEndian>(class_offset_u32)?;
written += name.len() + 6;
let buffer: Box<[u8]> =
vec![0; c.rapified_length()].into_boxed_slice();
let mut cursor: Cursor<Box<[u8]>> = Cursor::new(buffer);
class_offset +=
c.write_rapified(&mut cursor, class_offset).prepend_error(
format!("Failed to rapify {}:", printable(name)),
)?;
class_bodies.push(cursor);
}
}
}
assert_eq!(
written - pre_write,
entry.rapified_length() + name.len() + 1
);
}
assert_eq!(written - pre_entries, entries_len);
for cursor in class_bodies {
output.write_all(cursor.get_ref())?;
written += cursor.get_ref().len();
}
}
None => {
return Err(error!("Cannot rapify a class without entries."));
}
}
Ok(written)
}
fn read_rapified<I: Read + Seek>(
input: &mut I,
level: u32,
budget: &mut ReadBudget,
) -> Result<ConfigClass, Error> {
let mut fp = 0;
let body_offset: u32 = if level == 0 {
input.seek(SeekFrom::Start(16))?;
16
} else {
let classbody_fp: u32 = input.read_u32::<LittleEndian>()?;
fp = input.stream_position()?;
input.seek(SeekFrom::Start(classbody_fp.into()))?;
classbody_fp
};
if budget.open_bodies.contains(&body_offset) {
return Err(error!("Rapified class body cycle at offset {body_offset}."));
}
if !budget.seen_bodies.insert(body_offset) {
return Err(error!(
"Rapified class body shared at offset {body_offset}."
));
}
budget.enter()?;
budget.open_bodies.push(body_offset);
let result = ConfigClass::read_rapified_body(input, level, budget);
budget.open_bodies.pop();
budget.leave();
let class = result?;
if level > 0 {
input.seek(SeekFrom::Start(fp))?;
}
Ok(class)
}
fn read_rapified_body<I: Read + Seek>(
input: &mut I,
level: u32,
budget: &mut ReadBudget,
) -> Result<ConfigClass, Error> {
let parent = input.read_cstring()?;
let num_entries: u32 = input.read_compressed_int()?;
let mut entries: Vec<(String, ConfigEntry)> =
Vec::with_capacity(num_entries.min(65536) as usize);
for _i in 0..num_entries {
budget.take_entry()?;
let mut buf = [0u8; 1];
input.read_exact(&mut buf)?;
let entry_type = buf[0];
if entry_type == 0 {
let name = input.read_cstring()?;
let class_entry = ConfigClass::read_rapified(input, level + 1, budget)
.prepend_error(format!(
"Failed to read rapified class \"{}\":",
printable(&name)
))?;
entries.push((name, ConfigEntry::ClassEntry(class_entry)));
} else if entry_type == 1 {
let mut buf = [0u8; 1];
input.read_exact(&mut buf)?;
let subtype = buf[0];
let name = input.read_cstring()?;
if subtype == 0 {
entries.push((name, ConfigEntry::StringEntry(input.read_cstring()?)));
} else if subtype == 1 {
entries.push((
name,
ConfigEntry::FloatEntry(input.read_f32::<LittleEndian>()?),
));
} else if subtype == 2 {
entries.push((
name,
ConfigEntry::IntEntry(input.read_i32::<LittleEndian>()?),
));
} else {
return Err(error!("Unrecognized variable entry subtype: {}.", subtype));
}
} else if entry_type == 2 || entry_type == 5 {
if entry_type == 5 {
input.seek(SeekFrom::Current(4))?;
}
let name = input.read_cstring()?;
let mut array = ConfigArray::read_rapified(input, budget)
.prepend_error("Failed to read rapified array:")?;
array.is_expansion = entry_type == 5;
entries.push((name.clone(), ConfigEntry::ArrayEntry(array)));
} else if entry_type == 3 || entry_type == 4 {
let name = input.read_cstring()?;
let class_entry = ConfigClass {
parent: String::new(),
is_external: entry_type == 3,
is_deletion: entry_type == 4,
entries: None,
};
entries.push((name.clone(), ConfigEntry::ClassEntry(class_entry)));
} else {
return Err(error!("Unrecognized class entry type: {}.", entry_type));
}
}
Ok(ConfigClass {
parent,
is_external: false,
is_deletion: false,
entries: Some(entries),
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct WriteOptions {
indent: String,
}
impl Default for WriteOptions {
fn default() -> Self {
Self {
indent: " ".to_string(),
}
}
}
impl WriteOptions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn indent(mut self, indent: impl Into<String>) -> Result<Self, Error> {
let indent = indent.into();
let length = indent.chars().count();
if length > 64 {
return Err(error!(
"Invalid indentation: it is {length} characters, at most 64 are allowed."
));
}
if !indent.chars().all(|c| c == ' ' || c == '\t') {
return Err(error!(
"Invalid indentation \"{}\": use spaces and tabs only.",
printable(&indent)
));
}
self.indent = indent;
Ok(self)
}
}
impl Config {
#[must_use]
pub fn root(&self) -> &ConfigClass {
&self.root_body
}
pub fn write<O: Write>(&self, output: &mut O) -> Result<(), Error> {
self.write_with(output, &WriteOptions::new())
}
pub fn write_with<O: Write>(
&self,
output: &mut O,
options: &WriteOptions,
) -> Result<(), Error> {
self.root_body.write(output, 0, &options.indent)
}
pub fn to_string(&self) -> Result<String, Error> {
let buffer = Vec::new();
let mut cursor: Cursor<Vec<u8>> = Cursor::new(buffer);
self.write(&mut cursor)?;
String::from_utf8(cursor.into_inner())
.map_err(|e| error!("Invalid UTF-8 in config output: {e}"))
}
pub fn write_rapified<O: Write>(&self, output: &mut O) -> Result<(), Error> {
let mut writer = BufWriter::new(output);
writer.write_all(b"\0raP")?;
writer.write_all(b"\0\0\0\0\x08\0\0\0")?;
let buffer: Box<[u8]> = vec![0; self.root_body.rapified_length()].into_boxed_slice();
let mut cursor: Cursor<Box<[u8]>> = Cursor::new(buffer);
self.root_body
.write_rapified(&mut cursor, 16)
.prepend_error("Failed to rapify root class:")?;
let enum_offset = u32::try_from(16 + cursor.get_ref().len())
.map_err(|_| error!("config too large: {} bytes", cursor.get_ref().len()))?;
writer.write_u32::<LittleEndian>(enum_offset)?;
writer.write_all(cursor.get_ref())?;
writer.write_all(b"\0\0\0\0")?;
Ok(())
}
pub fn to_cursor(&self) -> Result<Cursor<Box<[u8]>>, Error> {
let len = self.root_body.rapified_length() + 20;
let buffer: Box<[u8]> = vec![0; len].into_boxed_slice();
let mut cursor: Cursor<Box<[u8]>> = Cursor::new(buffer);
self.write_rapified(&mut cursor)?;
Ok(cursor)
}
pub fn read<I: Read>(
input: &mut I,
path: Option<PathBuf>,
includefolders: &[PathBuf],
warnings: &mut dyn WarningSink,
) -> Result<Config, Error> {
Self::read_in(
input,
path,
&IncludeFolders::new(includefolders.to_vec()),
warnings,
)
}
pub fn read_in<I: Read>(
input: &mut I,
path: Option<PathBuf>,
includefolders: &IncludeFolders,
warnings: &mut dyn WarningSink,
) -> Result<Config, Error> {
let mut bytes = Vec::new();
input
.read_to_end(&mut bytes)
.prepend_error("Failed to read input file:")?;
if is_rapified(&bytes) {
return Config::read_rapified(&mut Cursor::new(bytes));
}
let buffer =
String::from_utf8(bytes).map_err(|e| error!("Failed to read input file: {}", e))?;
let (preprocessed, info) = preprocess_in(buffer, path, includefolders, warnings)
.prepend_error("Failed to preprocess config:")?;
let mut found = ConfigWarnings(Vec::new());
let mut ctx = EvalCtx::default();
let parsed = config_grammar::config(&preprocessed, &mut found, &mut ctx);
let result = match ctx.error.take() {
Some(e) => return Err(e),
_ => ConfigParseErrorExt::format_error(parsed, &info, &preprocessed),
};
let mut seen = std::collections::HashSet::new();
found.0.retain(|w| seen.insert((w.0, w.1.clone())));
for w in found.0 {
let location = if warnings.is_suppressed(w.2) {
(None, None)
} else {
let lines_before = preprocessed[..w.0].chars().filter(|c| c == &'\n').count();
let idx = min(lines_before, info.line_origins.len().saturating_sub(1)); let file = info
.line_origins
.get(idx)
.and_then(|o| o.1.as_ref())
.map(|p| p.display().to_string());
let line = match info.line_origins.get(idx) {
Some(o) => o.0,
None => u32::try_from(lines_before + 1)
.map_err(|_| error!("line number out of range"))?,
};
(file, Some(line))
};
let mut warning = Warning::new(w.2, w.1);
if let Some(file) = location.0 {
warning = warning.in_file(file);
}
if let Some(line) = location.1 {
warning = warning.at_line(line);
}
warnings.warn(warning);
}
result
}
pub fn from_string(
input: String,
path: Option<PathBuf>,
includefolders: &[PathBuf],
warnings: &mut dyn WarningSink,
) -> Result<Config, Error> {
let mut cursor = Cursor::new(input.into_bytes());
Self::read(&mut cursor, path, includefolders, warnings)
}
pub fn from_string_in(
input: String,
path: Option<PathBuf>,
includefolders: &IncludeFolders,
warnings: &mut dyn WarningSink,
) -> Result<Config, Error> {
let mut cursor = Cursor::new(input.into_bytes());
Self::read_in(&mut cursor, path, includefolders, warnings)
}
pub fn read_rapified<I: Read + Seek>(input: &mut I) -> Result<Config, Error> {
let mut reader = BufReader::new(input);
let mut buffer = [0; 4];
reader.read_exact(&mut buffer)?;
if &buffer != b"\0raP" {
return Err(error!("File doesn't seem to be a rapified config."));
}
Ok(Config {
root_body: ConfigClass::read_rapified(&mut reader, 0, &mut ReadBudget::new())?,
})
}
}