Improved README with media type creation documentation.

This commit is contained in:
Tamás Kálcza 2022-03-21 12:19:55 +01:00
parent 2e54cd7f5d
commit 735423e8b3
2 changed files with 54 additions and 2 deletions

View File

@ -2,5 +2,52 @@
## Overview
This library can be used to create [Media Type](https://www.iana.org/assignments/media-types/media-types.xhtml)s in a
type-safe manner.
MediaType is a library -- developed by [21Gram Consulting](https://21gram.consulting) -- that can be used to
create [Media Type](https://www.iana.org/assignments/media-types/media-types.xhtml)s in a type-safe manner.
Mainly intended -- although not limited -- to be used in [server-side Swift](https://www.swift.org/server)
applications.
## Creating Media Types
Media types are represented by the ``MediaType`` Swift type.
You can create a media type in a type-safe manner using one of the possible cases. You can also create media type
instances simply by using string literals.
```swift
MediaType.application(.json()) // is equivalent to
let mediaType: MediaType = "application/json"
```
It is also possible to create a ``MediaType`` instance from a string variable as shown in the following example.
```swift
let rawMediaType = "application/json"
let mediaType = MediaType(rawValue: rawMediaType)
```
### Suffixes and Parameters
Media type suffixes and parameters are supported both via string literals and ``MediaType`` cases.
```swift
MediaType.application(.atom(nil, ["charset": "utf-8"])) // is equivalent to
let mediaType: MediaType = "application/atom; charset=utf-8"
MediaType.application(.atom(.xml)) // is equivalent to
let mediaType: MediaType = "application/atom+xml"
MediaType.application(.atom(.xml, ["charset": "utf-8"])) // is equivalent to
let mediaType: MediaType = "application/atom+xml; charset=utf-8"
```
### Trees
You can create media type trees by using either the string literal syntax, or using the `other` case of a particular
media type.
```swift
MediaType.application(.other("vnd.efi.img")) // is equivalent to
"application/vnd.efi.img"
```

View File

@ -10,6 +10,11 @@ extension CodeExamples {
XCTAssertEqual("application/json", MediaType.application(.json()))
}
func test_media_type_from_string_variable() {
let rawMediaType = "application/json"
XCTAssertEqual(MediaType(rawValue: rawMediaType), MediaType.application(.json()))
}
func test_audio_media_type() {
XCTAssertEqual((MediaType.audio(.ac3())), "audio/ac3")
XCTAssertEqual((MediaType.audio(.mpeg())), "audio/mpeg")