From: Dessalines <dessalines@users.noreply.github.com>
Date: Fri, 14 Jul 2023 08:45:18 +0000 (-0400)
Subject: Make `lemmy_api_common` wasm-compatible (#3587)
X-Git-Url: http://these/git/%22%7Bauthor_url%7D/readmes/%22%7B%7D/%22?a=commitdiff_plain;h=164f4b93d9a4dca2ffe91affe2368c444a1d535f;p=lemmy.git

Make `lemmy_api_common` wasm-compatible (#3587)

* Fixing lemmy_api_common optionals

* Adding taplo for .toml files.

* Versioning taplo in woodpecker.

* Addressing PR comments.
---

diff --git a/.rustfmt.toml b/.rustfmt.toml
index c539ff0b..80c01a69 100644
--- a/.rustfmt.toml
+++ b/.rustfmt.toml
@@ -1,5 +1,5 @@
 tab_spaces = 2
-edition="2021"
-imports_layout="HorizontalVertical"
-imports_granularity="Crate"
-group_imports="One"
\ No newline at end of file
+edition = "2021"
+imports_layout = "HorizontalVertical"
+imports_granularity = "Crate"
+group_imports = "One"
diff --git a/.woodpecker.yml b/.woodpecker.yml
index 4c9bf518..07b0d60b 100644
--- a/.woodpecker.yml
+++ b/.woodpecker.yml
@@ -27,6 +27,11 @@ pipeline:
     commands:
       - prettier -c . '!**/volumes' '!**/dist' '!target' '!**/translations'
 
+  taplo_check:
+    image: tamasfe/taplo:0.8.1
+    commands:
+      - taplo format --check
+
   # use minimum supported rust version for most steps
   cargo_fmt:
     image: *muslrust_image
@@ -87,6 +92,13 @@ pipeline:
       - "! cargo tree -p lemmy_api_common --no-default-features -i diesel"
     # when:
     #   platform: linux/amd64
+  lemmy_api_common_works_with_wasm:
+    image: *muslrust_image
+    environment:
+      CARGO_HOME: .cargo
+    commands:
+      - "rustup target add wasm32-unknown-unknown"
+      - "cargo check --target wasm32-unknown-unknown -p lemmy_api_common"
 
   check_defaults_hjson_updated:
     image: *muslrust_image
diff --git a/Cargo.lock b/Cargo.lock
index b2d3a7bf..3396e1e5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -346,7 +346,7 @@ version = "0.7.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
 dependencies = [
- "getrandom 0.2.8",
+ "getrandom 0.2.10",
  "once_cell",
  "version_check",
 ]
@@ -358,7 +358,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f"
 dependencies = [
  "cfg-if",
- "getrandom 0.2.8",
+ "getrandom 0.2.10",
  "once_cell",
  "version_check",
 ]
@@ -678,7 +678,7 @@ checksum = "28d1c9c15093eb224f0baa400f38fcd713fc1391a6f1c389d886beef146d60a3"
 dependencies = [
  "base64 0.21.2",
  "blowfish",
- "getrandom 0.2.8",
+ "getrandom 0.2.10",
  "subtle",
  "zeroize",
 ]
@@ -2045,9 +2045,9 @@ dependencies = [
 
 [[package]]
 name = "getrandom"
-version = "0.2.8"
+version = "0.2.10"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
+checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
 dependencies = [
  "cfg-if",
  "js-sys",
@@ -2397,17 +2397,6 @@ version = "1.0.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
 
-[[package]]
-name = "idna"
-version = "0.2.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8"
-dependencies = [
- "matches",
- "unicode-bidi",
- "unicode-normalization",
-]
-
 [[package]]
 name = "idna"
 version = "0.3.0"
@@ -2649,6 +2638,7 @@ dependencies = [
  "chrono",
  "encoding",
  "futures",
+ "getrandom 0.2.10",
  "lemmy_db_schema",
  "lemmy_db_views",
  "lemmy_db_views_actor",
@@ -3162,12 +3152,6 @@ dependencies = [
  "regex-automata 0.1.10",
 ]
 
-[[package]]
-name = "matches"
-version = "0.1.9"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f"
-
 [[package]]
 name = "matchit"
 version = "0.5.0"
@@ -3195,7 +3179,7 @@ version = "0.3.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "5736ba45bbac8f7ccc99a897f88ce85e508a18baec973a040f2514e6cdbff0d2"
 dependencies = [
- "idna 0.2.3",
+ "idna 0.3.0",
  "once_cell",
  "regex",
 ]
@@ -4271,7 +4255,7 @@ version = "0.6.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
 dependencies = [
- "getrandom 0.2.8",
+ "getrandom 0.2.10",
 ]
 
 [[package]]
@@ -4437,7 +4421,7 @@ checksum = "1b97ad83c2fc18113346b7158d79732242002427c30f620fa817c1f32901e0a8"
 dependencies = [
  "anyhow",
  "async-trait",
- "getrandom 0.2.8",
+ "getrandom 0.2.10",
  "matchit 0.7.0",
  "opentelemetry 0.16.0",
  "reqwest",
@@ -5915,7 +5899,7 @@ version = "1.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d023da39d1fde5a8a3fe1f3e01ca9632ada0a63e9797de55a879d6e2236277be"
 dependencies = [
- "getrandom 0.2.8",
+ "getrandom 0.2.10",
  "serde",
 ]
 
diff --git a/Cargo.toml b/Cargo.toml
index bcde667a..2a9ffb1a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -31,23 +31,29 @@ debug = 0
 
 [features]
 embed-pictrs = ["pict-rs"]
-console = ["console-subscriber", "opentelemetry", "opentelemetry-otlp", "tracing-opentelemetry", "reqwest-tracing/opentelemetry_0_16"]
+console = [
+  "console-subscriber",
+  "opentelemetry",
+  "opentelemetry-otlp",
+  "tracing-opentelemetry",
+  "reqwest-tracing/opentelemetry_0_16",
+]
 json-log = ["tracing-subscriber/json"]
 prometheus-metrics = ["prometheus", "actix-web-prom"]
 default = []
 
 [workspace]
 members = [
-    "crates/api",
-    "crates/api_crud",
-    "crates/api_common",
-    "crates/apub",
-    "crates/utils",
-    "crates/db_schema",
-    "crates/db_views",
-    "crates/db_views_actor",
-    "crates/db_views_actor",
-    "crates/routes"
+  "crates/api",
+  "crates/api_crud",
+  "crates/api_common",
+  "crates/apub",
+  "crates/utils",
+  "crates/db_schema",
+  "crates/db_views",
+  "crates/db_views_actor",
+  "crates/db_views_actor",
+  "crates/routes",
 ]
 
 [workspace.dependencies]
@@ -61,13 +67,21 @@ lemmy_routes = { version = "=0.18.1", path = "./crates/routes" }
 lemmy_db_views = { version = "=0.18.1", path = "./crates/db_views" }
 lemmy_db_views_actor = { version = "=0.18.1", path = "./crates/db_views_actor" }
 lemmy_db_views_moderator = { version = "=0.18.1", path = "./crates/db_views_moderator" }
-activitypub_federation = { version = "0.4.5", default-features = false, features = ["actix-web"] }
+activitypub_federation = { version = "0.4.5", default-features = false, features = [
+  "actix-web",
+] }
 diesel = "2.1.0"
 diesel_migrations = "2.1.0"
 diesel-async = "0.3.1"
 serde = { version = "1.0.167", features = ["derive"] }
 serde_with = "3.0.0"
-actix-web = { version = "4.3.1", default-features = false, features = ["macros", "rustls", "compress-brotli", "compress-gzip", "compress-zstd"] }
+actix-web = { version = "4.3.1", default-features = false, features = [
+  "macros",
+  "rustls",
+  "compress-brotli",
+  "compress-gzip",
+  "compress-zstd",
+] }
 tracing = "0.1.37"
 tracing-actix-web = { version = "0.7.5", default-features = false }
 tracing-error = "0.2.0"
@@ -87,7 +101,9 @@ base64 = "0.21.2"
 uuid = { version = "1.4.0", features = ["serde", "v4"] }
 async-trait = "0.1.71"
 captcha = "0.0.9"
-anyhow = { version = "1.0.71", features = ["backtrace"] } # backtrace is on by default on nightly, but not stable rust
+anyhow = { version = "1.0.71", features = [
+  "backtrace",
+] } # backtrace is on by default on nightly, but not stable rust
 diesel_ltree = "0.3.0"
 typed-builder = "0.15.0"
 serial_test = "2.0.0"
@@ -96,7 +112,7 @@ sha2 = "0.10.7"
 regex = "1.9.0"
 once_cell = "1.18.0"
 diesel-derive-newtype = "2.1.0"
-diesel-derive-enum = {version = "2.1.0", features = ["postgres"] }
+diesel-derive-enum = { version = "2.1.0", features = ["postgres"] }
 strum = "0.25.0"
 strum_macros = "0.25.1"
 itertools = "0.11.0"
@@ -108,7 +124,7 @@ rand = "0.8.5"
 opentelemetry = { version = "0.19.0", features = ["rt-tokio"] }
 tracing-opentelemetry = { version = "0.19.0" }
 ts-rs = { version = "6.2", features = ["serde-compat", "chrono-impl"] }
-rustls = { version ="0.21.3", features = ["dangerous_configuration"]}
+rustls = { version = "0.21.3", features = ["dangerous_configuration"] }
 futures-util = "0.3.28"
 tokio-postgres = "0.7.8"
 tokio-postgres-rustls = "0.10.0"
diff --git a/crates/api_common/Cargo.toml b/crates/api_common/Cargo.toml
index a9b2bf19..bef5ab28 100644
--- a/crates/api_common/Cargo.toml
+++ b/crates/api_common/Cargo.toml
@@ -14,9 +14,25 @@ path = "src/lib.rs"
 doctest = false
 
 [features]
-full = ["tracing", "rosetta-i18n", "chrono", "lemmy_utils",
-    "lemmy_db_views/full", "lemmy_db_views_actor/full", "lemmy_db_views_moderator/full",
-    "percent-encoding", "encoding", "reqwest-middleware", "webpage", "ts-rs"]
+full = [
+  "tracing",
+  "rosetta-i18n",
+  "chrono",
+  "lemmy_utils",
+  "lemmy_db_views/full",
+  "lemmy_db_views_actor/full",
+  "lemmy_db_views_moderator/full",
+  "percent-encoding",
+  "encoding",
+  "reqwest-middleware",
+  "webpage",
+  "ts-rs",
+  "tokio",
+  "uuid",
+  "reqwest",
+  "actix-web",
+  "futures",
+]
 
 [dependencies]
 lemmy_db_views = { workspace = true }
@@ -33,12 +49,15 @@ reqwest-middleware = { workspace = true, optional = true }
 regex = { workspace = true }
 rosetta-i18n = { workspace = true, optional = true }
 percent-encoding = { workspace = true, optional = true }
-webpage = { version = "1.6", default-features = false, features = ["serde"], optional = true }
+webpage = { version = "1.6", default-features = false, features = [
+  "serde",
+], optional = true }
 encoding = { version = "0.2.33", optional = true }
 anyhow = { workspace = true }
-futures = { workspace = true }
-uuid = { workspace = true }
-tokio = { workspace = true }
-reqwest = { workspace = true }
+futures = { workspace = true, optional = true }
+uuid = { workspace = true, optional = true }
+tokio = { workspace = true, optional = true }
+reqwest = { workspace = true, optional = true }
 ts-rs = { workspace = true, optional = true }
-actix-web = { workspace = true }
+actix-web = { workspace = true, optional = true }
+getrandom = { version = "0.2.10", features = ["js"] }
diff --git a/crates/api_crud/Cargo.toml b/crates/api_crud/Cargo.toml
index a4dfb4ad..1da8335e 100644
--- a/crates/api_crud/Cargo.toml
+++ b/crates/api_crud/Cargo.toml
@@ -23,4 +23,4 @@ url = { workspace = true }
 async-trait = { workspace = true }
 webmention = "0.4.0"
 chrono = { workspace = true }
-uuid = { workspace = true }
\ No newline at end of file
+uuid = { workspace = true }
diff --git a/crates/apub/Cargo.toml b/crates/apub/Cargo.toml
index 0bf02e57..cdddea4f 100644
--- a/crates/apub/Cargo.toml
+++ b/crates/apub/Cargo.toml
@@ -25,7 +25,7 @@ chrono = { workspace = true }
 serde_json = { workspace = true }
 serde = { workspace = true }
 actix-web = { workspace = true }
-tokio = {workspace = true}
+tokio = { workspace = true }
 tracing = { workspace = true }
 strum_macros = { workspace = true }
 url = { workspace = true }
diff --git a/crates/db_schema/Cargo.toml b/crates/db_schema/Cargo.toml
index 26f9b790..a5d3e21f 100644
--- a/crates/db_schema/Cargo.toml
+++ b/crates/db_schema/Cargo.toml
@@ -14,9 +14,27 @@ path = "src/lib.rs"
 doctest = false
 
 [features]
-full = ["diesel", "diesel-derive-newtype", "diesel-derive-enum", "diesel_migrations", "bcrypt", "lemmy_utils",
-    "activitypub_federation", "sha2", "regex", "once_cell", "serde_json", "diesel_ltree",
-    "diesel-async", "deadpool", "ts-rs"]
+full = [
+  "diesel",
+  "diesel-derive-newtype",
+  "diesel-derive-enum",
+  "diesel_migrations",
+  "bcrypt",
+  "lemmy_utils",
+  "activitypub_federation",
+  "sha2",
+  "regex",
+  "once_cell",
+  "serde_json",
+  "diesel_ltree",
+  "diesel-async",
+  "deadpool",
+  "ts-rs",
+  "tokio",
+  "tokio-postgres",
+  "tokio-postgres-rustls",
+  "rustls",
+]
 
 [dependencies]
 chrono = { workspace = true }
@@ -29,25 +47,33 @@ serde_json = { workspace = true, optional = true }
 activitypub_federation = { workspace = true, optional = true }
 lemmy_utils = { workspace = true, optional = true }
 bcrypt = { workspace = true, optional = true }
-diesel = { workspace = true, features = ["postgres","chrono", "serde_json", "uuid"], optional = true }
+diesel = { workspace = true, features = [
+  "postgres",
+  "chrono",
+  "serde_json",
+  "uuid",
+], optional = true }
 diesel-derive-newtype = { workspace = true, optional = true }
 diesel-derive-enum = { workspace = true, optional = true }
 diesel_migrations = { workspace = true, optional = true }
-diesel-async = { workspace = true, features = ["postgres", "deadpool"], optional = true }
+diesel-async = { workspace = true, features = [
+  "postgres",
+  "deadpool",
+], optional = true }
 sha2 = { workspace = true, optional = true }
 regex = { workspace = true, optional = true }
 once_cell = { workspace = true, optional = true }
 diesel_ltree = { workspace = true, optional = true }
 typed-builder = { workspace = true }
 async-trait = { workspace = true }
-tokio = { workspace = true }
 tracing = { workspace = true }
 deadpool = { version = "0.9.5", features = ["rt_tokio_1"], optional = true }
 ts-rs = { workspace = true, optional = true }
-rustls = { workspace = true }
 futures-util = { workspace = true }
-tokio-postgres = { workspace = true }
-tokio-postgres-rustls = { workspace = true }
+tokio = { workspace = true, optional = true }
+tokio-postgres = { workspace = true, optional = true }
+tokio-postgres-rustls = { workspace = true, optional = true }
+rustls = { workspace = true, optional = true }
 uuid = { workspace = true, features = ["v4"] }
 
 [dev-dependencies]
diff --git a/crates/db_views/Cargo.toml b/crates/db_views/Cargo.toml
index 8a1b90b5..69dc1533 100644
--- a/crates/db_views/Cargo.toml
+++ b/crates/db_views/Cargo.toml
@@ -12,18 +12,25 @@ repository.workspace = true
 doctest = false
 
 [features]
-full = ["lemmy_db_schema/full", "diesel", "diesel-async", "diesel_ltree", "tracing", "ts-rs"]
+full = [
+  "lemmy_db_schema/full",
+  "diesel",
+  "diesel-async",
+  "diesel_ltree",
+  "tracing",
+  "ts-rs",
+]
 
 [dependencies]
 lemmy_db_schema = { workspace = true }
 diesel = { workspace = true, optional = true }
-diesel-async = { workspace = true, optional = true}
-diesel_ltree = { workspace = true, optional = true}
+diesel-async = { workspace = true, optional = true }
+diesel_ltree = { workspace = true, optional = true }
 serde = { workspace = true }
 serde_with = { workspace = true }
 tracing = { workspace = true, optional = true }
 typed-builder = { workspace = true }
-ts-rs = { workspace = true, optional = true } 
+ts-rs = { workspace = true, optional = true }
 
 [dev-dependencies]
 serial_test = { workspace = true }
diff --git a/crates/db_views_actor/Cargo.toml b/crates/db_views_actor/Cargo.toml
index e977ca95..da844124 100644
--- a/crates/db_views_actor/Cargo.toml
+++ b/crates/db_views_actor/Cargo.toml
@@ -16,9 +16,16 @@ full = ["lemmy_db_schema/full", "diesel", "diesel-async", "ts-rs"]
 
 [dependencies]
 lemmy_db_schema = { workspace = true }
-diesel = { workspace = true, features = ["postgres","chrono","serde_json"], optional = true }
-diesel-async = { workspace = true, features = ["postgres", "deadpool"], optional = true }
+diesel = { workspace = true, features = [
+  "postgres",
+  "chrono",
+  "serde_json",
+], optional = true }
+diesel-async = { workspace = true, features = [
+  "postgres",
+  "deadpool",
+], optional = true }
 serde = { workspace = true }
 serde_with = { workspace = true }
 typed-builder = { workspace = true }
-ts-rs = { workspace = true, optional = true } 
+ts-rs = { workspace = true, optional = true }
diff --git a/crates/db_views_moderator/Cargo.toml b/crates/db_views_moderator/Cargo.toml
index 4420feea..7efa922e 100644
--- a/crates/db_views_moderator/Cargo.toml
+++ b/crates/db_views_moderator/Cargo.toml
@@ -16,8 +16,15 @@ full = ["lemmy_db_schema/full", "diesel", "diesel-async", "ts-rs"]
 
 [dependencies]
 lemmy_db_schema = { workspace = true }
-diesel = { workspace = true, features = ["postgres","chrono","serde_json"], optional = true }
-diesel-async = { workspace = true, features = ["postgres", "deadpool"], optional = true }
+diesel = { workspace = true, features = [
+  "postgres",
+  "chrono",
+  "serde_json",
+], optional = true }
+diesel-async = { workspace = true, features = [
+  "postgres",
+  "deadpool",
+], optional = true }
 serde = { workspace = true }
 serde_with = { workspace = true }
-ts-rs = { workspace = true, optional = true } 
+ts-rs = { workspace = true, optional = true }
diff --git a/scripts/fix-clippy.sh b/scripts/fix-clippy.sh
index 759de577..5cf870b8 100755
--- a/scripts/fix-clippy.sh
+++ b/scripts/fix-clippy.sh
@@ -24,3 +24,4 @@ cargo clippy --workspace --features console -- \
   -D clippy::indexing_slicing
 
 cargo +nightly fmt
+taplo format