Skip to content

Commit

Permalink
fix: fix overflow in position conversion error
Browse files Browse the repository at this point in the history
  • Loading branch information
tshauck committed May 20, 2024
1 parent c4a4b73 commit b340bcb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
40 changes: 40 additions & 0 deletions exon/exon-bam/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 WHERE TRUE Technologies.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::error::Error;

use arrow::error::ArrowError;

#[derive(Debug)]
pub enum ExonBAMError {
PositionConversionError(String),
}

impl Error for ExonBAMError {}

impl std::fmt::Display for ExonBAMError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExonBAMError::PositionConversionError(e) => {
write!(f, "Error converting position: {}", e)
}
}
}
}

impl From<ExonBAMError> for ArrowError {
fn from(e: ExonBAMError) -> Self {
ArrowError::ExternalError(Box::new(e))
}
}
12 changes: 4 additions & 8 deletions exon/exon-bam/src/indexed_async_batch_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use noodles::{
};
use tokio::io::AsyncBufRead;

use crate::ExonBAMError;

use super::{array_builder::BAMArrayBuilder, BAMConfig};

/// This is a semi-lazy record that can be used to filter on the region without
Expand All @@ -36,16 +38,10 @@ pub(crate) struct SemiLazyRecord {
}

impl TryFrom<RecordBuf> for SemiLazyRecord {
type Error = arrow::error::ArrowError;
type Error = ExonBAMError;

fn try_from(record: RecordBuf) -> Result<Self, Self::Error> {
let start = record.alignment_start();

let alignment_end = start.map(|s| usize::from(s) + record.cigar().alignment_span() - 1);
let alignment_end = alignment_end
.map(Position::try_from)
.transpose()
.map_err(|e| ArrowError::ExternalError(Box::new(e)))?;
let alignment_end = record.alignment_end();

Ok(Self {
inner: record,
Expand Down
2 changes: 2 additions & 0 deletions exon/exon-bam/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
mod array_builder;
mod batch_reader;
mod config;
mod error;
mod indexed_async_batch_stream;

pub use array_builder::BAMArrayBuilder;
pub use batch_reader::BatchReader;
pub use config::BAMConfig;
pub use error::ExonBAMError;
pub use indexed_async_batch_stream::IndexedAsyncBatchStream;

0 comments on commit b340bcb

Please sign in to comment.