1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 09:13:54 +00:00

refactor(lua): parse version w/o regex (#2575)

This commit is contained in:
Dario Vladović 2021-04-15 08:46:29 +02:00 committed by GitHub
parent 98b5807dd2
commit 49f5b9c439
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,9 +3,6 @@ use super::{Context, Module, RootModuleConfig};
use crate::configs::lua::LuaConfig; use crate::configs::lua::LuaConfig;
use crate::formatter::StringFormatter; use crate::formatter::StringFormatter;
use regex::Regex;
const LUA_VERSION_PATERN: &str = "(?P<version>[\\d\\.]+[a-z\\-]*[1-9]*)[^\\s]*";
/// Creates a module with the current Lua version /// Creates a module with the current Lua version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("lua"); let mut module = context.new_module("lua");
@ -33,11 +30,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
_ => None, _ => None,
}) })
.map(|variable| match variable { .map(|variable| match variable {
"version" => { "version" => get_lua_version(context, &config.lua_binary).map(Ok),
let lua_version =
format_lua_version(&get_lua_version(context, &config.lua_binary)?)?;
Some(Ok(lua_version))
}
_ => None, _ => None,
}) })
.parse(None) .parse(None)
@ -55,27 +48,30 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
} }
fn get_lua_version(context: &Context, lua_binary: &str) -> Option<String> { fn get_lua_version(context: &Context, lua_binary: &str) -> Option<String> {
match context.exec_cmd(lua_binary, &["-v"]) { let output = context.exec_cmd(lua_binary, &["-v"])?;
Some(output) => { let lua_version = if output.stdout.is_empty() {
if output.stdout.is_empty() { output.stderr
Some(output.stderr)
} else { } else {
Some(output.stdout) output.stdout
} };
}
None => None, parse_lua_version(&lua_version)
}
} }
fn format_lua_version(lua_stdout: &str) -> Option<String> { fn parse_lua_version(lua_version: &str) -> Option<String> {
// lua -v output looks like this: // lua -v output looks like this:
// Lua 5.4.0 Copyright (C) 1994-2020 Lua.org, PUC-Rio // Lua 5.4.0 Copyright (C) 1994-2020 Lua.org, PUC-Rio
// luajit -v output looks like this: // luajit -v output looks like this:
// LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/ // LuaJIT 2.0.5 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/
let re = Regex::new(LUA_VERSION_PATERN).ok()?; let version = lua_version
let captures = re.captures(lua_stdout)?; // Lua: split into ["Lua", "5.4.0", "Copyright", ...]
let version = &captures["version"]; // LuaJIT: split into ["LuaJIT", "2.0.5", "--", ...]
.split_whitespace()
// Lua: take "5.4.0"
// LuaJIT: take "2.0.5"
.nth(1)?;
Some(format!("v{}", version)) Some(format!("v{}", version))
} }
@ -150,14 +146,14 @@ mod tests {
} }
#[test] #[test]
fn test_format_lua_version() { fn test_parse_lua_version() {
let lua_input = "Lua 5.4.0 Copyright (C) 1994-2020 Lua.org, PUC-Rio"; let lua_input = "Lua 5.4.0 Copyright (C) 1994-2020 Lua.org, PUC-Rio";
assert_eq!(format_lua_version(lua_input), Some("v5.4.0".to_string())); assert_eq!(parse_lua_version(lua_input), Some("v5.4.0".to_string()));
let luajit_input = let luajit_input =
"LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/"; "LuaJIT 2.1.0-beta3 -- Copyright (C) 2005-2017 Mike Pall. http://luajit.org/";
assert_eq!( assert_eq!(
format_lua_version(luajit_input), parse_lua_version(luajit_input),
Some("v2.1.0-beta3".to_string()) Some("v2.1.0-beta3".to_string())
); );
} }