all repos

clerk @ bfe61d3b40d0db412b11af6fcb7df9110770b71d

missing tooling for ledger/hledger
2 files changed, 35 insertions(+), 0 deletions(-)
ast: add constructor functions to help with building ast by hand
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2026-09-08 14:48:57 +0300
Authored at: 2026-09-08 14:33:39 +0300
Change ID: kmrstlxoyklnupmnxvqxwpykxzkoxtlp
Parent: 6573acb
M journal/ast/ast.go
···
        3
        3
         import (

      
        4
        4
         	"fmt"

      
        5
        5
         	"strings"

      
        
        6
        +	"time"

      
        6
        7
         

      
        7
        8
         	"olexsmir.xyz/clerk/journal/token"

      
        8
        9
         )

      ···
        31
        32
         	Year, Month, Day int

      
        32
        33
         	Sep              byte // '-' '/' '.'

      
        33
        34
         	Span             token.Span

      
        
        35
        +}

      
        
        36
        +

      
        
        37
        +// DateOf builds a Date from t, sets '-' as the separator. Helper for handbuilding ast.

      
        
        38
        +func DateOf(t time.Time) Date {

      
        
        39
        +	return Date{

      
        
        40
        +		Year:  t.Year(),

      
        
        41
        +		Month: int(t.Month()),

      
        
        42
        +		Day:   t.Day(),

      
        
        43
        +		Sep:   '-',

      
        
        44
        +	}

      
        34
        45
         }

      
        35
        46
         

      
        36
        47
         // Compare returns -1 if d is before other, 0 if equal, 1 if after.

      ···
        127
        138
         type Account struct {

      
        128
        139
         	Name []SubAccount // ['expenses' 'food']

      
        129
        140
         	Span token.Span

      
        
        141
        +}

      
        
        142
        +

      
        
        143
        +// AccountFromString splits s on ':' into subaccounts. Helper for handbuilding ast.

      
        
        144
        +func AccountFromString(s string) Account {

      
        
        145
        +	subs := strings.Split(s, ":")

      
        
        146
        +	acc := Account{Name: make([]SubAccount, len(subs))}

      
        
        147
        +	for i := range subs {

      
        
        148
        +		acc.Name[i] = SubAccount{Name: subs[i]}

      
        
        149
        +	}

      
        
        150
        +	return acc

      
        130
        151
         }

      
        131
        152
         

      
        132
        153
         func (a Account) String() string {

      
M journal/ast/entries.go
···
        111
        111
         	Span          token.Span

      
        112
        112
         }

      
        113
        113
         

      
        
        114
        +// Quantity parses s as a decimal quantity. Helper for handcrafting ast.

      
        
        115
        +func Quantity(s string) (decimal.Decimal, error) {

      
        
        116
        +	return decimal.FromString(s)

      
        
        117
        +}

      
        
        118
        +

      
        
        119
        +// MustQuantity same as [Quantity] but panics in case of an error.

      
        
        120
        +func MustQuantity(s string) decimal.Decimal {

      
        
        121
        +	d, err := decimal.FromString(s)

      
        
        122
        +	if err != nil {

      
        
        123
        +		panic(err)

      
        
        124
        +	}

      
        
        125
        +	return d

      
        
        126
        +}

      
        
        127
        +

      
        114
        128
         type Cost struct {

      
        115
        129
         	IsTotal bool // @ vs @@

      
        116
        130
         	Amount  Amount