Skip to content

Commit

Permalink
use version::Version
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed Jan 29, 2025
1 parent d602a0c commit b86b440
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 28 deletions.
27 changes: 5 additions & 22 deletions forc-pkg/src/manifest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ pub mod build_profile;
use crate::pkg::{manifest_file_missing, parsing_failed, wrong_program_type};
use anyhow::{anyhow, bail, Context, Result};
use forc_tracing::println_warning;
use forc_util::{restricted::is_valid_package_version, validate_name, validate_project_name};
use forc_util::{validate_name, validate_project_name};
use semver::Version;
use serde::{de, Deserialize, Serialize};
use serde_with::{serde_as, DisplayFromStr};
use std::{
Expand Down Expand Up @@ -196,8 +197,7 @@ pub struct Project {
pub authors: Option<Vec<String>>,
#[serde(deserialize_with = "validate_package_name")]
pub name: String,
#[serde(default, deserialize_with = "validate_package_version")]
pub version: Option<String>,
pub version: Option<Version>,
pub description: Option<String>,
pub organization: Option<String>,
pub license: String,
Expand Down Expand Up @@ -225,23 +225,6 @@ where
}
}

// Validation function for `version`
fn validate_package_version<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: de::Deserializer<'de>,
{
let version: Option<String> = Deserialize::deserialize(deserializer)?;
if let Some(ref version_str) = version {
if !is_valid_package_version(version_str) {
return Err(de::Error::custom(format!(
"Invalid semantic version: '{}'",
version_str
)));
}
}
Ok(version)
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub struct Network {
Expand Down Expand Up @@ -1398,7 +1381,7 @@ mod tests {
let project = Project {
authors: Some(vec!["Test Author".to_string()]),
name: "test-project".to_string(),
version: Some("0.1.0".to_string()),
version: Some(Version::parse("0.1.0").unwrap()),
description: Some("test description".to_string()),
homepage: None,
documentation: None,
Expand All @@ -1424,7 +1407,7 @@ mod tests {
let project = Project {
authors: Some(vec!["Test Author".to_string()]),
name: "test-project".to_string(),
version: Some("0.1.0".to_string()),
version: Some(Version::parse("0.1.0").unwrap()),
description: Some("test description".to_string()),
homepage: Some(Url::parse("https://example.com").unwrap()),
documentation: Some(Url::parse("https://docs.example.com").unwrap()),
Expand Down
6 changes: 0 additions & 6 deletions forc-util/src/restricted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ pub fn is_valid_project_name_format(name: &str) -> Result<()> {
Ok(())
}

pub fn is_valid_package_version(vers: &str) -> bool {
// Must start with an alphanumeric character, can contain only letters, numbers, underscores, dots and hyphens
let re = Regex::new(r"^[a-zA-Z0-9][\w.-]*$").unwrap();
re.is_match(vers)
}

#[test]
fn test_invalid_char() {
assert_eq!(
Expand Down

0 comments on commit b86b440

Please sign in to comment.