# lawref.official_text

*module*

Точные байты официального текста фрагмента в CLIR (SPEC [§13](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/05-part-v-lexis-and-basic-syntax.ru.md#13-кодировка-и-нормализация), [§28](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/07-part-vii-source-and-document-model.ru.md#28-fragment), [§208](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/28-part-xxvii-canonical-legal-ir.ru.md#208-canonical-serialization); errata E-0248, DECISION-0375).

Канон [§208](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/28-part-xxvii-canonical-legal-ir.ru.md#208-canonical-serialization) требует NFC у каждой строки документа, [§13](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/05-part-v-lexis-and-basic-syntax.ru.md#13-кодировка-и-нормализация) — хранить исходные
байты официального text block. Оба требования выполняются двумя полями
элемента ``Fragment.texts[]``: ``text`` — NFC-форма, ``exactBytes`` — точные
байты в каноническом base64 RFC 4648 [§4](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/02-part-ii-goals-and-scope.ru.md#4-не-цели-01), присутствующие ТОЛЬКО когда они
отличаются от UTF-8 ``text``. Сравнение строк идёт по ``text``, проверки
целостности (content hash [§194](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/27-part-xxvi-static-semantics-and-diagnostics.ru.md#194-sourceprovenance-checks), вхождение в публикацию, text coverage [§33.1](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/07-part-vii-source-and-document-model.ru.md#331-formalization-coverage))
— по точным байтам.

Порт — ``law_canon::official_text`` в ``lawc``; причины отказа и их порядок
входят в байтовый контракт evaluation-документа ([§28](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/07-part-vii-source-and-document-model.ru.md#28-fragment)).

## lawref.official_text.EXACT_BYTES

*attribute* · *module attribute*

```python
EXACT_BYTES = 'exactBytes'
```

[View source](https://github.com-arxohq/arxo-io/law/blob/2edc2b92ce22b52e03f4081d2769a58229684379/engines/lawref/lawref/official_text.py#L22-L22)

## lawref.official_text.REASON_BASE64

*attribute* · *module attribute*

```python
REASON_BASE64 = 'не каноническая запись base64 RFC 4648 §4'
```

[View source](https://github.com-arxohq/arxo-io/law/blob/2edc2b92ce22b52e03f4081d2769a58229684379/engines/lawref/lawref/official_text.py#L25-L25)

## lawref.official_text.REASON_NFC

*attribute* · *module attribute*

```python
REASON_NFC = 'NFC байтов не равна text'
```

[View source](https://github.com-arxohq/arxo-io/law/blob/2edc2b92ce22b52e03f4081d2769a58229684379/engines/lawref/lawref/official_text.py#L27-L27)

## lawref.official_text.REASON_REDUNDANT

*attribute* · *module attribute*

```python
REASON_REDUNDANT = 'байты равны UTF-8 text — поле обязано отсутствовать'
```

[View source](https://github.com-arxohq/arxo-io/law/blob/2edc2b92ce22b52e03f4081d2769a58229684379/engines/lawref/lawref/official_text.py#L28-L28)

## lawref.official_text.REASON_UTF8

*attribute* · *module attribute*

```python
REASON_UTF8 = 'байты не UTF-8'
```

[View source](https://github.com-arxohq/arxo-io/law/blob/2edc2b92ce22b52e03f4081d2769a58229684379/engines/lawref/lawref/official_text.py#L26-L26)

## lawref.official_text.content_hash

*function*

```python
def content_hash(data: bytes) -> str
```

Digest [§194](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/27-part-xxvi-static-semantics-and-diagnostics.ru.md#194-sourceprovenance-checks) точных байтов официального текста.

[View source](https://github.com-arxohq/arxo-io/law/blob/2edc2b92ce22b52e03f4081d2769a58229684379/engines/lawref/lawref/official_text.py#L74-L76)

## lawref.official_text.encode

*function*

```python
def encode(raw: str) -> dict[str, str]
```

Поля ``text``/``exactBytes`` для исходного текста ``raw`` ([§13](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/05-part-v-lexis-and-basic-syntax.ru.md#13-кодировка-и-нормализация)).

``exactBytes`` есть только у текста, чьи байты отличаются от UTF-8 его
NFC-формы; у NFC-текста результат — ровно прежний ``{"text": raw}``.

[View source](https://github.com-arxohq/arxo-io/law/blob/2edc2b92ce22b52e03f4081d2769a58229684379/engines/lawref/lawref/official_text.py#L31-L41)

## lawref.official_text.exact_bytes

*function*

```python
def exact_bytes(text_item: dict) -> tuple[bytes | None, str | None]
```

Точные байты элемента ``texts[]`` либо причина незаконного носителя.

Возвращает ``(bytes, None)`` у законного элемента и ``(None, причина)``
у незаконного. Условия проверяются в порядке [§28](https://github.com/arxohq/law/blob/master/spec/SPEC.ru/07-part-vii-source-and-document-model.ru.md#28-fragment).

[View source](https://github.com-arxohq/arxo-io/law/blob/2edc2b92ce22b52e03f4081d2769a58229684379/engines/lawref/lawref/official_text.py#L44-L71)
