Skip to content

Commit cd22109

Browse files
committed
extra guards against empty textselectionsets
1 parent 154d195 commit cd22109

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/api/textselection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ impl<'store> TryFrom<&ResultItem<'store, Annotation>> for ResultTextSelectionSet
12001200
}
12011201
})
12021202
.collect();
1203-
if *invalid {
1203+
if *invalid || tset.is_empty() {
12041204
Err(StamError::NoText(
12051205
"conversion Annotation->TextSelectionSet failed: Annotation does not reference any text or text does not pertain to a single resource",
12061206
))

src/api/translate.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ impl<'store> Translatable<'store> for ResultItem<'store, Annotation> {
8585
mut config: TranslateConfig,
8686
) -> Result<Vec<AnnotationBuilder<'static>>, StamError> {
8787
if let Some(tset) = self.textselectionset() {
88+
if tset.inner().is_empty() {
89+
return Err(StamError::TranslateError(
90+
"Can not translate an annotation that references no text or text in multiple resources".to_string(),
91+
"(translate annotation)",
92+
));
93+
}
8894
if config.source_side_id.is_none() && self.id().is_some() {
8995
config.source_side_id = Some(
9096
self.id()
@@ -126,6 +132,12 @@ impl<'store> Translatable<'store> for ResultTextSelectionSet<'store> {
126132
config: TranslateConfig,
127133
) -> Result<Vec<AnnotationBuilder<'static>>, StamError> {
128134
via.valid_translation()?;
135+
if self.inner().is_empty() {
136+
return Err(StamError::TranslateError(
137+
format!("Can not translate empty TextSelectionSet"),
138+
"",
139+
));
140+
}
129141

130142
let mut builders: Vec<AnnotationBuilder<'static>> = Vec::with_capacity(3);
131143
// Keeps track of which side of the translation the source is found
@@ -144,7 +156,10 @@ impl<'store> Translatable<'store> for ResultTextSelectionSet<'store> {
144156
let mut resegment = false; //resegmentations are produced when the translated annotation covers multiple source text selections, and when users do not want to lose this segmentation (!no_resegmentation)
145157

146158
if config.debug {
147-
eprintln!("[stam translate] ----------------------------");
159+
eprintln!(
160+
"[stam translate] -------------- Translating via {:?} --------------",
161+
via.id()
162+
);
148163
}
149164

150165
let mut sourcecoverage = 0;
@@ -255,7 +270,7 @@ impl<'store> Translatable<'store> for ResultTextSelectionSet<'store> {
255270
//produce the pivot as output
256271
// We may have multiple text selections to translate (all must be found)
257272
return Err(StamError::TranslateError(
258-
format!("Can not translate over a simple translation, pivot has to be complex"),
273+
format!("Can not translate over a simple translation, pivot has to be complex. Pivot ID={}", via.id().unwrap_or("unknown") ),
259274
"",
260275
));
261276
} else {

src/api/transpose.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@ impl<'store> Transposable<'store> for ResultTextSelectionSet<'store> {
127127
config: TransposeConfig,
128128
) -> Result<Vec<AnnotationBuilder<'static>>, StamError> {
129129
via.valid_transposition()?;
130+
if self.inner().is_empty() {
131+
return Err(StamError::TranslateError(
132+
format!("Can not transpose empty TextSelectionSet"),
133+
"",
134+
));
135+
}
130136

131137
let mut builders: Vec<AnnotationBuilder<'static>> = Vec::with_capacity(3);
132138
// Keeps track of which side of the transposition the source is found

0 commit comments

Comments
 (0)