gbf_core/decompiler/ast/
meta.rs#![deny(missing_docs)]
use std::collections::HashMap;
use gbf_macros::AstNodeTransform;
use serde::{Deserialize, Serialize};
use crate::utils::Gs2BytecodeAddress;
use super::{AstKind, AstVisitable};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, AstNodeTransform)]
#[convert_to(AstKind::Meta)]
pub struct MetaNode {
node: Box<AstKind>,
comment: Option<String>,
source_location: Option<Gs2BytecodeAddress>,
properties: HashMap<String, String>,
}
impl MetaNode {
pub fn new(
node: Box<AstKind>,
comment: Option<String>,
source_location: Option<Gs2BytecodeAddress>,
properties: HashMap<String, String>,
) -> Self {
Self {
node,
comment,
source_location,
properties,
}
}
pub fn node(&self) -> &AstKind {
&self.node
}
pub fn comment(&self) -> Option<&String> {
self.comment.as_ref()
}
pub fn source_location(&self) -> Option<Gs2BytecodeAddress> {
self.source_location
}
pub fn properties(&self) -> &HashMap<String, String> {
&self.properties
}
}
impl AstVisitable for MetaNode {
fn accept(&self, visitor: &mut dyn super::visitors::AstVisitor) {
visitor.visit_meta(self);
}
}
impl PartialEq for MetaNode {
fn eq(&self, other: &Self) -> bool {
self.node == other.node
&& self.comment == other.comment
&& self.source_location == other.source_location
&& self.properties == other.properties
}
}
#[cfg(test)]
mod tests {
use crate::decompiler::ast::{comment, emit, new_id, statement};
#[test]
fn test_comment_emit() {
let statement = statement(new_id("foo"), new_id("bar"));
let comment = comment(statement, "Sets foo to bar");
assert_eq!(emit(comment), "// Sets foo to bar\nfoo = bar;");
}
#[test]
fn test_comment_equality() {
let statement = statement(new_id("foo"), new_id("bar"));
let comment1 = comment(statement.clone(), "Sets foo to bar");
let comment2 = comment(statement.clone(), "Sets foo to bar");
let comment3 = comment(statement.clone(), "Sets foo to baz");
assert_eq!(comment1, comment2);
assert_ne!(comment1, comment3);
}
}