Skip to content

Commit 9af587a

Browse files
committed
provenance API update
1 parent 20f620e commit 9af587a

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/provenance.rs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! See [`Provenance`] for examples.
1212
1313
use crate::bindings as ll_bindings;
14-
use crate::{tsk_id_t, tsk_size_t, TskitError};
14+
use crate::{tsk_id_t, tsk_size_t, ProvenanceId, TskitError};
1515

1616
/// Enable provenance table access.
1717
///
@@ -114,7 +114,7 @@ pub trait Provenance: crate::TableAccess {
114114
/// Row of a [`ProvenanceTable`].
115115
pub struct ProvenanceTableRow {
116116
/// The row id
117-
pub id: tsk_id_t,
117+
pub id: ProvenanceId,
118118
/// ISO-formatted time stamp
119119
pub timestamp: String,
120120
/// The provenance record
@@ -127,6 +127,12 @@ impl PartialEq for ProvenanceTableRow {
127127
}
128128
}
129129

130+
impl std::fmt::Display for ProvenanceId {
131+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
132+
write!(f, "ProvenanceId({})", self.0)
133+
}
134+
}
135+
130136
impl std::fmt::Display for ProvenanceTableRow {
131137
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
132138
write!(
@@ -140,7 +146,7 @@ impl std::fmt::Display for ProvenanceTableRow {
140146
fn make_provenance_table_row(table: &ProvenanceTable, pos: tsk_id_t) -> Option<ProvenanceTableRow> {
141147
if pos < table.num_rows() as tsk_id_t {
142148
Some(ProvenanceTableRow {
143-
id: pos,
149+
id: pos.into(),
144150
timestamp: table.timestamp(pos).unwrap(),
145151
record: table.record(pos).unwrap(),
146152
})
@@ -203,9 +209,9 @@ impl<'a> ProvenanceTable<'a> {
203209
/// # Errors
204210
///
205211
/// [`TskitError::IndexError`] if `r` is out of range.
206-
pub fn timestamp(&'a self, row: tsk_id_t) -> Result<String, TskitError> {
212+
pub fn timestamp<P: Into<ProvenanceId> + Copy>(&'a self, row: P) -> Result<String, TskitError> {
207213
match unsafe_tsk_ragged_char_column_access!(
208-
row,
214+
row.into().0,
209215
0,
210216
self.num_rows(),
211217
self.table_.timestamp,
@@ -226,9 +232,9 @@ impl<'a> ProvenanceTable<'a> {
226232
/// # Errors
227233
///
228234
/// [`TskitError::IndexError`] if `r` is out of range.
229-
pub fn record(&'a self, row: tsk_id_t) -> Result<String, TskitError> {
235+
pub fn record<P: Into<ProvenanceId> + Copy>(&'a self, row: P) -> Result<String, TskitError> {
230236
match unsafe_tsk_ragged_char_column_access!(
231-
row,
237+
row.into().0,
232238
0,
233239
self.num_rows(),
234240
self.table_.record,
@@ -249,11 +255,14 @@ impl<'a> ProvenanceTable<'a> {
249255
/// # Errors
250256
///
251257
/// [`TskitError::IndexError`] if `r` is out of range.
252-
pub fn row(&'a self, row: tsk_id_t) -> Result<ProvenanceTableRow, TskitError> {
253-
if row < 0 {
258+
pub fn row<P: Into<ProvenanceId> + Copy>(
259+
&'a self,
260+
row: P,
261+
) -> Result<ProvenanceTableRow, TskitError> {
262+
if row.into() < 0 {
254263
Err(TskitError::IndexError)
255264
} else {
256-
match make_provenance_table_row(self, row) {
265+
match make_provenance_table_row(self, row.into().0) {
257266
Some(x) => Ok(x),
258267
None => Err(TskitError::IndexError),
259268
}

0 commit comments

Comments
 (0)