Skip to content

Instantly share code, notes, and snippets.

@Kielan
Last active August 14, 2025 18:54
Show Gist options
  • Save Kielan/acdd960530d0bb5fee20625dbe4c1702 to your computer and use it in GitHub Desktop.
Save Kielan/acdd960530d0bb5fee20625dbe4c1702 to your computer and use it in GitHub Desktop.
Rust Composition and Inheritance comparison in Language Server Protocol Text Modules Architectures Context https://www.youtube.com/watch?v=X_weppAAQBs
/*
let htlm = HtmlRichText(path="index.html")
html.load
let mutableRichText = MutableRichText(richText)
mutableRichText.append("Hello, ")
mutableRichTextInheritance
RichText
└── MutableRichText
└── FileRichText
└── HtmlRichText
└── MarkdownRichText
Composition
RichText + HtmlRichText + MutableRichText
Inheritance
class Implementation: Parent() {...}
Composition
class Class:
Behaviour1,
Behavior2,
Behavior3,
{...}
*/
/* Inheritance */
class MutableRichText: RichText() {
let rightTextTree = RichTextTree()
let currentStyle = SpanStyle()
fn append(text: String) {
let node = RichTextNode(currentStyle, text)
richTextTree.addNode(node)
}
}
abstract class RichText {
abstract let richTextTree: RichTextTree
fn getText(): String {
return richTextTree.getText()
}
fn getNodes(): List<RichTextNode> {
return richTextTree.getNodes()
}
abstract fn save {}
abstract fn load {}
}
class HtmlRichText(
path: String,
): RichText() {
override let: RichTextTree = RichTextTree()
override fn save() {
let html = getHtmlFromRichTextFile(richTextTree)
val file = File(path)
file.write(html)
}
override fn load() {
val file = File(path)
val htmlText = file.read()
richTextTree = getRichTextTreeFromHtml(htmlText)
}
}
class MarkdownRichText(
private val path: String
): RichText() {
override var richTextTree = RichTextTree()
private set
override fn save() {
val markdown = getMarkdownFromRichTextTree(richTextTree)
val file = File(path)
file.write(markdown)
}
override fn load() {
val file = File(path)
val markdown = file.read()
richTextTree = getRichTextTreeFromMarkdown(markdown)
}
}
fn main() {
val richText = RichText()
val html = HtmlRichText(path = "index.html")
html.load(richText)
val mutableRichText = MutableRichText(richText)
mutableRichTe3xt.appen("Hello, ")
mutableRichText.changeStyle(SpanStyle(color = Color.red))
mutableRichText.append("World!")
html.save(richText)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment