Skip to content

Commit

Permalink
impl Display for DependencyKind
Browse files Browse the repository at this point in the history
This reuses the Serialize impl for consistency.
  • Loading branch information
jyn514 committed Apr 22, 2021
1 parent 73eb617 commit 5f7eaf7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/dependency.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! This module contains `Dependency` and the types/functions it uses for deserialization.

use std::fmt;

use camino::Utf8PathBuf;
use semver::VersionReq;
use serde::{Deserialize, Deserializer, Serialize};
Expand Down Expand Up @@ -29,6 +31,14 @@ impl Default for DependencyKind {
}
}

impl fmt::Display for DependencyKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let s = serde_json::to_string(self).unwrap();
// skip opening and closing quotes
f.write_str(&s[1..s.len() - 1])
}
}

/// The `kind` can be `null`, which is interpreted as the default - `Normal`.
pub(super) fn parse_dependency_kind<'de, D>(d: D) -> Result<DependencyKind, D::Error>
where
Expand Down
8 changes: 8 additions & 0 deletions tests/test_samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,11 @@ fn advanced_feature_configuration() {
});
assert_eq!(sorted!(all_flag_variants), sorted!(all_features));
}

#[test]
fn depkind_to_string() {
assert_eq!(DependencyKind::Normal.to_string(), "normal");
assert_eq!(DependencyKind::Development.to_string(), "dev");
assert_eq!(DependencyKind::Build.to_string(), "build");
assert_eq!(DependencyKind::Unknown.to_string(), "Unknown");
}

0 comments on commit 5f7eaf7

Please sign in to comment.