<commit_before>#!/bin/lua

local store = require('voucher.store')
local config = require('voucher.config')


local function deepcompare(t1, t2)
    local ty1 = type(t1)
    local ty2 = type(t2)
    if ty1 ~= ty2 then return false end
    -- exclude functions
    if ty1 == 'function' and ty1 == ty2 then
        return true
    end
    -- non-table types can be directly compared
    if ty1 ~= 'table' and ty2 ~= 'table' then return t1 == t2 end
    for k1, v1 in pairs(t1) do
        local v2 = t2[k1]
        if v2 == nil or not deepcompare(v1, v2) then return false end
    end
    for k2, v2 in pairs(t2) do
        local v1 = t1[k2]
        if v1 == nil or not deepcompare(v1, v2) then
            return false
        end
    end
    return true
end

--! Simplify the comparison of vouchers using a metatable for the == operator
local voucher_metatable = {
    __eq = function(self, value)
        return deepcompare(self, value)
    end
}

--! obj attrs name, code, mac, expiration_date, duration_m, mod_counter
function voucher_init(obj)
    local voucher = {}
    voucher.name = obj.name

    if type(obj.code) ~= "string" then
        return nil, "code must be a string"
    end
    voucher.code = obj.code

    if type(obj.mac) == "string" and #obj.mac ~= 17 then
        return nil, "invalid mac"
    end
    voucher.mac = obj.mac

    if obj.expiration_date == nil and obj.duration_m == nil then
        return nil
    elseif obj.expiration_date ~= nil then
        voucher.expiration_date = obj.expiration_date
    elseif obj.duration_m ~= nil then
        voucher.duration_m = obj.duration_m
    end

    voucher.mod_counter = obj.mod_counter or 1

    voucher.tostring = function()
        local v = voucher
        return(string.format('%s\t%s\t%s\t%s\t%s', v.name, v.code, v.mac or 'xx:xx:xx:xx:xx:xx',
                             os.date("%c", v.expiration_date) or '', v.mod_counter))
    end

    setmetatable(voucher, voucher_metatable)
    return voucher
end

local vouchera = {}

function vouchera.init(cfg)
    if cfg ~= nil then<commit_msg>pirania: improve voucher tostring and use it as compare function<commit_after>#!/bin/lua

local store = require('voucher.store')
local config = require('voucher.config')

--! Simplify the comparison of vouchers using a metatable for the == operator
local voucher_metatable = {
    __eq = function(self, value)
        return self.tostring() == value.tostring()
    end
}

--! obj attrs name, code, mac, expiration_date, duration_m, mod_counter
function voucher_init(obj)
    local voucher = {}
    voucher.name = obj.name

    if type(obj.code) ~= "string" then
        return nil, "code must be a string"
    end
    voucher.code = obj.code

    if type(obj.mac) == "string" and #obj.mac ~= 17 then
        return nil, "invalid mac"
    end
    voucher.mac = obj.mac

    if obj.expiration_date == nil and obj.duration_m == nil then
        return nil
    elseif obj.expiration_date ~= nil then
        voucher.expiration_date = obj.expiration_date
    elseif obj.duration_m ~= nil then
        voucher.duration_m = obj.duration_m
    end

    voucher.mod_counter = obj.mod_counter or 1

    --! tostring must reflect all the state of a voucher (so vouchers can be compared reliably using tostring)
    voucher.tostring = function()
        local v = voucher
        return(string.format('%s\t%s\t%s\t%s\t%s\t%s', v.name, v.code, v.mac or 'xx:xx:xx:xx:xx:xx',
                             os.date("%c", v.expiration_date) or '', tostring(v.duration_m), v.mod_counter))
    end

    setmetatable(voucher, voucher_metatable)
    return voucher
end

local vouchera = {}

function vouchera.init(cfg)
    if cfg ~= nil then
{'fraction_non_alphanumeric': 0.10553445469343462, 'fraction_numerical': 0.010580575149213239, 'mean_word_length': 2.6218074656188604, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2764711', 'n_tokens_mistral': 1272, 'n_tokens_neox': 1246, 'n_words': 365}