68 lines
1.4 KiB
Rust
68 lines
1.4 KiB
Rust
|
|
use crate::{
|
||
|
|
message::wrapper::{InternalRecord, InternalRecords, Oid, Record},
|
||
|
|
name::{Name, Names},
|
||
|
|
};
|
||
|
|
|
||
|
|
#[derive(Clone, Debug)]
|
||
|
|
pub struct Records {
|
||
|
|
names: Names,
|
||
|
|
data: InternalRecords,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Records {
|
||
|
|
pub fn new(names: Names) -> Self {
|
||
|
|
Self {
|
||
|
|
names: names,
|
||
|
|
data: InternalRecords::new(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn with_data(names: Names, records: InternalRecords) -> Self {
|
||
|
|
Self {
|
||
|
|
names: names,
|
||
|
|
data: records,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn insert(&mut self, oid: Oid, record: InternalRecord) -> Option<InternalRecord> {
|
||
|
|
self.data.insert(oid, record)
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn len(&self) -> usize {
|
||
|
|
self.data.len()
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn iter(&self) -> impl Iterator<Item = Record> {
|
||
|
|
RecordIter::new(self)
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn get_internal_records(&self) -> &InternalRecords {
|
||
|
|
&self.data
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
struct RecordIter {
|
||
|
|
names: Names,
|
||
|
|
recs: Vec<InternalRecord>,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl RecordIter {
|
||
|
|
fn new(records: &Records) -> Self {
|
||
|
|
Self {
|
||
|
|
names: records.names.clone(),
|
||
|
|
recs: records.data.values().cloned().collect(),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Iterator for RecordIter {
|
||
|
|
type Item = Record;
|
||
|
|
|
||
|
|
fn next(&mut self) -> Option<Self::Item> {
|
||
|
|
match self.recs.pop() {
|
||
|
|
Some(rec) => Some(Record::with_data(self.names.clone(), rec.clone())),
|
||
|
|
None => None,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|