Skip to content

Commit

Permalink
fix(common): handle deprecation booleans in registry response (#246)
Browse files Browse the repository at this point in the history
Fixes: #244
  • Loading branch information
kouhaidev committed Apr 17, 2023
1 parent 8123013 commit 8ae1219
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
2 changes: 1 addition & 1 deletion crates/node-maintainer/src/resolver.rs
Expand Up @@ -206,7 +206,7 @@ impl<'a> Resolver<'a> {
if let Some(deprecated) = deprecated {
tracing::warn!(
"{} {}@{}: {}",
"deprecated".magenta(),
"deprecated".on_magenta(),
manifest.name.as_ref().unwrap(),
manifest
.version
Expand Down
75 changes: 58 additions & 17 deletions crates/oro-common/src/packument.rs
@@ -1,8 +1,8 @@
use derive_builder::Builder;
use node_semver::Version;
use serde::{Deserialize, Deserializer, Serialize};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;
use std::collections::HashMap;
use std::{collections::HashMap, fmt::Display};
use url::Url;

use crate::{CorgiManifest, Manifest, PersonField};
Expand Down Expand Up @@ -76,10 +76,10 @@ pub struct CorgiVersionMetadata {
pub manifest: CorgiManifest,
#[serde(
default,
deserialize_with = "string_or_bool",
deserialize_with = "deserialize_deprecation_info",
skip_serializing_if = "Option::is_none"
)]
pub deprecated: Option<String>,
pub deprecated: Option<DeprecationInfo>,
}

/// A manifest for an individual package version.
Expand All @@ -95,10 +95,10 @@ pub struct VersionMetadata {
pub has_shrinkwrap: Option<bool>,
#[serde(
default,
deserialize_with = "string_or_bool",
deserialize_with = "deserialize_deprecation_info",
skip_serializing_if = "Option::is_none"
)]
pub deprecated: Option<String>,
pub deprecated: Option<DeprecationInfo>,

#[serde(flatten)]
pub manifest: Manifest,
Expand Down Expand Up @@ -138,17 +138,11 @@ impl From<VersionMetadata> for Manifest {
}
}

fn string_or_bool<'de, D, T>(deserializer: D) -> std::result::Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
T: std::cmp::PartialEq<&'de str> + Deserialize<'de>,
{
let val: T = Deserialize::deserialize(deserializer)?;
if val != "false" {
Ok(Some(val))
} else {
Ok(None)
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
enum StringOrBool {
String(String),
Bool(bool),
}

/// Representation for the `bin` field in package manifests.
Expand All @@ -166,6 +160,53 @@ pub struct NpmUser {
pub email: Option<String>,
}

/// Represents the deprecation state of a package.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum DeprecationInfo {
Reason(String),
UnknownReason,
}

impl Display for DeprecationInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Reason(s) => write!(f, "{:?}", s),
Self::UnknownReason => write!(f, "Unknown Reason"),
}
}
}

impl Serialize for DeprecationInfo {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
DeprecationInfo::Reason(s) => serializer.serialize_str(s),
DeprecationInfo::UnknownReason => serializer.serialize_bool(true),
}
}
}

fn deserialize_deprecation_info<'de, D>(
deserializer: D,
) -> std::result::Result<Option<DeprecationInfo>, D::Error>
where
D: Deserializer<'de>,
{
let val: StringOrBool = Deserialize::deserialize(deserializer)?;
Ok(match val {
StringOrBool::String(s) => Some(DeprecationInfo::Reason(s)),
StringOrBool::Bool(b) => {
if b {
Some(DeprecationInfo::UnknownReason)
} else {
None
}
}
})
}

/// Distribution information for a particular package version.
///
/// This version is a reduced-size CorgiDist that only contains fields from
Expand Down
11 changes: 8 additions & 3 deletions src/commands/view.rs
Expand Up @@ -3,7 +3,7 @@ use clap::Args;
use colored::*;
use humansize::{file_size_opts, FileSize};
use miette::{IntoDiagnostic, Result, WrapErr};
use oro_common::{Bin, Manifest, NpmUser, Person, PersonField, VersionMetadata};
use oro_common::{Bin, DeprecationInfo, Manifest, NpmUser, Person, PersonField, VersionMetadata};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};

use crate::commands::OroCommand;
Expand Down Expand Up @@ -97,8 +97,13 @@ impl OroCommand for ViewCmd {
println!();

// DEPRECATED - <deprecation message>
if let Some(msg) = deprecated.as_ref() {
println!("{} - {}\n", "DEPRECATED".bright_red(), msg);
if let Some(info) = deprecated.as_ref() {
let deprecated = "DEPRECATED".on_magenta();
if let DeprecationInfo::Reason(msg) = info {
println!("{deprecated} {msg}\n");
} else {
println!("{deprecated}\n");
}
}

// keywords: foo, bar, baz
Expand Down

0 comments on commit 8ae1219

Please sign in to comment.