Skip to main content

Rset

Struct Rset 

Source
pub struct Rset<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> Rset<'a>

Source

pub fn num_records(&self) -> usize

Examples found in repository?
examples/count_records.rs (line 12)
5fn main() {
6    let path = std::env::args().nth(1).expect("usage: count_records <file.rec>");
7    let text = fs::read_to_string(&path).expect("read file");
8
9    let mut db = Db::parse_str(&text).expect("parse");
10    let n_rsets = db.num_rsets();
11    let total: usize = (0..n_rsets)
12        .map(|i| db.rset_at(i).expect("rset by index").num_records())
13        .sum();
14    println!("{}: {} records across {} record set(s)", path, total, n_rsets);
15}
Source

pub fn descriptor(&self) -> Option<RecordRef<'_>>

The record-descriptor record (everything declared in the %rec: / %type: / %key: / %mandatory: block). None for record sets that have no descriptor.

Source

pub fn records(&self) -> Records<'_>

Examples found in repository?
examples/query_records.rs (line 23)
5fn main() {
6    let mut args = std::env::args().skip(1);
7    let path = args
8        .next()
9        .expect("usage: query_records <file.rec> <Type> <selection-expression>");
10    let rset_type = args.next().expect("missing record type");
11    let expr = args.next().expect("missing selection expression");
12
13    let text = fs::read_to_string(&path).expect("read file");
14    let mut db = Db::parse_str(&text).expect("parse");
15
16    let rset = db
17        .rset_by_type(&rset_type)
18        .unwrap_or_else(|| panic!("no record set of type {rset_type:?}"));
19    let selection_expression =
20        SelectionExpression::compile(&expr, false).expect("compile selection expression");
21
22    let mut matches = 0usize;
23    for record in rset.records().filter(|r| selection_expression.matches(r)) {
24        matches += 1;
25        for field in record.fields() {
26            println!("{}: {}", field.name(), field.value());
27        }
28        println!();
29    }
30    eprintln!("{matches} match(es)");
31}
More examples
Hide additional examples
examples/modify_records.rs (line 40)
15fn main() -> ExitCode {
16    let mut args = std::env::args().skip(1);
17    let path = args.next().unwrap_or_else(|| usage());
18    let rset_type = args.next().unwrap_or_else(|| usage());
19    let expr = args.next().unwrap_or_else(|| usage());
20    let action = args.next().unwrap_or_else(|| usage());
21
22    let text = fs::read_to_string(&path).expect("read file");
23    let mut db = Db::parse_str(&text).expect("parse");
24    let selection_expression =
25        SelectionExpression::compile(&expr, false).expect("compile selection expression");
26
27    let summary = {
28        let mut rset = db
29            .rset_by_type(&rset_type)
30            .unwrap_or_else(|| panic!("no record set of type {rset_type:?}"));
31
32        match action.as_str() {
33            "set" => {
34                let pair = args.next().unwrap_or_else(|| usage());
35                let (field, value) = pair
36                    .split_once('=')
37                    .unwrap_or_else(|| panic!("expected Field=Value, got {pair:?}"));
38                let mut updated = 0usize;
39                let mut missing = 0usize;
40                for mut record in rset.records().filter(|r| selection_expression.matches(r)) {
41                    match record.set_field(field, value).expect("set field") {
42                        true => updated += 1,
43                        false => missing += 1,
44                    }
45                }
46                format!(
47                    "set {field}={value} on {updated} record(s); {missing} matching record(s) had no such field"
48                )
49            }
50            "delete" => {
51                let removed = rset.remove_matching(|r| selection_expression.matches(r));
52                format!("deleted {removed} record(s)")
53            }
54            other => {
55                eprintln!("unknown action {other:?}");
56                return ExitCode::from(2);
57            }
58        }
59    };
60
61    let serialized = db.to_rec_string().expect("serialize");
62    fs::write(&path, serialized).expect("write file");
63    println!("{summary}");
64    ExitCode::SUCCESS
65}
Source

pub fn remove_matching<F>(&mut self, pred: F) -> usize
where F: FnMut(&RecordRef<'_>) -> bool,

Remove every record for which pred returns true. Returns the count of records removed.

Examples found in repository?
examples/modify_records.rs (line 51)
15fn main() -> ExitCode {
16    let mut args = std::env::args().skip(1);
17    let path = args.next().unwrap_or_else(|| usage());
18    let rset_type = args.next().unwrap_or_else(|| usage());
19    let expr = args.next().unwrap_or_else(|| usage());
20    let action = args.next().unwrap_or_else(|| usage());
21
22    let text = fs::read_to_string(&path).expect("read file");
23    let mut db = Db::parse_str(&text).expect("parse");
24    let selection_expression =
25        SelectionExpression::compile(&expr, false).expect("compile selection expression");
26
27    let summary = {
28        let mut rset = db
29            .rset_by_type(&rset_type)
30            .unwrap_or_else(|| panic!("no record set of type {rset_type:?}"));
31
32        match action.as_str() {
33            "set" => {
34                let pair = args.next().unwrap_or_else(|| usage());
35                let (field, value) = pair
36                    .split_once('=')
37                    .unwrap_or_else(|| panic!("expected Field=Value, got {pair:?}"));
38                let mut updated = 0usize;
39                let mut missing = 0usize;
40                for mut record in rset.records().filter(|r| selection_expression.matches(r)) {
41                    match record.set_field(field, value).expect("set field") {
42                        true => updated += 1,
43                        false => missing += 1,
44                    }
45                }
46                format!(
47                    "set {field}={value} on {updated} record(s); {missing} matching record(s) had no such field"
48                )
49            }
50            "delete" => {
51                let removed = rset.remove_matching(|r| selection_expression.matches(r));
52                format!("deleted {removed} record(s)")
53            }
54            other => {
55                eprintln!("unknown action {other:?}");
56                return ExitCode::from(2);
57            }
58        }
59    };
60
61    let serialized = db.to_rec_string().expect("serialize");
62    fs::write(&path, serialized).expect("write file");
63    println!("{summary}");
64    ExitCode::SUCCESS
65}
Source

pub fn append_record(&mut self, record: Record) -> Result<(), Error>

Examples found in repository?
examples/append_record.rs (line 31)
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}

Auto Trait Implementations§

§

impl<'a> Freeze for Rset<'a>

§

impl<'a> RefUnwindSafe for Rset<'a>

§

impl<'a> !Send for Rset<'a>

§

impl<'a> !Sync for Rset<'a>

§

impl<'a> Unpin for Rset<'a>

§

impl<'a> UnsafeUnpin for Rset<'a>

§

impl<'a> !UnwindSafe for Rset<'a>

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.