|
14 | 14 |
|
15 | 15 | use crate::bindings as ll_bindings;
|
16 | 16 | use crate::SizeType;
|
17 |
| -use crate::{tsk_id_t, tsk_size_t, ProvenanceId, TskitError}; |
| 17 | +use crate::{tsk_id_t, tsk_size_t, ProvenanceId}; |
18 | 18 | use ll_bindings::{tsk_provenance_table_free, tsk_provenance_table_init};
|
19 | 19 |
|
20 | 20 | #[derive(Eq)]
|
@@ -47,8 +47,8 @@ impl std::fmt::Display for ProvenanceTableRow {
|
47 | 47 | fn make_provenance_row(table: &ProvenanceTable, pos: tsk_id_t) -> Option<ProvenanceTableRow> {
|
48 | 48 | Some(ProvenanceTableRow {
|
49 | 49 | id: pos.into(),
|
50 |
| - timestamp: table.timestamp(pos).ok()?, |
51 |
| - record: table.record(pos).ok()?, |
| 50 | + timestamp: table.timestamp(pos)?, |
| 51 | + record: table.record(pos)?, |
52 | 52 | })
|
53 | 53 | }
|
54 | 54 |
|
@@ -103,47 +103,66 @@ impl<'a> ProvenanceTable<'a> {
|
103 | 103 |
|
104 | 104 | /// Get the ISO-formatted time stamp for row `row`.
|
105 | 105 | ///
|
106 |
| - /// # Errors |
| 106 | + /// # Returns |
| 107 | + /// |
| 108 | + /// * `Some(String)` if `row` is valid. |
| 109 | + /// * `None` otherwise. |
107 | 110 | ///
|
108 |
| - /// [`TskitError::IndexError`] if `r` is out of range. |
109 |
| - pub fn timestamp<P: Into<ProvenanceId> + Copy>(&'a self, row: P) -> Result<String, TskitError> { |
110 |
| - match unsafe_tsk_ragged_char_column_access!( |
| 111 | + /// # Examples |
| 112 | + /// |
| 113 | + /// ``` |
| 114 | + /// use tskit::TableAccess; |
| 115 | + /// let mut tables = tskit::TableCollection::new(10.).unwrap(); |
| 116 | + /// assert!(tables.add_provenance("foo").is_ok()); |
| 117 | + /// if let Some(timestamp) = tables.provenances().timestamp(0) { |
| 118 | + /// // then 0 is a valid row in the provenance table |
| 119 | + /// } |
| 120 | + /// # else { |
| 121 | + /// # panic!("Expected Some(timestamp)"); |
| 122 | + /// # } |
| 123 | + /// ``` |
| 124 | + pub fn timestamp<P: Into<ProvenanceId> + Copy>(&'a self, row: P) -> Option<String> { |
| 125 | + unsafe_tsk_ragged_char_column_access!( |
111 | 126 | row.into().0,
|
112 | 127 | 0,
|
113 | 128 | self.num_rows(),
|
114 | 129 | self.table_,
|
115 | 130 | timestamp,
|
116 | 131 | timestamp_offset,
|
117 | 132 | timestamp_length
|
118 |
| - ) { |
119 |
| - Ok(Some(string)) => Ok(string), |
120 |
| - Ok(None) => Err(crate::TskitError::ValueError { |
121 |
| - got: String::from("None"), |
122 |
| - expected: String::from("String"), |
123 |
| - }), |
124 |
| - Err(e) => Err(e), |
125 |
| - } |
| 133 | + ) |
126 | 134 | }
|
127 | 135 |
|
128 | 136 | /// Get the provenance record for row `row`.
|
129 | 137 | ///
|
130 |
| - /// # Errors |
| 138 | + /// # Returns |
| 139 | + /// |
| 140 | + /// * `Some(String)` if `row` is valid. |
| 141 | + /// * `None` otherwise. |
| 142 | + /// |
| 143 | + /// # Examples |
131 | 144 | ///
|
132 |
| - /// [`TskitError::IndexError`] if `r` is out of range. |
133 |
| - pub fn record<P: Into<ProvenanceId> + Copy>(&'a self, row: P) -> Result<String, TskitError> { |
134 |
| - match unsafe_tsk_ragged_char_column_access!( |
| 145 | + /// ``` |
| 146 | + /// use tskit::TableAccess; |
| 147 | + /// let mut tables = tskit::TableCollection::new(10.).unwrap(); |
| 148 | + /// assert!(tables.add_provenance("foo").is_ok()); |
| 149 | + /// if let Some(record) = tables.provenances().record(0) { |
| 150 | + /// // then 0 is a valid row in the provenance table |
| 151 | + /// # assert_eq!(record, "foo"); |
| 152 | + /// } |
| 153 | + /// # else { |
| 154 | + /// # panic!("Expected Some(timestamp)"); |
| 155 | + /// # } |
| 156 | + pub fn record<P: Into<ProvenanceId> + Copy>(&'a self, row: P) -> Option<String> { |
| 157 | + unsafe_tsk_ragged_char_column_access!( |
135 | 158 | row.into().0,
|
136 | 159 | 0,
|
137 | 160 | self.num_rows(),
|
138 | 161 | self.table_,
|
139 | 162 | record,
|
140 | 163 | record_offset,
|
141 | 164 | record_length
|
142 |
| - ) { |
143 |
| - Ok(Some(string)) => Ok(string), |
144 |
| - Ok(None) => Ok(String::from("")), |
145 |
| - Err(e) => Err(e), |
146 |
| - } |
| 165 | + ) |
147 | 166 | }
|
148 | 167 |
|
149 | 168 | /// Obtain a [`ProvenanceTableRow`] for row `row`.
|
@@ -201,16 +220,14 @@ mod test_provenances {
|
201 | 220 | // check for tables...
|
202 | 221 | let mut tables = crate::TableCollection::new(10.).unwrap();
|
203 | 222 | let s = String::from("");
|
204 |
| - let row_id = tables.add_provenance(&s).unwrap(); |
205 |
| - let _ = tables.provenances().row(row_id).unwrap(); |
| 223 | + assert!(tables.add_provenance(&s).is_err()); |
206 | 224 |
|
207 | 225 | // and for tree sequences...
|
208 | 226 | tables.build_index().unwrap();
|
209 | 227 | let mut ts = tables
|
210 | 228 | .tree_sequence(crate::TreeSequenceFlags::default())
|
211 | 229 | .unwrap();
|
212 |
| - let row_id = ts.add_provenance(&s).unwrap(); |
213 |
| - let _ = ts.provenances().row(row_id).unwrap(); |
| 230 | + assert!(ts.add_provenance(&s).is_err()) |
214 | 231 | }
|
215 | 232 |
|
216 | 233 | #[test]
|
|
0 commit comments