antoniusnaumann / cargo-swift

A cargo plugin to easily build Swift packages from Rust code
https://crates.io/crates/cargo-swift
Apache License 2.0
173 stars 22 forks source link

`unknown throw type` error when composing multiple crate #67

Closed setoelkahfi closed 1 week ago

setoelkahfi commented 2 weeks ago

Not sure if this is an issue with uniffi, cargo swift, or my setup. Repost https://github.com/mozilla/uniffi-rs/issues/2225

I have a networking crate that depends on a crate_error_codes crate and uses cargo swift to generate the swift package of the networking crate. I got an error while generating Swift binding with the cargo swift package command:

unknown throw type: Some(External { module_path: "crate_error_codes", name: "ErrorResponse", namespace: "crate_error_codes", kind: DataClass, tagged: false })

But I ran the same command directly from the crate_error_codes itself without issue. Below is my data structure:


#[derive(Serialize, Deserialize, Debug, Error)]
#[tsync]
pub enum ErrorResponse {
    Unknown { error_code: ErrorCode, message: String },
    UserNotFound { error_code: ErrorCode, message: String },
    // continues ...
}

impl Error for ErrorResponse {}

impl Display for ErrorResponse {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

#[repr(i32)]
#[derive(Serialize_repr, Deserialize_repr, Debug, EnumIter, Enum)]
#[tsync]
pub enum ErrorCode {
    Unknown = 0,
    //User-defined error codes start from 1000
    UserNotFound = 1000,
    // continues ....

setup_scaffolding!();

Cargo.toml

[package]
name = "crate_error_codes"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["lib", "cdylib", "staticlib"]
name = "crate_error_codes"

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_repr = "0.1"
tsync = "1"
strum = "0.26"
strum_macros = "0.26"
uniffi = "0.27"

[build-dependencies]
uniffi = { version = "0.27", features = ["build"] }
[package]
name = "crate_networking"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
crate-type = ["lib", "staticlib"]
name = "crate_networking"

[dependencies]
reqwest = { version = "0.12", features = ["json"] }
uniffi = { version = "0.27", features = ["tokio"] }
log = "^0.4"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1"
url-builder = "0.1.1"

# Local crates
crate_error_codes = { path = "./../../lib/crate_error_codes" }
crate_models = { path = "./../../lib/crate_models" }

[build-dependencies]
uniffi = { version = "0.27", features = ["build"] }

Any pointers?

antoniusnaumann commented 1 week ago

Can you try to remove the .no_deps() call in src/metadata.rs (line 13) in the cargo swift code base?

setoelkahfi commented 1 week ago

It gives me different errors like the published cargo swift one:

❯ ./.path-to-cargo-swift/target/debug/cargo-swift swift package
✔ Swift Package Name · CrateNetworking
✔ Select Target Platforms · iOS
✔ Building target iOS
    cargo build --target aarch64-apple-ios
✔ Building target iOS Simulator
    cargo build --target x86_64-apple-ios
    cargo build --target aarch64-apple-ios-sim
    mkdir -p target/universal-ios/debug
    lipo target/x86_64-apple-ios/debug/libcrate_networking.a \
        target/aarch64-apple-ios-sim/debug/libcrate_networking.a -create -output \
        target/universal-ios/debug/libcrate_networking.a
⠂ Generating Swift bindings...                                                                                                                                thread 'main' panicked at /Users/setoelka/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi_bindgen-0.28.1/src/interface/mod.rs:1126:14:
unknown throw type: Some(External { module_path: "crate_error_codes", name: "ErrorResponse", namespace: "crate_error_codes", kind: DataClass, tagged: false })
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
antoniusnaumann commented 1 week ago

Can you give context why this was closed? @setoelkahfi

setoelkahfi commented 1 week ago

@antoniusnaumann I moved all my data models to the same crate with the networking layer. It sounds counterintuitive but works like a charm with cargo swift.