std/uri/data
std/uri/src/data.trb
DataUri: a data: URI read into the parts RFC 2397 gives it - a media type, its parameters and the bytes the URI
carries in place (docs/design/URI.md section 7).
type DataParameter
type DataParameter with Show, Equals, Hash
One ;name=value of a data: URI's media type, as RFC 2397 section 3 writes them.
type DataUri
type DataUri with Show, Equals, Hash, Compare
A data: URI per RFC 2397: data:[<media type>][;<name>=<value>]*[;base64],<data>, with the data the bytes the URI
carries - percent-decoded, or Base64-decoded when the ;base64 flag ends the header.
Everything is checked when the value is made, including that a Base64 body decodes, so DataUri.bytes cannot fail.
==, hash() and compare() are over the Uri it was read from, as written: data:,a and data:text/plain,a are
two values that carry the same bytes, and the bytes are not kept a second time.
A ? in the data belongs to the data (RFC 2397 has no query); a # starts the fragment, which is not data.
Examples
const uri: Uri = "data:text/plain;charset=utf-8;base64,SGVsbG8="
const data = DataUri.tryFrom(uri)?
print "{data.mediaType()} {data.charset()} {data.bytes()}"
fn uri
fn uri(): Uri
The URI this value is.
fn mediaType
fn mediaType(): String
The media type, lower case as type/subtype: text/plain when the header leaves it out.
fn parameters
fn parameters(): List<DataParameter>
The parameters of the media type in the order they were written, without the ;base64 flag.
fn charset
fn charset(): String
The value of the charset parameter, US-ASCII when there is none (RFC 2397 section 3).
fn isBase64
fn isBase64(): Bool
Whether the data is Base64 (the header ends in ;base64) and not percent-encoded.
fn bytes
fn bytes(): Bytes
The bytes the URI carries, percent-decoded or Base64-decoded. They are not text until the charset says so.
fn fragment
fn fragment(): String?
The fragment: what follows #, which is not part of the data.
fn show
fn show(): String
The URI as its URI shows it.
fn compare
fn compare(other: Self): Ordering
In the order of the URIs.
fn encoded
static fn encoded(bytes: Bytes, mediaType: String = "text/plain"): Result<Self, UriError>
The data: URI that carries bytes as standard, padded Base64. mediaType is the header as it is written after
data:, so it may carry parameters: text/plain;charset=utf-8.
Errors
UriError.NotADatawheremediaTypeis not a media type with parameters.
extend DataUri with TryFrom<Uri, UriError>
extend DataUri with TryFrom<Uri, UriError>
A URI read as a data: URI (RFC 2397 section 3): no authority, a ,, a header of an optional media type that is
type/subtype, parameters that are each name=value, and a last base64 flag, and data that is valid Base64 when
the flag is set.
Errors
UriError.NotADatanaming the part that is not RFC 2397's.
fn tryFrom
static fn tryFrom(value: Uri): Result<Self, UriError>
extend Uri with From<DataUri>
extend Uri with From<DataUri>
The URI a data: URI is.
fn from
static fn from(value: DataUri): Self