On this page· 5
Rust quickstart
- file format 6
- Rust SDK 1.0 preview
Get the Rust SDK from the Cintilé registry, then run one program that writes a pack with three records, opens it, prints the records, and saves the pack as sample.plxi.
Sign in at
cr.cintile.io(available on request), the Cintilé container registry, and create an access token for Cargo.Checking a pack needs only standard tools. See Check a pack.
Tell Cargo where the registry is. Add these lines to
config.tomlin Cargo’s home folder,$CARGO_HOME:config.toml[registries.cintile] index = "sparse+https://cr.cintile.io/cargo/index/" credential-provider = ["cargo:token"]Give Cargo the token. It asks for it once and keeps it:
Terminalcargo login --registry cintileMake a new package with
cargo new my-pack. Cargo reads the registry lines from.cargoin your home folder, so the package needs only the SDK in itsCargo.toml, under[dependencies]:Terminalcargo new my-packCargo.toml[dependencies] plxi-sdk = { version = "1.0.0-rc2", registry = "cintile" }Put the program below in
src/main.rsand run it withcargo run. Cargo fetches the SDK from the registry on the first build.
Needs the Rust SDK
// Write a pack in memory, open it, and read the records back.
use plxi_sdk::{write, OpenOptions, Pack, Record};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// A record is one JSON object with a string member "k" (its kind).
let lines = [
r#"{"k":"rel","src":"e1","tgt":"e2","kind":"knows"}"#,
r#"{"k":"ent","id":"e2","t":"Person"}"#,
r#"{"k":"ent","id":"e1","t":"Person","name":"Ada","salience":0.5}"#,
];
let records = lines
.iter()
.map(|l| Record::from_json_line(l))
.collect::<Result<Vec<_>, _>>()?;
// `write` returns the whole pack as bytes. No file is touched.
let bytes = write(&records, None)?;
// Opening verifies the pack by default (digest, header, record count).
let pack = Pack::from_bytes(bytes, OpenOptions::default())?;
assert_eq!(pack.record_count(), 3);
assert!(!pack.has_appendix());
// Records come back in the format's sort order (entities before
// relations), spelled the way the writer emits them.
let out: Vec<String> = pack
.records()
.map(|r| r.map(|rec| rec.json_line().to_string()))
.collect::<Result<_, _>>()?;
assert_eq!(
out,
[
concat!(
r#"{"k":"ent","v":1,"id":"e1","t":"Person","#,
r#""name":"Ada","salience":0.5,"stub":false}"#
),
r#"{"k":"ent","v":1,"id":"e2","t":"Person","stub":false}"#,
r#"{"k":"rel","v":1,"src":"e1","tgt":"e2","kind":"knows"}"#,
]
);
for line in &out {
println!("{line}");
}
println!("sha256 = {}", pack.sha256_hex());
// Keep a copy on disk, for checking with standard tools.
std::fs::write("sample.plxi", pack.to_bytes())?;
println!("wrote sample.plxi, {} bytes", pack.to_bytes().len());
Ok(())
}
{"k":"ent","v":1,"id":"e1","t":"Person","name":"Ada","salience":0.5,"stub":false}
{"k":"ent","v":1,"id":"e2","t":"Person","stub":false}
{"k":"rel","v":1,"src":"e1","tgt":"e2","kind":"knows"}
sha256 = 068f161e337da4b2b0c319a3539b9c62f4b15715d0f3c4de8e1664e5ef66bb28
wrote sample.plxi, 544 bytesRan with cargo run · exit status 0
- Order. The relation went in first and came out last. A writer emits records in sort-key order, and entities sort before relations.
- Spelling. Each line gained
"v":1, the record's own format version, and each entity gained"stub":false, a member the writer always emits. - The digest. The same records give the same bytes, so the same digest:
068f161e…bb28on every run. - The file. The last line is the size of
sample.plxi, the pack written to disk.
- 1. The steps above were followed on Linux, in a new package made with
cargo new my-pack. - 2. In place of the registry, Cargo built
plxi-sdkfrom a copy of its source on the same machine; its build line beganCompiling plxi-sdk v1.0.0-rc2. The dependency line inCargo.tomlstayed as step 4 shows it. - 3. The program was saved as
src/main.rsand run withcargo run.
sample.plxi is the pack used on the no-SDK page. Its digest can be recomputed there with sha256sum. Check a pack with standard tools