<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Specio::Library::Structured;

use strict;
use warnings;

our $VERSION = '0.42';

use parent 'Specio::Exporter';

use B ();
use Carp qw( confess );
use List::Util ();
use Scalar::Util qw( blessed );
use Specio::Constraint::Structurable;
use Specio::Declare;
use Specio::Library::Builtins;
use Specio::Library::Structured::Dict;
use Specio::Library::Structured::Map;
use Specio::Library::Structured::Tuple;
use Specio::TypeChecks qw( does_role );

## no critic (Variables::ProtectPrivateVars)
declare(
    'Dict',
    type_class =&gt; 'Specio::Constraint::Structurable',
    parent     =&gt; Specio::Library::Structured::Dict-&gt;parent,
    inline     =&gt; \&amp;Specio::Library::Structured::Dict::_inline,
    parameterization_args_builder =&gt;
        \&amp;Specio::Library::Structured::Dict::_parameterization_args_builder,
    name_builder =&gt; \&amp;Specio::Library::Structured::Dict::_name_builder,
    structured_inline_generator =&gt;
        \&amp;Specio::Library::Structured::Dict::_structured_inline_generator,
);

declare(
    'Map',
    type_class =&gt; 'Specio::Constraint::Structurable',
    parent     =&gt; Specio::Library::Structured::Map-&gt;parent,
    inline     =&gt; \&amp;Specio::Library::Structured::Map::_inline,
    parameterization_args_builder =&gt;
        \&amp;Specio::Library::Structured::Map::_parameterization_args_builder,
    name_builder =&gt; \&amp;Specio::Library::Structured::Map::_name_builder,
    structured_inline_generator =&gt;
        \&amp;Specio::Library::Structured::Map::_structured_inline_generator,
);

declare(
    'Tuple',
    type_class =&gt; 'Specio::Constraint::Structurable',
    parent     =&gt; Specio::Library::Structured::Tuple-&gt;parent,
    inline     =&gt; \&amp;Specio::Library::Structured::Tuple::_inline,
    parameterization_args_builder =&gt;
        \&amp;Specio::Library::Structured::Tuple::_parameterization_args_builder,
    name_builder =&gt; \&amp;Specio::Library::Structured::Tuple::_name_builder,
    structured_inline_generator =&gt;
        \&amp;Specio::Library::Structured::Tuple::_structured_inline_generator,
);
## use critic

sub optional {
    return { optional =&gt; shift };
}

sub slurpy {
    return { slurpy =&gt; shift };
}

## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
sub _also_export {qw( optional slurpy )}
## use critic

1;

# ABSTRACT: Structured types for Specio (Dict, Map, Tuple)

__END__

=pod

=encoding UTF-8

=head1 NAME

Specio::Library::Structured - Structured types for Specio (Dict, Map, Tuple)

=head1 VERSION

version 0.42

=head1 SYNOPSIS

    use Specio::Library::Builtins;
    use Specio::Library::String;
    use Specio::Library::Structured;

    my $map = t(
        'Map',
        of =&gt; {
            key   =&gt; t('NonEmptyStr'),
            value =&gt; t('Int'),
        },
    );
    my $tuple = t(
        'Tuple',
        of =&gt; [ t('Str'), t('Num') ],
    );
    my $dict = t(
        'Dict',
        of =&gt; {
            kv =&gt; {
                name =&gt; t('Str'),
                age  =&gt; t('Int'),
            },
        },
    );

=head1 DESCRIPTION

B&lt;This particular library should be considered in an alpha state. The syntax
for defining structured types may change, as well as some of the internals of
its implementation.&gt;

This library provides a set of structured types for Specio, C&lt;Dict&gt;, C&lt;Map&gt;,
and C&lt;Tuple&gt;. This library also exports two helper subs used for some types,
C&lt;optional&gt; and C&lt;slurpy&gt;.

All structured types are parameterized by calling C&lt;&lt; t( 'Type Name', of =&gt;
... ) &gt;&gt;. The arguments passed after C&lt;of&gt; vary for each type.

=head2 Dict

A C&lt;Dict&gt; is a hashref with a well-defined set of keys and types for those
key.

The argument passed to C&lt;of&gt; should be a single hashref. That hashref must
contain a C&lt;kv&gt; key defining the expected keys and the types for their
values. This C&lt;kv&gt; value is itself a hashref. If a key/value pair is optional,
use C&lt;optional&gt; around the I&lt;type&gt; for that key:

    my $person = t(
        'Dict',
        of =&gt; {
            kv =&gt; {
                first  =&gt; t('NonEmptyStr'),
                middle =&gt; optional( t('NonEmptyStr') ),
                last   =&gt; t('NonEmptyStr'),
            },
        },
    );

If a key is optional, then it can be omitted entirely, but if it passed then
it's type will be checked, so it cannot just be set to C&lt;undef&gt;.

You can also pass a C&lt;slurpy&gt; key. If this is passed, then the C&lt;Dict&gt; will
allow other, unknown keys, as long as they match the specified type:

    my $person = t(
        'Dict',
        of =&gt; {
            kv =&gt; {
                first  =&gt; t('NonEmptyStr'),
                middle =&gt; optional( t('NonEmptyStr') ),
                last   =&gt; t('NonEmptyStr'),
            },
            slurpy =&gt; t('Int'),
        },
    );

=head2 Map

A C&lt;Map&gt; is a hashref with specified types for its keys and values, but no
well-defined key names.

The argument passed to C&lt;of&gt; should be a single hashref with two keys, C&lt;key&gt;
and C&lt;value&gt;. The type for the C&lt;key&gt; will typically be some sort of key, but
if you're using a tied hash or an object with hash overloading it could
conceivably be any sort of value.

=head2 Tuple

A C&lt;Tuple&gt; is an arrayref with a fixed set of members in a specific order.

The argument passed to C&lt;of&gt; should be a single arrayref consisting of
types. You can mark a slot in the C&lt;Tuple&gt; as optional by wrapping the type in
a call to C&lt;optional&gt;:

    my $record = t(
        'Tuple',
        of =&gt; [
            t('PositiveInt'),
            t('Str'),
            optional( t('Num') ),
            optional( t('Num') ),
        ],
    );

You can have as many C&lt;optional&gt; elements as you want, but they must always
come in sequence at the end of the tuple definition. You cannot interleave
required and optional elements.

You can also make the Tuple accept an arbitrary number of values by wrapping
the last type in a call to C&lt;slurpy&gt;:

    my $record = t(
        'Tuple',
        of =&gt; [
            t('PositiveInt'),
            t('Str'),
            slurpy( t('Num') ),
        ],
    );

In this case, the C&lt;Tuple&gt; will require the first two elements and then allow
any number (including zero) of C&lt;Num&gt; elements.

You cannot mix C&lt;optional&gt; and C&lt;slurpy&gt; in a C&lt;Tuple&gt; definition.

=for Pod::Coverage optional slurpy

=head1 LIMITATIONS

Currently all structured types require that the types they are structured with
can be inlined. This may change in the future, but inlining all your types is
a really good idea, so you should do that anyway.

=head1 SUPPORT

Bugs may be submitted at L&lt;https://github.com/houseabsolute/Specio/issues&gt;.

I am also usually active on IRC as 'autarch' on C&lt;irc://irc.perl.org&gt;.

=head1 SOURCE

The source code repository for Specio can be found at L&lt;https://github.com/houseabsolute/Specio&gt;.

=head1 AUTHOR

Dave Rolsky &lt;autarch@urth.org&gt;

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2012 - 2017 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

The full text of the license can be found in the
F&lt;LICENSE&gt; file included with this distribution.

=cut
</pre></body></html>