Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use PackageName in manifest #46

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions rocks-bin/src/download.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::Args;
use eyre::Result;
use rocks_lib::config::Config;
use rocks_lib::{config::Config, lua_package::PackageName};

#[derive(Args)]
pub struct Download {
name: String,
name: PackageName,
version: Option<String>,
}

Expand Down
4 changes: 2 additions & 2 deletions rocks-bin/src/install.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use eyre::Result;
use rocks_lib::config::Config;
use rocks_lib::{config::Config, lua_package::PackageName};

#[derive(clap::Args)]
pub struct Install {
/// Name of the rock to install.
name: String,
name: PackageName,
/// Rocks version to install.
version: Option<String>,
}
Expand Down
9 changes: 5 additions & 4 deletions rocks-bin/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ use text_trees::{FormatCharacters, StringTreeNode, TreeFormatting};

use rocks_lib::{
config::Config,
lua_package::PackageName,
manifest::{manifest_from_server, ManifestMetadata},
};

#[derive(Args)]
pub struct Search {
/// Name of the rock to search for.
name: String,
name: PackageName,
/// Rocks version to search for.
version: Option<String>,
// TODO(vhyrro): Add options.
Expand All @@ -29,11 +30,11 @@ pub async fn search(data: Search, config: &Config) -> Result<()> {

let metadata = ManifestMetadata::new(&manifest)?;

let rock_to_version_map: HashMap<&String, Vec<&String>> = metadata
let rock_to_version_map: HashMap<&PackageName, Vec<&String>> = metadata
.repository
.iter()
.filter_map(|(key, value)| {
if key.contains(&data.name) {
if key.to_string().contains(&data.name.to_string()) {
Some((
key,
value.keys().sorted_by(|a, b| Ord::cmp(b, a)).collect_vec(),
Expand All @@ -48,7 +49,7 @@ pub async fn search(data: Search, config: &Config) -> Result<()> {
println!("{}", serde_json::to_string(&rock_to_version_map)?);
} else {
for (key, versions) in rock_to_version_map.into_iter().sorted() {
let mut tree = StringTreeNode::new(key.to_owned());
let mut tree = StringTreeNode::new(key.to_string().to_owned());

for version in versions {
tree.push(version.to_owned());
Expand Down
4 changes: 2 additions & 2 deletions rocks-bin/src/unpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use clap::Args;
use eyre::Result;
use rocks_lib::config::Config;
use rocks_lib::{config::Config, lua_package::PackageName};

#[derive(Args)]
pub struct Unpack {
Expand All @@ -15,7 +15,7 @@ pub struct Unpack {
#[derive(Args)]
pub struct UnpackRemote {
/// The name of the rock to unpack
name: String,
name: PackageName,
/// The version of the rock to download (defaults to latest version)
version: Option<String>,
/// The directory to unpack to
Expand Down
22 changes: 20 additions & 2 deletions rocks-lib/src/lua_package/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use eyre::Result;
use semver::Version;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

mod version;
Expand Down Expand Up @@ -29,7 +29,7 @@ impl LuaPackage {
}

/// A luarocks package name, which is always lowercase
#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Hash)]
pub struct PackageName {
name: String,
}
Expand All @@ -43,6 +43,24 @@ impl PackageName {
}
}

impl<'de> Deserialize<'de> for PackageName {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(PackageName::new(String::deserialize(deserializer)?))
}
}

impl Serialize for PackageName {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.name.serialize(serializer)
}
}

impl From<&str> for PackageName {
fn from(value: &str) -> Self {
Self::new(value.into())
Expand Down
17 changes: 12 additions & 5 deletions rocks-lib/src/manifest/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ use itertools::Itertools;
use mlua::{Lua, LuaSerdeExt};
use std::collections::HashMap;

use crate::config::Config;
use crate::{config::Config, lua_package::PackageName};

#[derive(serde::Deserialize)]
pub struct ManifestMetadata {
pub repository: HashMap<String, HashMap<String, Vec<HashMap<String, String>>>>,
/// The key of each package's HashMap is the version string
pub repository: HashMap<PackageName, HashMap<String, Vec<ManifestRockEntry>>>,
}

#[derive(serde::Deserialize)]
pub struct ManifestRockEntry {
/// e.g. "linux-x86_64", "rockspec", "src", ...
pub arch: String,
}

impl ManifestMetadata {
Expand All @@ -31,19 +38,19 @@ impl ManifestMetadata {
Self::new(&manifest)
}

pub fn has_rock(&self, rock_name: &String) -> bool {
pub fn has_rock(&self, rock_name: &PackageName) -> bool {
self.repository.contains_key(rock_name)
}

pub fn available_versions(&self, rock_name: &String) -> Option<Vec<&String>> {
pub fn available_versions(&self, rock_name: &PackageName) -> Option<Vec<&String>> {
if !self.has_rock(rock_name) {
return None;
}

Some(self.repository[rock_name].keys().collect())
}

pub fn latest_version(&self, rock_name: &String) -> Option<&String> {
pub fn latest_version(&self, rock_name: &PackageName) -> Option<&String> {
if !self.has_rock(rock_name) {
return None;
}
Expand Down
6 changes: 3 additions & 3 deletions rocks-lib/src/operations/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ use std::path::PathBuf;

use eyre::{eyre, Result};

use crate::config::Config;
use crate::{config::Config, lua_package::PackageName};

pub struct DownloadedRock {
pub name: String,
pub name: PackageName,
pub version: String,
pub path: PathBuf,
}

pub async fn download(
rock_name: &String,
rock_name: &PackageName,
rock_version: Option<&String>,
destination_dir: Option<PathBuf>,
config: &Config,
Expand Down
6 changes: 3 additions & 3 deletions rocks-lib/src/operations/install.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use eyre::Result;
use tempdir::TempDir;

use crate::{config::Config, rockspec::Rockspec};
use crate::{config::Config, lua_package::PackageName, rockspec::Rockspec};

pub async fn install(
rock_name: String,
rock_name: PackageName,
rock_version: Option<String>,
config: &Config,
) -> Result<()> {
let temp = TempDir::new(&rock_name)?;
let temp = TempDir::new(&rock_name.to_string())?;

let rock = super::download(
&rock_name,
Expand Down