ver_from_txt
#![feature(slice_concat_trait)]
use std::{error::Error, fmt};
use tracing::{error, warn};
use sver::Ver;
use base64::{Engine, engine::general_purpose::STANDARD};
use aok::Result;
mod name_li;
pub use name_li::name_li;
#[derive(Debug)]
pub struct TxtInvalid;
impl fmt::Display for TxtInvalid {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Invalid text was provided")
}
}
impl Error for TxtInvalid {}
#[derive(Debug)]
pub struct VerUrlLi {
pub ver: Ver,
pub url_li: Vec<String>,
}
pub fn ver_from_txt(project: &str, pre_ver: &[u64; 3], txt: &str) -> Result<Option<VerUrlLi>> {
if let Some((ver, txt)) = txt.split_once(";") {
let ver = vb::d(STANDARD.decode(ver)?)?;
if let Ok::<[u64; 3], _>(sver) = ver.try_into() {
if *pre_ver >= sver {
return Ok(None);
}
let sver = Ver(sver);
let ver = sver.to_string();
let mut url_li = vec![];
for i in txt.split(";") {
if let Some(first) = i.chars().next()
&& first.is_ascii_uppercase()
{
let i = &i[1..];
match first {
'G' => {
let url = format!("https://github.com/{i}/releases/download/{project}-{ver}",);
url_li.push(url);
}
_ => {
warn!("txt unknown : {}", i);
}
}
continue;
} else {
let suffix = format!("/{project}/{ver}");
if let Some((prefix, remain)) = i.split_once("[") {
if let Some((range, remain)) = remain.split_once("]") {
for i in name_li(range) {
url_li.push(format!("https://{prefix}{i}{remain}{suffix}",));
}
} else {
error!("txt invalid : {i}");
}
} else {
url_li.push(format!("https://{i}{suffix}"));
}
}
}
if !url_li.is_empty() {
return Ok(Some(VerUrlLi { ver: sver, url_li }));
}
}
}
Err(TxtInvalid.into())
}
About
This project is an open-source component of i18n.site ⋅ Internationalization Solution.
关于
本项目为 i18n.site ⋅ 国际化解决方案 的开源组件。