Skip to content

Commit

Permalink
fix(core): ser/de for FileKind
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico committed Aug 28, 2024
1 parent f186ee2 commit e16f73e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 23 additions & 19 deletions crates/biome_fs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
41 changes: 40 additions & 1 deletion crates/biome_fs/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileKind>", into = "Vec<FileKind>")
)]
pub struct FileKinds(BitFlags<FileKind>);

impl From<Vec<FileKind>> for FileKinds {
fn from(value: Vec<FileKind>) -> Self {
value
.into_iter()
.fold(FileKinds::default(), |mut acc, kind| {
acc.insert(kind);
acc
})
}
}

impl From<FileKinds> for Vec<FileKind> {
fn from(value: FileKinds) -> Self {
value.iter().collect()
}
}

impl Deref for FileKinds {
type Target = BitFlags<FileKind>;

Expand Down Expand Up @@ -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::<FileKinds>("[\"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\"]");
}
}

0 comments on commit e16f73e

Please sign in to comment.