From 6ddd40509016bffe698a53300afa029b5bc2039a Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 28 Aug 2024 16:37:34 +0100 Subject: [PATCH] fix(core): ser/de for `FileKind` --- .github/workflows/main.yml | 2 +- .github/workflows/pull_request.yml | 2 +- Cargo.lock | 1 + crates/biome_fs/Cargo.toml | 42 ++++++++++++++++------------- crates/biome_fs/src/path.rs | 43 ++++++++++++++++++++++++++++-- 5 files changed, 67 insertions(+), 23 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2ab150cb8c00..ee8101ca5a63 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -93,7 +93,7 @@ jobs: with: cache-base: main - name: Run tests on ${{ matrix.os }} - run: cargo test --workspace + run: cargo test --workspace --all-features coverage: name: Test262 Coverage diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index cb8beda8a175..cd695d9d6f41 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -94,7 +94,7 @@ jobs: with: cache-base: main - name: Run tests - run: cargo test --workspace + run: cargo test --workspace --all-features test-node-api: name: Test node.js API diff --git a/Cargo.lock b/Cargo.lock index e2ba19178498..2208a4ada9bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -480,6 +480,7 @@ dependencies = [ "rustc-hash 1.1.0", "schemars", "serde", + "serde_json", "tracing", ] diff --git a/crates/biome_fs/Cargo.toml b/crates/biome_fs/Cargo.toml index 5d2658377276..1eaae402b2bc 100644 --- a/crates/biome_fs/Cargo.toml +++ b/crates/biome_fs/Cargo.toml @@ -1,30 +1,34 @@ [package] -authors.workspace = true +authors.workspace = true categories.workspace = true -description = "A small wrapper around std::path::PathBuf contains additional information and convenient methods" -edition.workspace = true -homepage.workspace = true -keywords.workspace = true -license.workspace = true -name = "biome_fs" +description = "A small wrapper around std::path::PathBuf contains additional information and convenient methods" +edition.workspace = true +homepage.workspace = true +keywords.workspace = true +license.workspace = true +name = "biome_fs" repository.workspace = true -version = "0.5.7" +version = "0.5.7" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] biome_diagnostics = { workspace = true } -crossbeam = { workspace = true } -directories = "5.0.1" -enumflags2 = { workspace = true, features = ["serde"] } -indexmap = { workspace = true } -oxc_resolver = { workspace = true } -parking_lot = { version = "0.12.3", features = ["arc_lock"] } -rayon = { workspace = true } -rustc-hash = { workspace = true } -schemars = { workspace = true, optional = true } -serde = { workspace = true } -tracing = { workspace = true } +crossbeam = { workspace = true } +directories = "5.0.1" +enumflags2 = { workspace = true, features = ["serde"] } +indexmap = { workspace = true } +oxc_resolver = { workspace = true } +parking_lot = { version = "0.12.3", features = ["arc_lock"] } +rayon = { workspace = true } +rustc-hash = { workspace = true } +schemars = { workspace = true, optional = true } +serde = { workspace = true } +tracing = { workspace = true } + +[dev-dependencies] +serde_json = { workspace = true } + [features] serde = ["schemars", "biome_diagnostics/schema"] diff --git a/crates/biome_fs/src/path.rs b/crates/biome_fs/src/path.rs index eaeaf85756f8..eaca2e0f2253 100644 --- a/crates/biome_fs/src/path.rs +++ b/crates/biome_fs/src/path.rs @@ -41,9 +41,30 @@ pub enum FileKind { } #[derive(Debug, Clone, Hash, Ord, PartialOrd, Eq, PartialEq, Default)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[cfg_attr( + feature = "serde", + derive(serde::Serialize, serde::Deserialize), + serde(from = "Vec", into = "Vec") +)] pub struct FileKinds(BitFlags); +impl From> for FileKinds { + fn from(value: Vec) -> Self { + value + .into_iter() + .fold(FileKinds::default(), |mut acc, kind| { + acc.insert(kind); + acc + }) + } +} + +impl From for Vec { + fn from(value: FileKinds) -> Self { + value.iter().collect() + } +} + impl Deref for FileKinds { type Target = BitFlags; @@ -265,7 +286,7 @@ impl Aliases { #[cfg(test)] mod test { - use crate::path::FileKind; + use crate::path::{FileKind, FileKinds}; use std::ffi::OsStr; #[test] @@ -370,4 +391,22 @@ mod test { ); assert_eq!(iter.next().unwrap().display().to_string(), "src/README.md"); } + + #[test] + #[cfg(feature = "serde")] + fn deserialize_file_kind_from_str() { + let result = serde_json::from_str::("[\"Config\"]"); + assert!(result.is_ok()); + let file_kinds = result.unwrap(); + assert_eq!(file_kinds.contains(FileKind::Config), true); + } + + #[test] + #[cfg(feature = "serde")] + fn serialize_file_kind_into_vec() { + let file_kinds = FileKinds::from(FileKind::Config); + let result = serde_json::to_string(&file_kinds); + assert!(result.is_ok()); + assert_eq!(result.unwrap(), "[\"Config\"]"); + } }