# `Cidr`

A range of IP addresses: `192.0.2.0/24`.

Use it for allow lists. `web.cidr_contains` tells you whether an [`IpAddress`](/docs/types/type-ip-address.md) is inside.

## Shape

One value, held as text in one set form, with no parts: `2001:db8:0:1::/64`.

## Example

```jadpo
Cidr("192.0.2.0/24")
```

`Cidr(raw)` checks the text, and text that isn't one breaks the `format` rule.

`text.parse(raw, as: Cidr)` reads one from text that comes from outside. 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`.

## Good to know

- It has to start at the range's first address, so `192.0.2.1/24` is refused.
- `==` tells you whether two are the same. `<` and `list.sort_by` don't take it.

## In JSON

A string, in a request, a response or `json.write`:

```json
"192.0.2.0/24"
"198.51.100.7/32"
"0.0.0.0/0"
```

## Details

<details>
<summary>Text form, storage, how it is shown, and operations</summary>

`Cidr` is in the group Web and network, and has been in Jadpo since 0.1.0.

### Text form

`text.from` writes one standard text form, the same form `text.parse` reads back: `192.0.2.0/24`, `198.51.100.7/32` and `0.0.0.0/0`.

### How it is stored

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'`.

### How it is 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.

It also takes `text.parse`, `text.from`, `text.format`, `json.read`, `json.write` and `==`. [How every standard type behaves](/docs/every-standard-type-meets-one-contract.md) says how they work.

</details>
