From 170e9fb1c0178be432ea35c218273ffe1c60d6e4 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Tue, 17 Dec 2024 22:16:31 +0100 Subject: [PATCH] Add serde support to `PackageType` This adds both serialization and deserialization to the `PackageType` enum, based on its lowercase representation. --- purl/Cargo.toml | 2 +- purl/src/package_type.rs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/purl/Cargo.toml b/purl/Cargo.toml index e09a7d4..d42949b 100644 --- a/purl/Cargo.toml +++ b/purl/Cargo.toml @@ -18,7 +18,7 @@ package-type = ["phf", "unicase"] hex = "0.4.3" percent-encoding = "2.2.0" phf = { version = "0.11.1", features = ["macros", "unicase"], optional = true } -serde = { version = "1.0.150", optional = true } +serde = { version = "1.0.150", optional = true, features = ["derive"] } smartstring = { version = "1.0.1", optional = true } thiserror = "1.0.37" unicase = { version = "2.6.0", optional = true } diff --git a/purl/src/package_type.rs b/purl/src/package_type.rs index ecc2f35..ce2f1f3 100644 --- a/purl/src/package_type.rs +++ b/purl/src/package_type.rs @@ -5,6 +5,8 @@ use std::fmt; use std::str::FromStr; use phf::phf_map; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; use unicase::UniCase; use crate::{ @@ -134,6 +136,8 @@ pub type PurlBuilder = GenericPurlBuilder; /// /// This is a subset of the types described in the PURL spec repository. See /// [`Purl`] for details. +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))] #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] #[non_exhaustive] pub enum PackageType {