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

fix(core): ser/de for FileKind #3736

Merged
merged 3 commits into from
Aug 28, 2024
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
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/biome_deserialize/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use biome_json_parser::{parse_json, JsonParserOptions};
use biome_json_syntax::{AnyJsonValue, JsonMemberName, JsonRoot, T};
use biome_rowan::{AstNode, AstSeparatedList, TokenText};

#[cfg(feature = "serde")]
use crate::DeserializableTypes;

/// It attempts to parse and deserialize a source file in JSON. Diagnostics from the parse phase
/// are consumed and joined with the diagnostics emitted during the deserialization.
///
Expand Down
5 changes: 5 additions & 0 deletions crates/biome_fs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ rayon = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true }
smallvec = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
serde_json = { workspace = true }


[features]
serde = ["schemars", "biome_diagnostics/schema"]

Expand Down
47 changes: 45 additions & 2 deletions crates/biome_fs/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! - shortcuts to open/write to the file
use crate::ConfigName;
use enumflags2::{bitflags, BitFlags};
use smallvec::SmallVec;
use std::cmp::Ordering;
use std::ffi::OsStr;
use std::fs::read_to_string;
Expand Down Expand Up @@ -41,9 +42,33 @@ 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 = "smallvec::SmallVec<[FileKind; 5]>",
into = "smallvec::SmallVec<[FileKind; 5]>"
)
)]
pub struct FileKinds(BitFlags<FileKind>);

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

impl From<FileKinds> for SmallVec<[FileKind; 5]> {
fn from(value: FileKinds) -> Self {
value.iter().collect()
}
}

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

Expand Down Expand Up @@ -265,7 +290,7 @@ impl Aliases {

#[cfg(test)]
mod test {
use crate::path::FileKind;
use crate::path::{FileKind, FileKinds};
use std::ffi::OsStr;

#[test]
Expand Down Expand Up @@ -370,4 +395,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!(file_kinds.contains(FileKind::Config));
}

#[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\"]");
}
}
Loading