# `Cidr`

An address range (RFC 4632 and RFC 4291): its first address as `IpAddress` writes it, `/` and the prefix's length, every bit after the prefix zero: `192.0.2.0/24`, `2001:db8::/32`.

[Web and network](types.md#web-and-network); not ordered; since Jadpo 0.1.0.

## Make one

Build one from its text, `Cidr("192.0.2.0/24")`, which checks it: text that isn't one breaks `format`.

`text.parse(raw, as: Cidr)` reads one from its text; text that isn't one at all fails `malformed`, such as `192.0.2.1/24`, `192.0.2.0/33` or `192.0.2.0`.

## Text, JSON and storage

`text.from` writes its canonical text, the one form `text.parse` reads back. JSON sends its canonical text as a string.

| Text | JSON |
|---|---|
| `192.0.2.0/24` | `"192.0.2.0/24"` |
| `198.51.100.7/32` | `"198.51.100.7/32"` |
| `0.0.0.0/0` | `"0.0.0.0/0"` |
| `2001:db8::/32` | `"2001:db8::/32"` |
| `2001:db8:0:1::/64` | `"2001:db8:0:1::/64"` |

It is stored in one column named for the field, of type `text`. `0.0.0.0/0` is stored as `'0.0.0.0/0'`.

## Shown to people

`text.format(value, locale:)` shows it to people in their locale.

With `locale: Locale.en_gb`:

| Value | Shown |
|---|---|
| `192.0.2.0/24` | `192.0.2.0/24` |
| `198.51.100.7/32` | `198.51.100.7/32` |
| `0.0.0.0/0` | `0.0.0.0/0` |
| `2001:db8::/32` | `2001:db8::/32` |
| `2001:db8:0:1::/64` | `2001:db8:0:1::/64` |

## Operations

- `web.cidr_contains(range: Cidr, address: IpAddress) -> Bool`: Whether `address` is inside `range`. An IPv4 address is never inside an IPv6 range, nor the other way round.

Like every standard type, it also has the contract's own operations: `text.parse`, `text.from`, `text.format`, `json.read`, `json.write` and `==`. It isn't ordered, so `<` and sorting don't take it. [Every standard type meets one contract](../topics/every-standard-type-meets-one-contract.md) says how they work.
