Create an entry

We'll now create a new Entry and all its constituent fields.

Prerequisites

A basic knowledge of the Rust programming language and executing commands in the terminal will be helpful for completing this tutorial. Some of the steps below also require cargo to be installed.

Additionally, knowledge of the Path API would be helpful. If you're not yet familiar, please see our dedicated tutorial for paths.

Setup

  1. Create a new directory on your filesystem and name it something like entry.
  2. Using your terminal, run cargo init within the newly created directory.
  3. After than, run cargo add willow25.

Create an entry

Open src/main.rs, delete its contents, and enter the following:

use willow25::prelude::*;

fn main() {
    let namespace_id = NamespaceId::from([0; NAMESPACE_ID_WIDTH]);
    let subspace_id = SubspaceId::from([1; SUBSPACE_ID_WIDTH]);

    let entry = Entry::builder()
        .namespace_id(namespace_id)
        .subspace_id(subspace_id)
        .path(path!("/blog/idea/1"))
        .now().unwrap()
        .payload(b"Dear reader, I've got a great idea")
        .build().unwrap();

    println!("{:#?}", entry);
}

In your terminal, run cargo run, and you should see the following output:

Entry {
    namespace_id: NamespaceId(
        +0000000000000000000000000000000000000000000000000000000000000000,
    ),
    subspace_id: SubspaceId(
        0101010101010101010101010101010101010101010101010101010101010101,
    ),
    path: Path(
        /blog/idea/1,
    ),
    timestamp: Timestamp(
        814676859787575,
    ),
    payload_length: 34,
    payload_digest: PayloadDigest(
        70a4247d8038f6022fa34bd205685ee443dfd1202b384cc440ecc86adf45e58b,
    ),
}

Create an Entry based off another one

Next, we'll create an Entry which is mostly identical to an earlier one.

Add the following tosrc/main.rs:

use willow25::prelude::*;

fn main() {
    let namespace_id = NamespaceId::from([0; NAMESPACE_ID_WIDTH]);
    let subspace_id = SubspaceId::from([1; SUBSPACE_ID_WIDTH]);

    let entry = Entry::builder()
        .namespace_id(namespace_id)
        .subspace_id(subspace_id)
        .path(path!("/blog/idea/1"))
        .now().unwrap()
        .payload(b"Dear reader, I've got a great idea")
        .build().unwrap();

    let oops = Entry::prefilled_builder(&entry)
        .path(path!("/blog/idea"))
        .timestamp(entry.timestamp() + 10.minutes())
        .payload(b"")
        .build().unwrap();

    println!("{:#?}", entry);
    println!("{:#?}", oops);
}

In your terminal, run cargo run, and you should see the following output:

Entry {
    namespace_id: NamespaceId(
        +0000000000000000000000000000000000000000000000000000000000000000,
    ),
    subspace_id: SubspaceId(
        0101010101010101010101010101010101010101010101010101010101010101,
    ),
    path: Path(
        /blog/idea/1,
    ),
    timestamp: Timestamp(
        814676859787575,
    ),
    payload_length: 34,
    payload_digest: PayloadDigest(
        70a4247d8038f6022fa34bd205685ee443dfd1202b384cc440ecc86adf45e58b,
    ),
}
Entry {
    namespace_id: NamespaceId(
        +0000000000000000000000000000000000000000000000000000000000000000,
    ),
    subspace_id: SubspaceId(
        0101010101010101010101010101010101010101010101010101010101010101,
    ),
    path: Path(
        /blog/idea,
    ),
    timestamp: Timestamp(
        814677459787575,
    ),
    payload_length: 0,
    payload_digest: PayloadDigest(
        3b638fc8f2fb68418325a36b4718ffb07de457ac301393a845466a79eea3286b,
    ),
}

Compare entries

Finally, we'll compare how the two Entries relate to each other.

Add the following tosrc/main.rs:

use willow25::prelude::*;

fn main() {
    let namespace_id = NamespaceId::from([0; NAMESPACE_ID_WIDTH]);
    let subspace_id = SubspaceId::from([1; SUBSPACE_ID_WIDTH]);

    let entry = Entry::builder()
        .namespace_id(namespace_id)
        .subspace_id(subspace_id)
        .path(path!("/blog/idea/1"))
        .now().unwrap()
        .payload(b"Dear reader, I've got a great idea")
        .build().unwrap();

    let oops = Entry::prefilled_builder(&entry)
        .path(path!("/blog/idea"))
        .timestamp(entry.timestamp() + 10.minutes())
        .payload(b"")
        .build().unwrap();

    println!("{:#?}", entry);
    println!("{:#?}", oops);

    assert_eq!(entry.namespace_id(), oops.namespace_id());
    assert_ne!(entry, oops);
    assert!(oops.is_newer_than(&entry));
    assert!(entry.is_pruned_by(&oops));
}

When running this code with cargo run, all assertions should pass!

Summary

In this tutorial we used the Entry API to create and compare entries: