macro_rules! serialize_test {
($name:ident, $dsl:expr, $variant:ident $body:tt $(,)?) => {
#[cfg(test)]
#[test]
fn $name() {
let command: ::std::boxed::Box<dyn $crate::commands::Command> =
::std::boxed::Box::new($crate::commands::$variant $body);
assert_eq!($crate::store::to_dsl(&*command), $dsl, "Rust -> DSL");
let parsed = $crate::store::from_dsl($dsl).expect("DSL -> Rust");
assert!(
*parsed == *command,
"DSL -> Rust: {parsed:?} != {command:?}",
);
}
};
}
macro_rules! register_commands {
($($type:ident),* $(,)?) => {
$(
impl $type {
const COMMAND_NAME: &'static str = {
const SRC: &str = stringify!($type);
const LEN: usize = $crate::commands::snake_len(SRC);
const BYTES: [u8; LEN] = $crate::commands::snake_bytes::<LEN>(SRC);
match ::core::str::from_utf8(&BYTES) {
::core::result::Result::Ok(name) => name,
::core::result::Result::Err(_) => ::core::panic!("non-ASCII command name"),
}
};
}
impl $crate::commands::Command for $type {
fn apply(
self: ::std::boxed::Box<Self>,
db: &mut $crate::db::Db,
env: ::core::option::Option<&dyn $crate::commands::Environment>,
) -> ::core::result::Result<
::std::vec::Vec<::std::boxed::Box<dyn $crate::commands::Command>>,
$crate::db::Error,
> {
$crate::commands::Apply::apply(*self, db, env)
}
fn name(&self) -> &'static str {
<$type>::COMMAND_NAME
}
fn to_value(&self) -> $crate::store::value::Value {
$crate::store::ser::to_value(self).expect("command serialization is infallible")
}
fn as_any(&self) -> &dyn ::core::any::Any {
self
}
}
#[::scattered_collect::scatter($crate::commands::COMMANDS)]
#[allow(non_upper_case_globals)]
static $type: (&'static str, $crate::commands::CommandParser) =
(<$type>::COMMAND_NAME, $crate::commands::parse::<$type>);
)*
};
}
mod auto_disassemble;
mod bytes;
mod comment;
mod equivalent;
mod function;
mod label;
mod note;
use std::any::Any;
use std::collections::BTreeMap;
use std::io;
pub use auto_disassemble::AutoDisassemble;
pub use bytes::{ClearBytes, MapBytes, SetConstantBytes};
pub use comment::{ClearComment, SetComment};
pub use equivalent::{ClearEquivalents, SetEquivalent};
pub use function::{ClearFunction, SetFunction};
pub use label::{ClearLabel, SetLabel};
pub use note::{ClearNote, SetNote};
use scattered_collect::{ScatteredMap, gather};
use serde::de::DeserializeOwned;
use crate::address::AddressValue;
use crate::db::{Db, Error};
use crate::store::error::DslError;
use crate::store::value::Value;
pub trait Environment {
fn load_file_bytes(
&self,
file: &str,
offset: usize,
size: AddressValue,
) -> Result<Vec<u8>, io::Error>;
}
pub trait Command: std::fmt::Debug {
fn apply(
self: Box<Self>,
db: &mut Db,
env: Option<&dyn Environment>,
) -> Result<Vec<Box<dyn Command>>, Error>;
fn name(&self) -> &'static str;
fn to_value(&self) -> Value;
fn as_any(&self) -> &dyn Any;
}
impl PartialEq for dyn Command {
fn eq(&self, other: &Self) -> bool {
self.name() == other.name() && self.to_value() == other.to_value()
}
}
pub trait Apply {
fn apply(
self,
db: &mut Db,
env: Option<&dyn Environment>,
) -> Result<Vec<Box<dyn Command>>, Error>;
}
pub fn boxed(command: impl Command + 'static) -> Box<dyn Command> {
Box::new(command)
}
pub type CommandParser = fn(BTreeMap<String, Value>) -> Result<Box<dyn Command>, DslError>;
#[gather]
pub static COMMANDS: ScatteredMap<&'static str, CommandParser>;
pub fn parse<T: Command + DeserializeOwned + 'static>(
kwargs: BTreeMap<String, Value>,
) -> Result<Box<dyn Command>, DslError> {
let value = Value::Struct {
name: String::new(),
fields: kwargs,
};
Ok(Box::new(crate::store::de::from_value::<T>(value)?))
}
pub const fn snake_len(s: &str) -> usize {
let bytes = s.as_bytes();
let mut len = 0;
let mut i = 0;
while i < bytes.len() {
if i > 0 && bytes[i].is_ascii_uppercase() {
len += 1; }
len += 1;
i += 1;
}
len
}
pub const fn snake_bytes<const N: usize>(s: &str) -> [u8; N] {
let bytes = s.as_bytes();
let mut out = [0u8; N];
let mut out_i = 0;
let mut i = 0;
while i < bytes.len() {
let byte = bytes[i];
if byte.is_ascii_uppercase() {
if i > 0 {
out[out_i] = b'_';
out_i += 1;
}
out[out_i] = byte.to_ascii_lowercase();
} else {
out[out_i] = byte;
}
out_i += 1;
i += 1;
}
out
}