Formula/Config/YAML.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 26 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 154 SCAR 54.8
CC
15
Cov
0%
_serialize
132.0
_unpack
30.0
save
2.0
CRAP 240.0
1 # Copyrights 2023 by [Mark Overmeer <markov@cpan.org>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.03.
5 package Math::Formula::Config::YAML;
6 use vars '$VERSION';
7 $VERSION = '0.16';
8  
9 use base 'Math::Formula::Config';
10  
11 use warnings;
12 use strict;
13  
14 use Log::Report 'math-formula';
15  
16 use YAML::XS qw/Dump Load/;
17 use boolean ();
18 use File::Slurper 'read_binary';
19  
20 # It is not possible to use YAML.pm, because it cannot produce output where
21 # boolean true and a string with content 'true' can be distinguished.
22  
23 use Scalar::Util 'blessed';
24  
25  
26 #----------------------
27  
28 sub save($%)
29 { my ($self, $context, %args) = @_;
30 my $name = $context->name;
31  
32 local $YAML::XS::Boolean = "boolean";
33 my $index = $context->_index;
34  
35 my $fn = $self->path_for($args{filename} || "$name.yml");
36 open my $fh, '>:encoding(utf8)', $fn
37 or fault __x"Trying to save context '{name}' to {fn}", name => $name, fn => $fn;
38  
39 $fh->print(Dump $self->_set($index->{attributes}));
40 $fh->print(Dump $self->_set($index->{formulas}));
41 $fh->print(Dump $self->_set($index->{fragments}));
42  
43 $fh->close
44 or fault __x"Error on close while saving '{name}' to {fn}", name => $name, fn => $fn;
45 }
46  
47 sub _set($)
48 { my ($self, $set) = @_;
49 my %data;
50 $data{$_ =~ s/^ctx_//r} = $self->_serialize($_, $set->{$_}) for keys %$set;
51 \%data;
52 }
53  
54 sub _serialize($$)
55 { my ($self, $name, $what) = @_;
56 my %attrs;
57  
58 if(blessed $what && $what->isa('Math::Formula'))
59 { if(my $r = $what->returns) { $attrs{returns} = $r };
60 $what = $what->expression;
61 }
62  
63 my $v = '';
64 if(blessed $what && $what->isa('MF::STRING'))
65 { $v = $what->value;
66 }
67 elsif(blessed $what && $what->isa('Math::Formula::Type'))
68 { $v = $what->isa('MF::INTEGER') || $what->isa('MF::FLOAT') ? $what->value
69 : $what->isa('MF::BOOLEAN') ? ($what->value ? boolean::true : boolean::false)
70 : '=' . $what->token;
71 }
72 elsif(ref $what eq 'CODE')
73 { warning __x"cannot (yet) save CODE, skipped '{name}'", name => $name;
74 return undef;
75 }
76 elsif(length $what)
77 { $v = '=' . $what;
78 }
79  
80 if(keys %attrs)
81 { $v .= '; ' . (join ', ', map "$_='$attrs{$_}'", sort keys %attrs);
82 }
83  
84 return $v;
85 }
86  
87  
88 sub load($%)
89 { my ($self, $name, %args) = @_;
90 my $fn = $self->path_for($args{filename} || "$name.yml");
91  
92 local $YAML::XS::Boolean = "boolean";
93 my ($attributes, $forms, $frags) = Load(read_binary $fn);
94  
95 my $attrs = $self->_set_decode($attributes);
96 Math::Formula::Context->new(name => $name,
97 %$attrs,
98 formulas => $self->_set_decode($forms),
99 );
100 }
101  
102 sub _set_decode($)
103 { my ($self, $set) = @_;
104 $set or return {};
105  
106 my %forms;
107 $forms{$_} = $self->_unpack($_, $set->{$_}) for keys %$set;
108 \%forms;
109 }
110  
111 sub _unpack($$)
112 { my ($self, $name, $encoded) = @_;
113 my $dummy = Math::Formula->new('dummy', '7');
114  
115 if(ref $encoded eq 'boolean')
116 { return MF::BOOLEAN->new(undef, $encoded);
117 }
118  
119 if($encoded =~ m/^\=(.*?)(?:;\s*(.*))?$/)
120 { my ($expr, $attrs) = ($1, $2 // '');
121 my %attrs = $attrs =~ m/(\w+)\='([^']+)'/g;
122 return Math::Formula->new($name, $expr =~ s/\\"/"/gr, %attrs);
123 }
124  
125 $encoded =~ qr/^[0-9]+$/ ? MF::INTEGER->new($encoded)
126 : $encoded =~ qr/^[0-9][0-9.e+\-]+$/ ? MF::FLOAT->new($encoded)
127 : MF::STRING->new(undef, $encoded);
128 }
129  
130 #----------------------
131  
132 1;