Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using serde just for printing debug representations #1719

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/custom-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ name = "custom_types"
[dependencies]
anyhow = "1"
bytes = "1.3"
serde_json = "1"
uniffi = {path = "../../uniffi", version = "0.24" }
url = "2.2"

Expand Down
1 change: 0 additions & 1 deletion fixtures/ext-types/guid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ name = "ext_types_guid"
[dependencies]
anyhow = "1"
bytes = "1.3"
serde_json = "1"
thiserror = "1.0"
uniffi = {path = "../../../uniffi", version = "0.24" }

Expand Down
1 change: 0 additions & 1 deletion fixtures/ext-types/http-headermap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ name = "ext_types_http_headermap"
anyhow = "1"
bytes = "1.3"
http = "0.2.9"
serde_json = "1"
thiserror = "1.0"
uniffi = {path = "../../../uniffi", version = "0.24" }

Expand Down
8 changes: 4 additions & 4 deletions uniffi/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ enum Commands {
udl_file: Utf8PathBuf,
},

/// Print the JSON representation of the interface from a dynamic library
PrintJson {
/// Print a debug representation of the interface from a dynamic library
PrintRepr {
/// Path to the library file (.so, .dll, .dylib, or .a)
path: Utf8PathBuf,
},
Expand Down Expand Up @@ -128,8 +128,8 @@ pub fn run_main() -> anyhow::Result<()> {
!no_format,
)?;
}
Commands::PrintJson { path } => {
uniffi_bindgen::print_json(&path)?;
Commands::PrintRepr { path } => {
uniffi_bindgen::print_repr(&path)?;
}
};
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use uniffi_bindgen::bindings::ruby::run_test as ruby_run_test;
pub use uniffi_bindgen::bindings::swift::run_test as swift_run_test;
#[cfg(feature = "bindgen")]
pub use uniffi_bindgen::{
bindings::TargetLanguage, generate_bindings, generate_component_scaffolding, print_json,
bindings::TargetLanguage, generate_bindings, generate_component_scaffolding, print_repr,
};
#[cfg(feature = "build")]
pub use uniffi_build::generate_scaffolding;
Expand Down
1 change: 0 additions & 1 deletion uniffi_bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ heck = "0.4"
once_cell = "1.12"
paste = "1.0"
serde = "1"
serde_json = "1.0.80"
toml = "0.5"
uniffi_meta = { path = "../uniffi_meta", version = "=0.24.1" }
uniffi_testing = { path = "../uniffi_testing", version = "=0.24.1" }
Expand Down
8 changes: 2 additions & 6 deletions uniffi_bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,9 @@ pub fn generate_bindings(
Ok(())
}

