From 420c47e01c361a8d07175374dd456b74c263a9f5 Mon Sep 17 00:00:00 2001 From: Jeff Ober Date: Mon, 13 Jan 2020 11:26:50 -0500 Subject: [PATCH] Start docs --- README.pod | 55 ++++++++++++++++++++++++++++++ cpanfile | 8 +++-- lib/TOML/Tiny.pm | 87 ++++++++++++++++++++++++++++++++++++++++++++++++ t/parity.t | 4 +-- 4 files changed, 149 insertions(+), 5 deletions(-) diff --git a/README.pod b/README.pod index dd2287f..c65dc47 100644 --- a/README.pod +++ b/README.pod @@ -10,6 +10,61 @@ TOML::Tiny - a minimal, pure perl TOML parser and serializer version 0.01 +=head1 SYNOPSIS + + use TOML::Tiny qw(from_toml to_toml); + + binmode STDIN, ':encoding(UTF-8)'; + binmode STDOUT, ':encoding(UTF-8)'; + + # Decoding TOML + my $toml = do{ local $/; }; + my ($parsed, $error) = from_toml $toml; + + # Encoding TOML + say to_toml({ + stuff => { + about => ['other', 'stuff'], + }, + }); + + # Object API + my $parser = TOML::Tiny->new; + my $result = $parser->parse($toml); + +=head1 DESCRIPTION + +C implements a pure-perl parser and generator for the +L data format. It conforms to TOML +v5.0. + +C strives to maintain an interface compatible to the L and +L modules, and could even be used to override C<$TOML::Parser>: + + use TOML; + use TOML::Tiny; + + local $TOML::Parser = TOML::Tiny->new(...); + say to_toml(...); + +=head1 EXPORTS + +=head2 from_toml + +=head2 to_toml + +=head1 OBJECT API + +=head2 new + +=head2 decode + +=head2 encode + +=head2 parse + +=head1 COMPATIBILITY + =head1 AUTHOR Jeff Ober diff --git a/cpanfile b/cpanfile index 93bb50a..40ed767 100644 --- a/cpanfile +++ b/cpanfile @@ -1,12 +1,14 @@ requires 'perl' => '>= 5.018'; -requires 'Scalar::Util' => '>= 1.14'; -requires 'Data::Dumper' => '0'; + +requires 'Scalar::Util' => '>= 1.14'; +requires 'Data::Dumper' => '0'; +requires 'DateTime::Format::RFC3339' => '0'; recommends 'Types::Serialiser' => 0; on test => sub{ requires 'Data::Dumper' => '0'; - requires 'DateTime::Format::ISO8601' => '0'; + requires 'DateTime::Format::RFC3339' => '0'; requires 'TOML::Parser' => '0'; requires 'Test2::V0' => '0'; requires 'Test::Pod' => '0'; diff --git a/lib/TOML/Tiny.pm b/lib/TOML/Tiny.pm index 7ccee72..284a4ad 100644 --- a/lib/TOML/Tiny.pm +++ b/lib/TOML/Tiny.pm @@ -16,6 +16,9 @@ our @EXPORT = qw( to_toml ); +#------------------------------------------------------------------------------- +# TOML module compatibility +#------------------------------------------------------------------------------- sub from_toml { my $source = shift; my $parser = TOML::Tiny::Parser->new(@_); @@ -32,4 +35,88 @@ sub to_toml { goto \&TOML::Tiny::Writer::to_toml; } +#------------------------------------------------------------------------------- +# Object API +#------------------------------------------------------------------------------- +sub new { + my ($class, %param) = @_; + bless{ parser => TOML::Tiny::Parser->new(%param) }, $class; +} + +sub encode { + my ($self, $source) = @_; + $self->{parser}->parse; +} + +sub decode { + my ($self, $data) = @_; + TOML::Tiny::Writer::to_toml($data); +} + +#------------------------------------------------------------------------------- +# For compatibility with TOML::from_toml's use of $TOML::Parser +#------------------------------------------------------------------------------- +sub parse { + goto \&encode; +} + 1; + +=head1 SYNOPSIS + + use TOML::Tiny qw(from_toml to_toml); + + binmode STDIN, ':encoding(UTF-8)'; + binmode STDOUT, ':encoding(UTF-8)'; + + # Decoding TOML + my $toml = do{ local $/; }; + my ($parsed, $error) = from_toml $toml; + + # Encoding TOML + say to_toml({ + stuff => { + about => ['other', 'stuff'], + }, + }); + + # Object API + my $parser = TOML::Tiny->new; + my $result = $parser->parse($toml); + + +=head1 DESCRIPTION + +C implements a pure-perl parser and generator for the +L data format. It conforms to TOML +v5.0. + +C strives to maintain an interface compatible to the L and +L modules, and could even be used to override C<$TOML::Parser>: + + use TOML; + use TOML::Tiny; + + local $TOML::Parser = TOML::Tiny->new(...); + say to_toml(...); + + +=head1 EXPORTS + +=head2 from_toml + +=head2 to_toml + + +=head1 OBJECT API + +=head2 new + +=head2 decode + +=head2 encode + +=head2 parse + + +=head1 COMPATIBILITY diff --git a/t/parity.t b/t/parity.t index 29e00c8..3ae9a45 100644 --- a/t/parity.t +++ b/t/parity.t @@ -29,8 +29,8 @@ subtest 'TOML::Parser' => sub{ }; subtest 'inflate_datetime' => sub{ - require DateTime::Format::ISO8601; - my $inflate = sub{ DateTime::Format::ISO8601->parse_datetime(shift) }; + require DateTime::Format::RFC3339; + my $inflate = sub{ DateTime::Format::RFC3339->parse_datetime(shift) }; my $exp = TOML::Parser->new(inflate_datetime => $inflate)->parse($toml); my $got = TOML::Tiny::Parser->new(inflate_datetime => $inflate)->parse($toml); is $got, $exp, 'equivalence' -- 2.30.2