Skip to content

Commit

Permalink
Demonstrate rustbuffer confusion reported in mozilla#2108
Browse files Browse the repository at this point in the history
  • Loading branch information
mhammond committed May 17, 2024
1 parent f14a453 commit 2281eee
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
18 changes: 18 additions & 0 deletions fixtures/error-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,34 @@ fn return_proc_error(e: String) -> Arc<ProcErrorInterface> {
Arc::new(ProcErrorInterface { e })
}

#[derive(thiserror::Error, uniffi::Error, Debug)]
#[uniffi(flat_error)]
pub enum FlatInner {
#[error("{0}")]
CaseA(String),
}

// Enums have good coverage elsewhere, but simple coverage here is good.
#[derive(thiserror::Error, uniffi::Error, Debug)]
pub enum Error {
#[error("Oops")]
Oops,
#[error(transparent)]
FlatInner {
#[from]
error: FlatInner,
}
// TODO: add non-flat inner.
}

#[uniffi::export]
fn oops_enum() -> Result<(), Error> {
Err(Error::Oops)
}

#[uniffi::export]
fn oops_flat_inner() -> Result<(), Error> {
Err(Error::FlatInner { error: FlatInner::CaseA("inner".to_string()) })
}

uniffi::include_scaffolding!("error_types");
7 changes: 7 additions & 0 deletions fixtures/error-types/tests/bindings/test.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ try {
assert(e.msg() == "trait-oops")
}

try {
oopsFlatInner()
throw RuntimeException("Should have failed")
} catch (e: Error.FlatInner) {
assert(false)
}

val e = getError("the error")
assert(e.toString() == "the error")
assert(e.link(0U) == "the error")
Expand Down
11 changes: 11 additions & 0 deletions fixtures/error-types/tests/bindings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,16 @@ def test_procmacro_interface_errors(self):
self.assertEqual(cm.exception.message(), "eek")
self.assertEqual(str(cm.exception), "ProcErrorInterface(eek)")

def test_enum_errors(self):
with self.assertRaises(Error.Oops) as cm:
oops_enum()
# should be able to see "Oops", right!?
#?? self.assertEqual(cm.exception.args[0], "Oops")

def test_enum_flat_inner(self):
with self.assertRaises(Error.Oops) as cm:
oops_flat_inner()
# check value?

if __name__=='__main__':
unittest.main()
15 changes: 15 additions & 0 deletions fixtures/error-types/tests/bindings/test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ do {
assert(e.msg() == "trait-oops")
}

do {
try oopsEnum()
fatalError("Should have thrown")
} catch let e as Error {
assert(String(describing: e) == "Oops")
}

do {
try oopsFlatInner()
fatalError("Should have thrown")
} catch let e as Error {
print(String(describing: e))
assert(String(describing: e) == "??")
}

let e = getError(message: "the error")
assert(String(describing: e) == "the error")
assert(String(reflecting: e) == "ErrorInterface { e: the error }")
Expand Down

0 comments on commit 2281eee

Please sign in to comment.