pub fn dump_json(library_path: &Utf8Path) -> Result<String> {
pub fn print_repr(library_path: &Utf8Path) -> Result<()> {
let metadata = macro_metadata::extract_from_library(library_path)?;
Ok(serde_json::to_string_pretty(&metadata)?)
}

pub fn print_json(library_path: &Utf8Path) -> Result<()> {
println!("{}", dump_json(library_path)?);
println!("{metadata:#?}");
Ok(())
}

Expand Down
1 change: 0 additions & 1 deletion uniffi_meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ keywords = ["ffi", "bindgen"]
[dependencies]
anyhow = "1"
bytes = "1.3"
serde = { version = "1.0.136", features = ["derive"] }
siphasher = "0.3"
uniffi_checksum_derive = { version = "0.24.1", path = "../uniffi_checksum_derive" }
42 changes: 19 additions & 23 deletions uniffi_meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
use std::{collections::BTreeMap, hash::Hasher};
pub use uniffi_checksum_derive::Checksum;

use serde::Serialize;

mod ffi_names;
pub use ffi_names::*;

Expand Down Expand Up @@ -118,7 +116,7 @@ impl Checksum for &str {
// The namespace of a Component interface.
//
// This is used to match up the macro metadata with the UDL items.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct NamespaceMetadata {
pub crate_name: String,
pub name: String,
Expand All @@ -127,13 +125,13 @@ pub struct NamespaceMetadata {
// UDL file included with `include_scaffolding!()`
//
// This is to find the UDL files in library mode generation
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct UdlFile {
pub module_path: String,
pub name: String,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct FnMetadata {
pub module_path: String,
pub name: String,
Expand All @@ -154,7 +152,7 @@ impl FnMetadata {
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConstructorMetadata {
pub module_path: String,
pub self_name: String,
Expand All @@ -178,7 +176,7 @@ impl ConstructorMetadata {
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct MethodMetadata {
pub module_path: String,
pub self_name: String,
Expand All @@ -201,7 +199,7 @@ impl MethodMetadata {
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct TraitMethodMetadata {
pub module_path: String,
pub trait_name: String,
Expand All @@ -227,10 +225,9 @@ impl TraitMethodMetadata {
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct FnParamMetadata {
pub name: String,
#[serde(rename = "type")]
pub ty: Type,
pub by_ref: bool,
pub optional: bool,
Expand All @@ -249,7 +246,7 @@ impl FnParamMetadata {
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Checksum)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Checksum)]
pub enum LiteralMetadata {
Boolean(bool),
String(String),
Expand All @@ -271,50 +268,49 @@ pub enum LiteralMetadata {

// Represent the radix of integer literal values.
// We preserve the radix into the generated bindings for readability reasons.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Checksum)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Checksum)]
pub enum Radix {
Decimal = 10,
Octal = 8,
Hexadecimal = 16,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct RecordMetadata {
pub module_path: String,
pub name: String,
pub fields: Vec<FieldMetadata>,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct FieldMetadata {
pub name: String,
#[serde(rename = "type")]
pub ty: Type,
pub default: Option<LiteralMetadata>,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct EnumMetadata {
pub module_path: String,
pub name: String,
pub variants: Vec<VariantMetadata>,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct VariantMetadata {
pub name: String,
pub fields: Vec<FieldMetadata>,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct ObjectMetadata {
pub module_path: String,
pub name: String,
pub imp: types::ObjectImpl,
pub uniffi_traits: Vec<UniffiTraitMetadata>,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CallbackInterfaceMetadata {
pub module_path: String,
pub name: String,
Expand All @@ -330,7 +326,7 @@ impl ObjectMetadata {
}

/// The list of traits we support generating helper methods for.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum UniffiTraitMetadata {
Debug {
fmt: MethodMetadata,
Expand All @@ -347,7 +343,7 @@ pub enum UniffiTraitMetadata {
},
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum ErrorMetadata {
Enum { enum_: EnumMetadata, is_flat: bool },
}
Expand All @@ -366,7 +362,7 @@ impl ErrorMetadata {
}
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CustomTypeMetadata {
pub module_path: String,
pub name: String,
Expand All @@ -384,7 +380,7 @@ pub fn checksum<T: Checksum>(val: &T) -> u16 {
}

/// Enum covering all the possible metadata types
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Metadata {
Namespace(NamespaceMetadata),
UdlFile(UdlFile),
Expand Down
8 changes: 3 additions & 5 deletions uniffi_meta/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
//! about how these API-level types map into the lower-level types of the FFI layer as represented
//! by the [`ffi::FfiType`](super::ffi::FfiType) enum, but that's a detail that is invisible to end users.

use serde::Serialize;

use crate::Checksum;

#[derive(Debug, Copy, Clone, Eq, PartialEq, Checksum, Ord, PartialOrd, Serialize)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Checksum, Ord, PartialOrd)]
pub enum ObjectImpl {
Struct,
Trait,
Expand Down Expand Up @@ -51,7 +49,7 @@ impl ObjectImpl {
}
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Checksum, Ord, PartialOrd, Serialize)]
#[derive(Debug, Clone, Copy, Eq, PartialEq, Checksum, Ord, PartialOrd)]
pub enum ExternalKind {
Interface,
// Either a record or enum
Expand All @@ -61,7 +59,7 @@ pub enum ExternalKind {
/// Represents all the different high-level types that can be used in a component interface.
/// At this level we identify user-defined types by name, without knowing any details
/// of their internal structure apart from what type of thing they are (record, enum, etc).
#[derive(Debug, Clone, Eq, PartialEq, Checksum, Ord, PartialOrd, Serialize)]
#[derive(Debug, Clone, Eq, PartialEq, Checksum, Ord, PartialOrd)]
pub enum Type {
// Primitive types.
UInt8,
Expand Down
2 changes: 0 additions & 2 deletions uniffi_testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ camino = "1.0.8"
cargo_metadata = "0.15"
fs-err = "2.7.0"
once_cell = "1.12"
serde = "1"
serde_json = "1"