Swift: Deconstruct SPF an Outline and Background
Tags: swift, beginners, spf, package
Published: 2021-05-17 03:19:40

# What is SPF
If you have not followed my rust series on deconstructing SPF you can check it out [here](/series/learning-rust/).  
  
Here is some back ground on SPF.
- [What is SPF](https://www.easy365manager.com/what-is-spf-sender-policy-framework/)
- [SPF Record syntax](https://dmarcian.com/spf-syntax-table/)

## What is a qualifier
A qualifier changes the way that a mechanism is interpreted, they are
- `+` Pass
- `-` Fail
- `~` SoftFail
- `?` Neutral
  - Neutral is the default if no qualifier is matched.
 
## What is a mechanism
An SPF mechanism can be one of several types
- Include
- A
- Mx
- Ip4
- Ip6
- All
- Ptr
- Exists
- Redirect (Technially this is a Modifier, but I will be using it as a Mechanism)


I will be attempting to just learn some of the basics of the Swift language. So I will be looking at things like:
- enum
- struct
- Optional variables
- Perhaps more if I find that I need them.

## Swift: The building blocks

### Qualifiers
So I need to have a way to handle `qualifiers`

```swift
enum Qualifier: String {
    case Pass = "+"
    case Fail = "-"
    case SoftFail = "~"
    case Neutral = "?"
    case None = ""
}
```

This enumeration will manage my qualifiers. I have added a `None` for the moment since in many cases, there is no qualifier provided, and I think I might want to be able to reproduce the mechanism in the future.

### Mechanism
To handle these I will create a second enumeration  
```swift
enum MechanismKind {
    case Redirect, Include, A, MX, Ip4, Ip6, All;
}
```
Although `Redirect` is a modifier, I am listing it here as a mechanism. I am also omitting `ptr` and `exists`. These may or may not be handled.

A mechanism is made up of a `qualifier` and some `mechanism` value. It also needs to have a kind. So a struct will probably work here.

```swift
struct Mechanism {
    var kind: MechanismKind;
    var qualifier: Qualifier;
    var mechanism: String;
}
```

#### Examples

Let's say we have an SPF record of the following type:
```text
"v=spf1 a ip4:x.x.x.x ~all"
```
We want to have a struct that will represent `A` and another that represents `Ip4`

These are conceptual examples only.

##### A
```swift
struct {
  kind => MechanismKind.A
  qualifier => Qualifers.None
  mechanism => ""
}
```

##### Ip4
```swift
struct {
  kind => MechanismKind.Ip4
  qualifier => None
  mechanism => "x.x.x.x"
}
```

Seeing this I can see that there will be some good cases to make some variables `Optional`

### SPF
This struct will probably look something like what follows.
```swift
struct SPF {
    var redirect: Mechanism?;
    var is_redirect: Bool = false;
    var include: Mechanism?;
    var a: Mechanism?;
    var mx: Mechanism?;
    var all: Mechanism?;
}
```
This is merely a rough outline. Most will need to be a list of records.
You might notice the use if the `?` at the end of `Mechanism`. This denotes that the variable is optional and hence can have a `nil` value. That meaning it can be absent.  
I refer you to the [Swift Documentation](https://developer.apple.com/documentation/swift/optional)

In the next article we will start to develop some code focused on the struct for a `Mechanism`