Skip to content

Commit c91323f

Browse files
committed
feat(swift): implement Document.toString helper
1 parent afeb68b commit c91323f

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

crates/core/src/ffi/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ pub extern "C" fn document_empty() -> *mut dom::Document {
102102
#[export_name = "__liveview_native_core$Document$parse"]
103103
pub extern "C" fn document_parse<'a>(
104104
text: RustStr<'a>,
105-
error: *mut support::RustString,
105+
error: *mut RustString,
106106
) -> support::RustResult {
107107
match dom::Document::parse(text.to_str()) {
108108
Ok(doc) => {
@@ -114,7 +114,7 @@ pub extern "C" fn document_parse<'a>(
114114
}
115115
Err(err) => {
116116
unsafe {
117-
error.write(support::RustString::from_string(err.to_string()));
117+
error.write(RustString::from_string(err.to_string()));
118118
}
119119
support::RustResult {
120120
is_ok: false,
@@ -124,6 +124,12 @@ pub extern "C" fn document_parse<'a>(
124124
}
125125
}
126126

127+
#[export_name = "__liveview_native_core$Document$to_string"]
128+
pub extern "C" fn document_to_string(doc: *mut dom::Document) -> RustString {
129+
let doc = unsafe { &*doc };
130+
RustString::from_string(doc.to_string())
131+
}
132+
127133
#[export_name = "__liveview_native_core$Document$merge"]
128134
pub extern "C" fn document_merge(doc: *mut dom::Document, other: *const dom::Document) -> bool {
129135
let doc = unsafe { &mut *doc };

swift/LiveViewNative/Sources/LiveViewNative/LiveViewNative.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public class Document {
6565
}).get()
6666
}
6767

68+
/// Renders this document to a `String` for display and comparison
69+
public func toString() -> String {
70+
let str = RustString(__liveview_native_core$Document$to_string(self.repr))
71+
return str.toString()
72+
}
73+
6874
/// Register a callback to be fired when a matching event occurs on this document.
6975
///
7076
/// The given callback receives the document to which the event applies, as well as the

swift/LiveViewNative/Tests/LiveViewNativeTests/LiveViewNativeTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ final class LiveViewNativeTests: XCTestCase {
2323
XCTAssertEqual(ctx is MyContext, true)
2424
(ctx as! MyContext).didChange = true
2525
}
26+
let rendered1 = doc1.toString()
27+
XCTAssertEqual(input, rendered1)
2628

2729
let updated = """
2830
<html lang="en">
@@ -36,9 +38,14 @@ final class LiveViewNativeTests: XCTestCase {
3638
</html>
3739
"""
3840
let doc2 = try Document.parse(updated)
41+
let rendered2 = doc2.toString()
42+
XCTAssertEqual(updated, rendered2)
3943

4044
doc1.merge(with: doc2)
4145

4246
XCTAssertEqual(context.didChange, true)
47+
48+
let finalRender = doc1.toString()
49+
XCTAssertEqual(finalRender, rendered2)
4350
}
4451
}

swift/LiveViewNativeCore/include/LiveViewNativeCore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ typedef struct __Node {
4040
__NodeData data;
4141
} __Node;
4242

43+
extern _RustString __liveview_native_core$Document$to_string(__Document doc);
44+
4345
extern __Document __liveview_native_core$Document$empty();
4446

4547
extern void __liveview_native_core$Document$drop(__Document doc);

0 commit comments

Comments
 (0)