From 50e9640243f74e577d9fe436e666ca94855b66e5 Mon Sep 17 00:00:00 2001 From: Christopher Lam Date: Sun, 24 Mar 2019 23:48:24 +0800 Subject: [PATCH] [report] simplify functions to use srfi-1, annotate hash-map->list is simpler than hash-fold All refactored functions are tested in test-report-system.scm --- gnucash/report/report-system/report.scm | 53 ++++++++++--------------- 1 file changed, 21 insertions(+), 32 deletions(-) diff --git a/gnucash/report/report-system/report.scm b/gnucash/report/report-system/report.scm index 00968a1f60..5b5030f9d7 100644 --- a/gnucash/report/report-system/report.scm +++ b/gnucash/report/report-system/report.scm @@ -461,37 +461,30 @@ not found."))) ;; Load and save helper functions +;; list of all report guids in existence (includes standard & custom +;; reports, but not instantiated ones) (define (gnc:all-report-template-guids) - (hash-fold - (lambda (k v p) - (cons k p)) - '() *gnc:_report-templates_*)) + (map car (hash-map->list cons *gnc:_report-templates_*))) ;; return a list of the custom report template guids. (define (gnc:custom-report-template-guids) - (hash-fold - (lambda (k v p) - (if (gnc:report-template-parent-type v) - (begin - (gnc:debug "template " v) - (cons k p)) - p)) - '() *gnc:_report-templates_*)) + (map car (gnc:custom-report-templates-list))) -(define (gnc:find-report-template report-type) - (hash-ref *gnc:_report-templates_* report-type)) +(define (gnc:find-report-template guid) + (hash-ref *gnc:_report-templates_* guid)) (define (gnc:report-template-is-custom/template-guid? guid) - (let* ((custom-template (and (string? guid) - (not (string-null? guid)) - (hash-ref *gnc:_report-templates_* guid)))) - (and custom-template - (gnc:report-template-parent-type custom-template) - #t))) + (assoc guid (gnc:custom-report-templates-list))) (define (gnc:is-custom-report-type report) (gnc:report-template-is-custom/template-guid? (gnc:report-custom-template report))) +;; list of reports saved within the saved-reports; returns a list of +;; pairs whose cars = guid and cdrs = report-template +(define (gnc:custom-report-templates-list) + (filter (compose gnc:report-template-parent-type cdr) + (hash-map->list cons *gnc:_report-templates_*))) + ;; This function should be called right before changing a custom-template's name ;; to test if the new name is unique among the existting custom reports. ;; If not the calling function can prevent the name from being updated. @@ -740,18 +733,14 @@ not found."))) ;; saved-reports file aside as a backup ;; return #t if all templates were saved successfully (define (gnc:save-all-reports) - (let ((save-ok? #t)) - (gnc-saved-reports-backup) - (gnc-saved-reports-write-to-file "" #t) - (hash-for-each - (lambda (k v) - (if (gnc:report-template-parent-type v) - (begin - (gnc:debug "saving report " k) - (if (not (gnc:report-template-save-to-savefile v)) - (set! save-ok? #f))))) - *gnc:_report-templates_*) - save-ok?)) + (gnc-saved-reports-backup) + (gnc-saved-reports-write-to-file "" #t) + (every identity + (map + (lambda (p) + (gnc:debug "saving report " (car p)) + (gnc:report-template-save-to-savefile (cdr p))) + (gnc:custom-report-templates-list)))) ;; gets the renderer from the report template;