| 1 | # This code is part of Perl distribution Math-Formula version 0.18. | ||
| 2 | # The POD got stripped from this file by OODoc version 3.03. | ||
| 3 | # For contributors see file ChangeLog. | ||
| 4 | |||
| 5 | # This software is copyright (c) 2023-2025 by Mark Overmeer. | ||
| 6 | |||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||
| 9 | # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later | ||
| 10 | |||
| 11 | #oodist: *** DO NOT USE THIS VERSION FOR PRODUCTION *** | ||
| 12 | #oodist: This file contains OODoc-style documentation which will get stripped | ||
| 13 | #oodist: during its release in the distribution. You can use this file for | ||
| 14 | #oodist: testing, however the code of this development version may be broken! | ||
| 15 | |||
| 16 | package Math::Formula::Config::YAML;{ | ||
| 17 | our $VERSION = '0.18'; | ||
| 18 | } | ||
| 19 | |||
| 20 | use parent 'Math::Formula::Config'; | ||
| 21 | |||
| 22 | use warnings; | ||
| 23 | use strict; | ||
| 24 | |||
| 25 | use Log::Report 'math-formula'; | ||
| 26 | |||
| 27 | use YAML::XS qw/Dump Load/; | ||
| 28 | use boolean (); | ||
| 29 | use File::Slurper qw/read_binary/; | ||
| 30 | |||
| 31 | # It is not possible to use YAML.pm, because it cannot produce output where | ||
| 32 | # boolean true and a string with content 'true' can be distinguished. | ||
| 33 | |||
| 34 | use Scalar::Util 'blessed'; | ||
| 35 | |||
| 36 | #-------------------- | ||
| 37 | |||
| 38 | #-------------------- | ||
| 39 | |||
| 40 | sub save($%) | ||
| 41 | { my ($self, $context, %args) = @_; | ||
| 42 | my $name = $context->name; | ||
| 43 | |||
| 44 | local $YAML::XS::Boolean = "boolean"; | ||
| 45 | my $index = $context->_index; | ||
| 46 | |||
| 47 | my $fn = $self->path_for($args{filename} || "$name.yml"); | ||
| 48 | open my $fh, '>:encoding(utf8)', $fn | ||
| 49 | or fault __x"Trying to save context '{name}' to {file}", name => $name, file => $fn; | ||
| 50 | |||
| 51 | $fh->print(Dump $self->_set($index->{attributes})); | ||
| 52 | $fh->print(Dump $self->_set($index->{formulas})); | ||
| 53 | $fh->print(Dump $self->_set($index->{fragments})); | ||
| 54 | |||
| 55 | $fh->close | ||
| 56 | or fault __x"Error on close while saving '{name}' to {file}", name => $name, file => $fn; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _set($) | ||
| 60 | { my ($self, $set) = @_; | ||
| 61 | my %data; | ||
| 62 | $data{$_ =~ s/^ctx_//r} = $self->_serialize($_, $set->{$_}) for keys %$set; | ||
| 63 | \%data; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _serialize($$) | ||
| 67 | { my ($self, $name, $what) = @_; | ||
| 68 | my %attrs; | ||
| 69 | |||
| 70 | if(blessed $what && $what->isa('Math::Formula')) | ||
| 71 | { if(my $r = $what->returns) { $attrs{returns} = $r }; | ||
| 72 | $what = $what->expression; | ||
| 73 | } | ||
| 74 | |||
| 75 | my $v = ''; | ||
| 76 | if(blessed $what && $what->isa('MF::STRING')) | ||
| 77 | { $v = $what->value; | ||
| 78 | } | ||
| 79 | elsif(blessed $what && $what->isa('Math::Formula::Type')) | ||
| 80 | { $v = $what->isa('MF::INTEGER') || $what->isa('MF::FLOAT') ? $what->value | ||
| 81 | : $what->isa('MF::BOOLEAN') ? ($what->value ? boolean::true : boolean::false) | ||
| 82 | : '=' . $what->token; | ||
| 83 | } | ||
| 84 | elsif(ref $what eq 'CODE') | ||
| 85 | { warning __x"cannot (yet) save CODE, skipped '{name}'", name => $name; | ||
| 86 | return undef; | ||
| 87 | } | ||
| 88 | elsif(length $what) | ||
| 89 | { $v = '=' . $what; | ||
| 90 | } | ||
| 91 | |||
| 92 | if(keys %attrs) | ||
| 93 | { $v .= '; ' . (join ', ', map "$_='$attrs{$_}'", sort keys %attrs); | ||
| 94 | } | ||
| 95 | |||
| 96 | return $v; | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub load($%) | ||
| 101 | { my ($self, $name, %args) = @_; | ||
| 102 | my $fn = $self->path_for($args{filename} || "$name.yml"); | ||
| 103 | |||
| 104 | local $YAML::XS::Boolean = "boolean"; | ||
| 105 | my ($attributes, $forms, $frags) = Load(read_binary $fn); | ||
| 106 | |||
| 107 | my $attrs = $self->_set_decode($attributes); | ||
| 108 | Math::Formula::Context->new(name => $name, %$attrs, | ||
| 109 | formulas => $self->_set_decode($forms), | ||
| 110 | ); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub _set_decode($) | ||
| 114 | { my ($self, $set) = @_; | ||
| 115 | $set or return {}; | ||
| 116 | |||
| 117 | my %forms; | ||
| 118 | $forms{$_} = $self->_unpack($_, $set->{$_}) for keys %$set; | ||
| 119 | \%forms; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub _unpack($$) | ||
| 123 | { my ($self, $name, $encoded) = @_; | ||
| 124 | my $dummy = Math::Formula->new('dummy', '7'); | ||
| 125 | |||
| 126 | if(ref $encoded eq 'boolean') | ||
| 127 | { return MF::BOOLEAN->new(undef, $encoded); | ||
| 128 | } | ||
| 129 | |||
| 130 | if($encoded =~ m/^\=(.*?)(?:;\s*(.*))?$/) | ||
| 131 | { my ($expr, $attrs) = ($1, $2 // ''); | ||
| 132 | my %attrs = $attrs =~ m/(\w+)\='([^']+)'/g; | ||
| 133 | return Math::Formula->new($name, $expr =~ s/\\"/"/gr, %attrs); | ||
| 134 | } | ||
| 135 | |||
| 136 | $encoded =~ qr/^[0-9]+$/ ? MF::INTEGER->new($encoded) | ||
| 137 | : $encoded =~ qr/^[0-9][0-9.e+\-]+$/ ? MF::FLOAT->new($encoded) | ||
| 138 | : MF::STRING->new(undef, $encoded); | ||
| 139 | } | ||
| 140 | |||
| 141 | #-------------------- | ||
| 142 | |||
| 143 | 1; |