Moved Records into the document module.
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after 1s
This commit is contained in:
67
src/document/record.rs
Normal file
67
src/document/record.rs
Normal file
@@ -0,0 +1,67 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user