Skip to content

Commit

Permalink
Switch Version parsing from returning Option to Result
Browse files Browse the repository at this point in the history
Also implement a few basic tests in relation to #17 and move version into a standalone lib file.
  • Loading branch information
kobaltcore committed Apr 28, 2024
1 parent 6aba811 commit 5927871
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/bin/renconstruct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use itertools::Itertools;
use jwalk::WalkDir;
use renkit::common::Version;
use renkit::renconstruct::config::{Config, CustomOptionValue, TaskOptions};
use renkit::renconstruct::tasks::{
task_convert_images_pre, task_keystore_post, task_keystore_pre, task_lint_pre,
task_notarize_post, Task, TaskContext,
};
use renkit::renutil::{get_registry, install, launch};
use renkit::version::Version;
use rustpython::vm::builtins::{PyList, PyStr};
use rustpython::vm::convert::ToPyObject;
use rustpython::vm::function::FuncArgs;
Expand Down
6 changes: 3 additions & 3 deletions src/bin/renutil.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use anyhow::anyhow;
use anyhow::Result;
use clap::{Parser, Subcommand};
use renkit::common::Version;
use renkit::renutil::{cleanup, get_registry, install, launch, list, show, uninstall};
use renkit::version::Version;
use std::path::PathBuf;

#[derive(Parser)]
Expand All @@ -17,8 +17,8 @@ struct Cli {

fn parse_version(version: &str) -> Result<Version> {
match Version::from_str(version) {
Some(version) => Ok(version),
None => Err(anyhow!("Invalid version: {}", version)),
Ok(version) => Ok(version),
Err(e) => Err(anyhow!("Invalid version: {} - {}", version, e)),
}
}

Expand Down
118 changes: 0 additions & 118 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,130 +1,12 @@
use crate::renutil::{Instance, Local, Remote};
use anyhow::Result;
use jwalk::{ClientState, DirEntry};
use reqwest::Url;
use std::{
fs::File,
io::{Read, Seek, Write},
path::PathBuf,
};
use zip::{write::FileOptions, ZipWriter};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Version {
pub major: u32,
pub minor: u32,
pub patch: u32,
pub hotfix: u32,
pub nightly: bool,
}

impl Version {
pub fn from_str(s: &str) -> Option<Self> {
let reg = regex::Regex::new(r"^(\d+)\.(\d+)(?:\.(\d+))?(?:\.(\d+))?(\+nightly)?$").unwrap();
match reg.captures(s) {
Some(caps) => {
let major = caps.get(1).unwrap().as_str().parse::<u32>().unwrap();
let minor = caps.get(2).unwrap().as_str().parse::<u32>().unwrap();
let patch = caps
.get(3)
.map(|m| m.as_str().parse::<u32>().unwrap())
.unwrap_or(0);
let hotfix = caps
.get(4)
.map(|m| m.as_str().parse::<u32>().unwrap())
.unwrap_or(0);
let nightly = caps.get(5).is_some();
Some(Self {
major,
minor,
patch,
hotfix,
nightly,
})
}
None => None,
}
}

pub fn is_installed(&self, registry: &PathBuf) -> bool {
registry.join(self.to_string()).exists()
}

pub fn to_local(&self, registry: &PathBuf) -> Result<Instance<Local>, std::io::Error> {
if self.is_installed(registry) {
Ok(Instance::new(self.clone()))
} else {
Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Version {} is not installed.", self),
))
}
}

pub fn to_remote(&self, registry: &PathBuf) -> Result<Instance<Remote>, std::io::Error> {
if self.is_installed(registry) {
Err(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
format!("Version {} is installed.", self),
))
} else {
Ok(Instance::new(self.clone()))
}
}

pub fn sdk_url(&self) -> Result<Url> {
match self.nightly {
true => Url::parse(&format!(
"https://nightly.renpy.org/{self}/renpy-{self}-sdk.zip"
))
.map_err(|e| anyhow::anyhow!(e)),
false => Url::parse(&format!(
"https://www.renpy.org/dl/{self}/renpy-{self}-sdk.zip"
))
.map_err(|e| anyhow::anyhow!(e)),
}
}

pub fn rapt_url(&self) -> Result<Url> {
match self.nightly {
true => Url::parse(&format!(
"https://nightly.renpy.org/{self}/renpy-{self}-rapt.zip"
))
.map_err(|e| anyhow::anyhow!(e)),
false => Url::parse(&format!(
"https://www.renpy.org/dl/{self}/renpy-{self}-rapt.zip"
))
.map_err(|e| anyhow::anyhow!(e)),
}
}

pub fn steam_url(&self) -> Result<Url> {
match self.nightly {
true => Url::parse(&format!(
"https://nightly.renpy.org/{self}/renpy-{self}-steam.zip"
))
.map_err(|e| anyhow::anyhow!(e)),
false => Url::parse(&format!(
"https://www.renpy.org/dl/{self}/renpy-{self}-steam.zip"
))
.map_err(|e| anyhow::anyhow!(e)),
}
}

pub fn web_url(&self) -> Result<Url> {
match self.nightly {
true => Url::parse(&format!(
"https://nightly.renpy.org/{self}/renpy-{self}-web.zip"
))
.map_err(|e| anyhow::anyhow!(e)),
false => Url::parse(&format!(
"https://www.renpy.org/dl/{self}/renpy-{self}-web.zip"
))
.map_err(|e| anyhow::anyhow!(e)),
}
}
}

pub fn zip_dir<T, C>(
it: &mut dyn Iterator<Item = jwalk::Result<DirEntry<C>>>,
prefix: Option<&PathBuf>,
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod common;
pub mod renutil;
pub mod renotize;
pub mod renconstruct;
pub mod renotize;
pub mod renutil;
pub mod version;
10 changes: 5 additions & 5 deletions src/renconstruct/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::Version;
use crate::version::Version;
use rustpython_vm::PyObjectRef;
use serde::{Deserialize, Deserializer, Serialize};
use std::{
Expand All @@ -12,10 +12,10 @@ where
{
let buf = String::deserialize(deserializer)?;
match Version::from_str(&buf) {
Some(version) => Ok(version),
None => Err(serde::de::Error::custom(format!(
"Invalid version: {}",
buf
Ok(version) => Ok(version),
Err(e) => Err(serde::de::Error::custom(format!(
"Invalid version: {} - {}",
buf, e
))),
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/renconstruct/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use super::config::{
ConvertImagesOptions, GeneralTaskOptions, ImageFormat, KeystoreOptions, LintOptions,
NotarizeOptions,
};
use crate::common::Version;
use crate::renotize::full_run;
use crate::renutil::launch;
use crate::version::Version;
use anyhow::{bail, Result};
use base64::prelude::*;
use command_executor::command::Command;
Expand Down
10 changes: 5 additions & 5 deletions src/renutil.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::Version;
use crate::version::Version;
use anyhow::anyhow;
use anyhow::Result;
use crossterm::{
Expand Down Expand Up @@ -219,10 +219,10 @@ pub async fn get_available_versions(registry: &PathBuf, online: bool) -> Result<
};

match Version::from_str(href) {
Some(version) => {
Ok(version) => {
versions.push(version);
}
None => {}
Err(_) => {}
}

Ok(())
Expand All @@ -245,10 +245,10 @@ pub async fn get_available_versions(registry: &PathBuf, online: bool) -> Result<
.ok_or(anyhow!("Unable to get file name."))?;

match Version::from_str(path) {
Some(version) => {
Ok(version) => {
versions.push(version);
}
None => {}
Err(_) => {}
}
}
}
Expand Down
Loading

0 comments on commit 5927871

Please sign in to comment.