From 4ac60df3686164fa812c789187b43c2711b24ef7 Mon Sep 17 00:00:00 2001 From: Vhyrro Date: Sun, 11 Feb 2024 20:09:18 +0100 Subject: [PATCH] feat: add license pasring --- Cargo.lock | 10 ++++++++ rocks-bin/Cargo.toml | 1 + rocks-bin/src/rockspec/write_rockspec.rs | 32 ++++++++++++++++-------- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f841fcd6..58c1805e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1916,6 +1916,7 @@ dependencies = [ "octocrab", "rocks-lib", "rustyline", + "spdx", "spinners", "termcolor", "text_trees", @@ -2325,6 +2326,15 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "spdx" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62bde1398b09b9f93fc2fc9b9da86e362693e999d3a54a8ac47a99a5a73f638b" +dependencies = [ + "smallvec", +] + [[package]] name = "spin" version = "0.9.8" diff --git a/rocks-bin/Cargo.toml b/rocks-bin/Cargo.toml index 78514fff..cd5dd6b0 100644 --- a/rocks-bin/Cargo.toml +++ b/rocks-bin/Cargo.toml @@ -13,6 +13,7 @@ nucleo = "0.3.0" octocrab = "0.34.0" rocks-lib = { path = "../rocks-lib/" } rustyline = "13.0.0" +spdx = "0.10.3" spinners = "4.1.1" termcolor = "1.4.1" text_trees = "0.1.2" diff --git a/rocks-bin/src/rockspec/write_rockspec.rs b/rocks-bin/src/rockspec/write_rockspec.rs index 08adfaa2..bcc60c60 100644 --- a/rocks-bin/src/rockspec/write_rockspec.rs +++ b/rocks-bin/src/rockspec/write_rockspec.rs @@ -2,6 +2,7 @@ use clap::Args; use eyre::{eyre, Result}; use itertools::Itertools; use rustyline::error::ReadlineError; +use spdx::LicenseId; use spinners::{Spinner, Spinners}; use crate::rockspec::github_metadata::{self, RepoMetadata}; @@ -37,8 +38,6 @@ macro_rules! parse { // General notes and ideas: // - Should we require the user to create a "project" in order to use this command? -// - Should we grab all collaborators by default? That might end up being massive -// if there's a sizeable project. // - Ask user for a homepage // - Automatically detect build type by inspecting the current repo (is there a Cargo.toml? is // there something that tells us it's a lua project?). @@ -61,6 +60,20 @@ fn parse_list(input: String) -> Result> { } } +fn parse_license(input: String) -> Result> { + match input.to_lowercase().as_str() { + "none" => Ok(None), + _ => Ok(Some( + spdx::imprecise_license_id(&input) + .ok_or(eyre!( + "Unable to identify license '{}', please try again!", + input + ))? + .0, + )), + } +} + pub async fn write_rockspec(_data: WriteRockspec) -> Result<()> { let mut spinner = Spinner::new(Spinners::Dots, "Fetching repository metadata...".into()); @@ -91,7 +104,6 @@ pub async fn write_rockspec(_data: WriteRockspec) -> Result<()> { // let mut stdout = BufferedStandardStream::stdout(ColorChoice::Always); // stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)))?; - // TODO(vhyrro): Make the array inputs less confusing (mention it being space separated) let package_name = parse!( editor.readline(format!("Package Name (empty for '{}'): ", repo_metadata.name).as_str()), identity, @@ -116,19 +128,19 @@ pub async fn write_rockspec(_data: WriteRockspec) -> Result<()> { let license = parse!( editor.readline( format!( - "License (empty for '{}'): ", + "License (empty for '{}', 'none' for no license): ", repo_metadata .license .as_ref() - .unwrap_or(&"*** enter a license ***".to_string()) + .unwrap_or(&"none".to_string()) ) .as_str() ), - identity, // TODO: verify license validity - repo_metadata - .license - .unwrap_or("*** enter a license ***".into()) - )?; + parse_license, + parse_license(repo_metadata.license.unwrap())? + )? + .map(|license| license.name) + .unwrap_or("*** enter license here ***"); let maintainer = parse!( editor.readline(