record
ftd
supports record
types. These are also called struct
in some languages.record
record
syntax:-- record person: name: caption age: integer bio: optional body
person
. It has three fields: name
, age
and bio
.record
:-- person amitu: Amit Upadhyay age: 40
Here we have created a new variable of type person
, called it amitu
, and the
value of name
, since its declared as caption
in the record definition, is read
from the “caption” area, and age
is read from the “header”.
Note that we have not passed bio
, since bio
is declared as optional body
,
so it’s not a problem. Had it been body
the above would not have been valid.
record
in ftd
is equivalent of a struct
in Rust.ftd
file from Rust you will have to first create a struct
in Rust that is compatible with our person
definition:#[derive(serde::Deserialize)] struct Person { name: String, age: i32, bio: Option<String>, }
For each field in person
record, we have a corresponding field in our Person
struct
.
Note that we used age
as i32, but it could have been any type that can be
deserialised from JSON
Number since ftd
integer
is converted to JSON Number
.
ftd
crate to parse an ftd
file, and get data out of it:let doc = ftd::p2::Document::from("some/id", source, lib)?; let amitu: Person = doc.get("amitu")?;
ftd
files “Reading FTD Files” guide.ftd
also supports “anonymous” instances:-- person: Amit Upadhyay age: 40 This is the bio -- person: Shobhit Sharma age: 30
Note that we have not assigned these instances to any variable using the var
keyword. These are called “anonymous instances”.
These instances can be read from Rust using:
let doc = ftd::p2::Document::from("some/id", source, lib)?; let instances: Vec<Person> = doc.instances("person")?; // if you expect the document to contain only one instance: let instance: Person = doc.only_instance("person")?;
ftd
files.