import _decimal
import struct


def decimal(value):
    """Encode a decimal.Decimal value.

    :param decimal.Decimal value: Value to encode
    :rtype: bytes

    """
    if not isinstance(value, _decimal.Decimal):
        raise TypeError('decimal.Decimal type required')
    tmp = '%s' % value
    if '.' in tmp:
        decimals = len(tmp.split('.')[-1])
        value = value.normalize()
        raw = int(value * (_decimal.Decimal(10)**decimals))
        return struct.pack('>Bi', decimals, raw)
    return struct.pack('>Bi', 0, int(value))