Skip to main content

Record

Struct Record 

Source
pub struct Record { /* private fields */ }
Expand description

A freshly-built record. Owns its rec_record_t until passed to crate::Rset::append_record, at which point ownership is transferred to the containing record set.

Implementations§

Source§

impl Record

Source

pub fn new() -> Self

Examples found in repository?
examples/write_rec.rs (line 14)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let mut db = Db::new();
11
12    let mut rset = OwnedRset::new();
13
14    let mut descriptor = Record::new();
15    descriptor.append_field("%rec", "Book")?;
16    descriptor.append_field("%type", "Year int")?;
17    descriptor.append_field("%mandatory", "Title")?;
18    rset.set_descriptor(descriptor);
19
20    for (title, author, year) in [
21        ("Refactoring", "Martin Fowler", "1999"),
22        ("Domain-Driven Design", "Eric Evans", "2003"),
23        ("Test-Driven Development", "Kent Beck", "2002"),
24    ] {
25        let mut record = Record::new();
26        record.append_field("Title", title)?;
27        record.append_field("Author", author)?;
28        record.append_field("Year", year)?;
29        rset.append_record(record)?;
30    }
31
32    db.append_rset(rset)?;
33    println!("{}", db.to_rec_string()?);
34    Ok(())
35}
More examples
Hide additional examples
examples/append_record.rs (line 22)
5fn main() {
6    let mut args = std::env::args().skip(1);
7    let path = args
8        .next()
9        .expect("usage: append_record <file.rec> <Type> Field=Value [Field=Value ...]");
10    let rset_type = args.next().expect("missing record type");
11    let pairs: Vec<(String, String)> = args
12        .map(|a| {
13            let (k, v) = a.split_once('=').expect("expected Field=Value");
14            (k.to_string(), v.to_string())
15        })
16        .collect();
17    assert!(!pairs.is_empty(), "need at least one Field=Value");
18
19    let text = fs::read_to_string(&path).expect("read file");
20    let mut db = Db::parse_str(&text).expect("parse");
21
22    let mut record = Record::new();
23    for (k, v) in &pairs {
24        record.append_field(k, v).expect("append field");
25    }
26
27    {
28        let mut rset = db
29            .rset_by_type(&rset_type)
30            .unwrap_or_else(|| panic!("no record set of type {rset_type:?}"));
31        rset.append_record(record).expect("append record");
32    }
33
34    let serialized = db.to_rec_string().expect("serialize");
35    fs::write(&path, serialized).expect("write file");
36    println!("appended 1 record to rset {rset_type:?} in {path}");
37}
Source

pub fn append_field(&mut self, name: &str, value: &str) -> Result<(), Error>

Examples found in repository?
examples/write_rec.rs (line 15)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let mut db = Db::new();
11
12    let mut rset = OwnedRset::new();
13
14    let mut descriptor = Record::new();
15    descriptor.append_field("%rec", "Book")?;
16    descriptor.append_field("%type", "Year int")?;
17    descriptor.append_field("%mandatory", "Title")?;
18    rset.set_descriptor(descriptor);
19
20    for (title, author, year) in [
21        ("Refactoring", "Martin Fowler", "1999"),
22        ("Domain-Driven Design", "Eric Evans", "2003"),
23        ("Test-Driven Development", "Kent Beck", "2002"),
24    ] {
25        let mut record = Record::new();
26        record.append_field("Title", title)?;
27        record.append_field("Author", author)?;
28        record.append_field("Year", year)?;
29        rset.append_record(record)?;
30    }
31
32    db.append_rset(rset)?;
33    println!("{}", db.to_rec_string()?);
34    Ok(())
35}
More examples
Hide additional examples
examples/append_record.rs (line 24)
5fn main() {
6    let mut args = std::env::args().skip(1);
7    let path = args
8        .next()
9        .expect("usage: append_record <file.rec> <Type> Field=Value [Field=Value ...]");
10    let rset_type = args.next().expect("missing record type");
11    let pairs: Vec<(String, String)> = args
12        .map(|a| {
13            let (k, v) = a.split_once('=').expect("expected Field=Value");
14            (k.to_string(), v.to_string())
15        })
16        .collect();
17    assert!(!pairs.is_empty(), "need at least one Field=Value");
18
19    let text = fs::read_to_string(&path).expect("read file");
20    let mut db = Db::parse_str(&text).expect("parse");
21
22    let mut record = Record::new();
23    for (k, v) in &pairs {
24        record.append_field(k, v).expect("append field");
25    }
26
27    {
28        let mut rset = db
29            .rset_by_type(&rset_type)
30            .unwrap_or_else(|| panic!("no record set of type {rset_type:?}"));
31        rset.append_record(record).expect("append record");
32    }
33
34    let serialized = db.to_rec_string().expect("serialize");
35    fs::write(&path, serialized).expect("write file");
36    println!("appended 1 record to rset {rset_type:?} in {path}");
37}

Trait Implementations§

Source§

impl Default for Record

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Record

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.