From fb7a5b1cd220e7dc7356652c3de0f5cf7f2b111d Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 18 Nov 2022 02:36:55 +0300 Subject: [PATCH] initial commit --- .gitignore | 3 +++ flake.lock | 43 +++++++++++++++++++++++++++++++++++++++++++ flake.nix | 23 +++++++++++++++++++++++ lib.nix | 19 +++++++++++++++++++ lib.test.nix | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 .gitignore create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 lib.nix create mode 100644 lib.test.nix diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f26b10a --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# direnv +.envrc +.direnv diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2be96df --- /dev/null +++ b/flake.lock @@ -0,0 +1,43 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1668650906, + "narHash": "sha256-JuiYfDO23O8oxUUOmhQflmOoJovyC5G4RjcYQMQjrRE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "3a86856a13c88c8c64ea32082a851fefc79aa700", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..1b37178 --- /dev/null +++ b/flake.nix @@ -0,0 +1,23 @@ +{ + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { inherit system; }; + + runTests = pkgs.writeShellScript "runTests" '' + nix eval --impure --expr 'import ./lib.test.nix {}' + ''; + in + { + apps.tests = { + type = "app"; + program = toString runTests; + }; + + }); +} diff --git a/lib.nix b/lib.nix new file mode 100644 index 0000000..9058db1 --- /dev/null +++ b/lib.nix @@ -0,0 +1,19 @@ +let + inherit (builtins) isString isFloat isInt isBool isList concatStringsSep; + + toLua = val: + if isList val then toLuaList val + else if isString val then toLuaStr val + else if isFloat val || isInt val then toString val + else if isBool val then toLuaBool val + else ""; + + toLuaList = val: "{ ${concatStringsSep ", " (map toLua val)} }"; + + toLuaStr = val: "'${val}'"; + + toLuaBool = val: if val then "true" else "false"; +in +{ + inherit toLua; +} diff --git a/lib.test.nix b/lib.test.nix new file mode 100644 index 0000000..28d31eb --- /dev/null +++ b/lib.test.nix @@ -0,0 +1,36 @@ +{ pkgs ? import { } }: + +let + nix2lua = import ./lib.nix; + inherit (nix2lua) toLua; +in +pkgs.lib.runTests { + "test returns an empty string" = { + expr = toLua null; + expected = ""; + }; + "test returns a lua string" = { + expr = toLua "hello world"; + expected = "'hello world'"; + }; + "test returns an integer number" = { + expr = toLua 10; + expected = "10"; + }; + "test returns a float number" = { + expr = toLua 10.1; + expected = "10.100000"; + }; + "test returns true" = { + expr = toLua true; + expected = "true"; + }; + "test returns false" = { + expr = toLua false; + expected = "false"; + }; + "test returns array with all primitive types" = { + expr = toLua [ "hello" 10 10.1 true ]; + expected = "{ 'hello', 10, 10.100000, true }"; + }; +}