Skip to content
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: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
run: cargo test --verbose --all-features
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.cargo.features": "all"
}
46 changes: 46 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ license = "GPL-3.0"
rust-version = "1.85.0"

[dependencies]
serde = { version = "1.0", features = ["derive"], optional = true }
pest = "2.8.1"
pest_derive = "2.8.1"
thiserror = "2.0.12"

[dev-dependencies]
serde_json = "1.0"
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ Add the crate to your project:
cargo add dyf
```

## Examples
### Serde Support

The `dyf` crate provides optional support for serialization and deserialization using the `serde` crate.
To enable this feature, add the `serde` feature when adding the crate to your project:

```sh
cargo add dyf --features serde
```

Once the `serde` feature is enabled, the `FormatString` structure derives the `Serialize` and `Deserialize` traits.
This allows you to easily serialize and deserialize `FormatString` instances using the `serde` crate.

### Basic Formatting

Expand Down
29 changes: 28 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,17 @@
//! cargo add dyf
//! ```
//!
//! ## Examples
//! ### Serde Support
//!
//! The `dyf` crate provides optional support for serialization and deserialization using the `serde` crate.
//! To enable this feature, add the `serde` feature when adding the crate to your project:
//!
//! ```sh
//! cargo add dyf --features serde
//! ```
//!
//! Once the `serde` feature is enabled, the `FormatString` structure derives the `Serialize` and `Deserialize` traits.
//! This allows you to easily serialize and deserialize `FormatString` instances using the `serde` crate.
//!
//! ### Basic Formatting
//!
Expand Down Expand Up @@ -211,6 +221,9 @@ use std::{
use pest::{Parser, iterators::Pair};
use thiserror::Error;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

mod imp;
mod parser;
use parser::{FmtParser, Rule};
Expand Down Expand Up @@ -425,6 +438,7 @@ pub trait DynDisplay {
/// }
/// }
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub enum FmtType {
/// Default formatting for the type.
Expand Down Expand Up @@ -509,6 +523,7 @@ impl Display for FmtType {
/// The [`Align`] enum determines how text should be aligned when a width is specified
/// in a format specification. It controls whether the text is left-aligned, right-aligned,
/// or centered within the allocated space.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub enum Align {
/// Left-align the text within the field.
Expand Down Expand Up @@ -551,6 +566,7 @@ impl Display for Align {
/// In format strings, these correspond to:
/// - `+` for `Sign::Positive` (show signs for both positive and negative numbers)
/// - `-` for `Sign::Negative` (show signs only for negative numbers, default behavior)
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub enum Sign {
/// Always show the sign for numeric values.
Expand Down Expand Up @@ -586,6 +602,7 @@ impl Display for Sign {
///
/// A format specification in a string typically looks like:
/// `:[fill][align][sign][#][0][width][.precision][type]`
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct FormatSpec {
/// The fill character to use for padding.
Expand Down Expand Up @@ -765,6 +782,7 @@ impl FormatSpec {
}
}

#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
struct Format {
start: usize,
Expand Down Expand Up @@ -856,6 +874,7 @@ impl Format {
/// let fmt_str = fmt.to_string_lossy();
/// let owned_str = fmt.into_string();
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct FormatString {
s: String,
Expand Down Expand Up @@ -1624,4 +1643,12 @@ mod tests {
assert_eq!(format!("{arc}"), dformat_lit!("{}", arc).unwrap());
assert_eq!(format!("{cow_str}"), dformat_lit!("{}", cow_str).unwrap());
}

#[test]
fn test_serde() {
let fs = FormatString::new_from_str("{}").unwrap();
let js_fs = serde_json::to_string(&fs).unwrap();
let fs: FormatString = serde_json::from_str(&js_fs).unwrap();
assert_eq!(format!("{}", 42), dformat!(&fs, 42).unwrap())
}
}