Skip to content

Commit

Permalink
refactor: introduce PackageName for luarocks package names
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Sep 11, 2024
1 parent 9db6b5a commit d52336b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 25 deletions.
2 changes: 1 addition & 1 deletion rocks-lib/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn build(rockspec: Rockspec, config: &Config) -> Result<()> {
.dependencies
.current_platform()
.iter()
.find(|val| val.rock_name == "lua")
.find(|val| val.rock_name == "lua".into())
.map(|dependency| {
for (possibility, version) in [
("5.4.0", LuaVersion::Lua54),
Expand Down
60 changes: 55 additions & 5 deletions rocks-lib/src/luarock/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,69 @@
use semver::{Error, Version};
use eyre::Result;
use semver::Version;
use serde::Deserialize;
use std::fmt::Display;

mod version;

pub use version::{parse_version, parse_version_req};

// TODO: We probably need a better name for this
pub struct LuaRock {
pub name: String,
pub version: Version,
name: PackageName,
version: Version,
}

impl LuaRock {
pub fn new(name: String, version: String) -> Result<Self, Error> {
pub fn new(name: String, version: String) -> Result<Self> {
Ok(Self {
name,
name: PackageName::new(name),
version: parse_version(&version)?,
})
}
pub fn name(&self) -> &PackageName {
&self.name
}
pub fn version(&self) -> &Version {
&self.version
}
}

/// A luarocks package name, which is always lowercase
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct PackageName {
name: String,
}

impl PackageName {
pub fn new(name: String) -> Self {
Self {
// TODO: validations?
name: name.to_lowercase(),
}
}
}

impl From<&str> for PackageName {
fn from(value: &str) -> Self {
Self::new(value.into())
}
}

impl Display for PackageName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name.as_str())
}
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn parse_name() {
let mut package_name: PackageName = "neorg".into();
assert_eq!(package_name.to_string(), "neorg");
package_name = "LuaFileSystem".into();
assert_eq!(package_name.to_string(), "luafilesystem");
}
}
41 changes: 22 additions & 19 deletions rocks-lib/src/rockspec/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@ use itertools::Itertools;
use semver::VersionReq;
use serde::{de, Deserialize, Deserializer};

use crate::luarock::{parse_version_req, LuaRock};
use crate::luarock::{parse_version_req, LuaRock, PackageName};

use super::{PartialOverride, PerPlatform, PlatformOverridable};

#[derive(Debug, Clone, PartialEq)]
pub struct LuaDependency {
pub rock_name: String,
pub rock_name: PackageName,
pub rock_version_req: VersionReq,
}

impl FromStr for LuaDependency {
type Err = eyre::Error;

fn from_str(str: &str) -> Result<Self> {
let rock_name = str
let rock_name_str = str
.chars()
.peeking_take_while(|t| t.is_alphanumeric() || matches!(t, '-' | '_'))
.collect::<String>();

if rock_name.is_empty() {
if rock_name_str.is_empty() {
return Err(eyre!(
"Could not parse dependency name from {}",
str.to_string()
));
}

let constraints = str.trim_start_matches(&rock_name).trim();
let constraints = str.trim_start_matches(&rock_name_str).trim();
let rock_version_req = match constraints {
"" => VersionReq::default(),
constraints => parse_version_req(constraints.trim_start())?,
};
Ok(Self {
rock_name,
rock_name: PackageName::new(rock_name_str),
rock_version_req,
})
}
Expand All @@ -49,8 +49,8 @@ impl LuaDependency {
}

pub fn matches(&self, rock: &LuaRock) -> bool {
if self.rock_name == rock.name {
self.rock_version_req.matches(&rock.version)
if &self.rock_name == rock.name() {
self.rock_version_req.matches(rock.version())
} else {
false
}
Expand All @@ -74,10 +74,13 @@ impl PartialOverride for Vec<LuaDependency> {
fn apply_overrides(&self, override_vec: &Self) -> Self {
let mut result_map: HashMap<String, LuaDependency> = self
.iter()
.map(|dep| (dep.rock_name.clone(), dep.clone()))
.map(|dep| (dep.rock_name.clone().to_string(), dep.clone()))
.collect();
for override_dep in override_vec {
result_map.insert(override_dep.rock_name.clone(), override_dep.clone());
result_map.insert(
override_dep.rock_name.clone().to_string(),
override_dep.clone(),
);
}
result_map.into_values().collect()
}
Expand Down Expand Up @@ -135,26 +138,26 @@ mod tests {
async fn parse_luarock() {
let neorg = LuaRock::new("neorg".into(), "1.0.0".into()).unwrap();
let expected_version = Version::parse("1.0.0").unwrap();
assert_eq!(neorg.name, "neorg");
assert_eq!(neorg.version, expected_version);
assert_eq!(neorg.name().to_string(), "neorg");
assert_eq!(*neorg.version(), expected_version);
let neorg = LuaRock::new("neorg".into(), "1.0".into()).unwrap();
assert_eq!(neorg.version, expected_version);
assert_eq!(*neorg.version(), expected_version);
let neorg = LuaRock::new("neorg".into(), "1".into()).unwrap();
assert_eq!(neorg.version, expected_version);
assert_eq!(*neorg.version(), expected_version);
}

#[tokio::test]
async fn parse_dependency() {
let dep: LuaDependency = "lua >= 5.1".parse().unwrap();
assert_eq!(dep.rock_name, "lua");
assert_eq!(dep.rock_name.to_string(), "lua");
let dep: LuaDependency = "lua>=5.1".parse().unwrap();
assert_eq!(dep.rock_name, "lua");
assert_eq!(dep.rock_name.to_string(), "lua");
let dep: LuaDependency = "toml-edit >= 0.1.0".parse().unwrap();
assert_eq!(dep.rock_name, "toml-edit");
assert_eq!(dep.rock_name.to_string(), "toml-edit");
let dep: LuaDependency = "lfs".parse().unwrap();
assert_eq!(dep.rock_name, "lfs");
assert_eq!(dep.rock_name.to_string(), "lfs");
let dep: LuaDependency = "neorg 1.0.0".parse().unwrap();
assert_eq!(dep.rock_name, "neorg");
assert_eq!(dep.rock_name.to_string(), "neorg");
let neorg = LuaRock::new("neorg".into(), "1.0.0".into()).unwrap();
assert!(dep.matches(&neorg));
let neorg = LuaRock::new("neorg".into(), "2.0.0".into()).unwrap();
Expand Down

0 comments on commit d52336b

Please sign in to comment.