git » sdk » commit c34e488

Remove redundant TextNodeClass

author Stephen Paul Weber
2025-11-05 21:59:33 UTC
committer Stephen Paul Weber
2025-11-05 21:59:33 UTC
parent 3986c4bddae62dd0526461be5db88546b637c906

Remove redundant TextNodeClass

abstract around the enum instead to provide pseudo polymorphism

borogove/ChatMessage.hx +1 -1
borogove/Stanza.hx +12 -37

diff --git a/borogove/ChatMessage.hx b/borogove/ChatMessage.hx
index a2deabb..84881dc 100644
--- a/borogove/ChatMessage.hx
+++ b/borogove/ChatMessage.hx
@@ -341,7 +341,7 @@ class ChatMessage {
 		var htmlSource = "";
 		var isAction = false;
 		if (htmlBody != null) {
-			htmlSource = htmlBody.getChildren().map(el -> el.traverse(child -> {
+			htmlSource = htmlBody.children.map((el: NodeInterface) -> el.traverse(child -> {
 				if (child.name == "img") {
 					final src = child.attr.get("src");
 					if (src != null) {
diff --git a/borogove/Stanza.hx b/borogove/Stanza.hx
index 702898b..aa9c9c4 100644
--- a/borogove/Stanza.hx
+++ b/borogove/Stanza.hx
@@ -14,34 +14,20 @@ enum Node {
 
 typedef NodeList = Array<Node>;
 
-private interface NodeInterface {
-	public function serialize():String;
-	public function clone():NodeInterface;
-	public function traverse(f: (Stanza)->Bool):NodeInterface;
-}
-
-class TextNodeClass implements NodeInterface {
-	public var content(get, never):String;
-	private final node: TextNode;
-
-	public function new(node: TextNode) {
-		this.node = node;
-	}
-
-	private function get_content(): String {
-		return node.content;
-	}
-
-	public function serialize():String {
-		return node.serialize();
-	}
-
-	public function clone():TextNodeClass {
+abstract NodeInterface(Node) from Node to Node {
+	inline public function traverse(f: (Stanza)->Bool): NodeInterface {
+		switch(this) {
+			case Element(el): el.traverse(f);
+			default:
+		}
 		return this;
 	}
 
-	public function traverse(f: (Stanza)->Bool) {
-		return this;
+	inline public function serialize() {
+		switch(this) {
+			case Element(el): return el.serialize();
+			case CData(text): return text.serialize();
+		}
 	}
 }
 
@@ -63,10 +49,6 @@ abstract TextNode(String) {
 	inline public function clone():TextNode {
 		return new TextNode(this);
 	}
-
-	inline public function toClass() {
-		return new TextNodeClass(new TextNode(this));
-	}
 }
 
 class StanzaError {
@@ -82,7 +64,7 @@ class StanzaError {
 }
 
 @:expose
-class Stanza implements NodeInterface {
+class Stanza {
 	public final name:String = null;
 	public final attr:DynamicAccess<String> = {};
 	public var children(default, null):Array<Node> = [];
@@ -285,13 +267,6 @@ class Stanza implements NodeInterface {
 		return allTags()[0];
 	}
 
-	public function getChildren():Array<NodeInterface> {
-		return children.map(child -> switch(child) {
-			case Element(el): el;
-			case CData(text): text.toClass();
-		});
-	}
-
 	public function getChild(?name:Null<String>, ?xmlns:Null<String>):Null<Stanza> {
 		var ourXmlns = this.attr.get("xmlns");
 		/*