Skip to content

Commit

Permalink
add errors, also impl for SigStore
Browse files Browse the repository at this point in the history
  • Loading branch information
luizirber committed Oct 12, 2024
1 parent e7e2ae5 commit 48d99c5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/core/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ mod test {
use crate::prelude::Select;
use crate::selection::Selection;
use crate::signature::Signature;
use crate::Result;

#[test]
fn sigstore_selection_with_downsample() {
Expand Down
15 changes: 15 additions & 0 deletions src/core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ pub enum SourmashError {
#[error("sketch needs abundance for this operation")]
NeedsAbundanceTracking,

#[error("Expected a MinHash sketch in this signature")]
NoMinHashFound,

#[error("Empty signature")]
EmptySignature,

#[error("Multiple sketches found, expected one")]
MultipleSketchesFound,

#[error("Invalid hash function: {function:?}")]
InvalidHashFunction { function: String },

Expand Down Expand Up @@ -108,6 +117,9 @@ pub enum SourmashErrorCode {
MismatchNum = 1_07,
NeedsAbundanceTracking = 1_08,
CannotUpsampleScaled = 1_09,
NoMinHashFound = 1_10,
EmptySignature = 1_11,
MultipleSketchesFound = 1_12,
// Input sequence errors
InvalidDNA = 11_01,
InvalidProt = 11_02,
Expand Down Expand Up @@ -147,6 +159,9 @@ impl SourmashErrorCode {
SourmashError::MismatchSeed => SourmashErrorCode::MismatchSeed,
SourmashError::MismatchSignatureType => SourmashErrorCode::MismatchSignatureType,
SourmashError::NonEmptyMinHash { .. } => SourmashErrorCode::NonEmptyMinHash,
SourmashError::NoMinHashFound => SourmashErrorCode::NoMinHashFound,
SourmashError::EmptySignature => SourmashErrorCode::EmptySignature,
SourmashError::MultipleSketchesFound => SourmashErrorCode::MultipleSketchesFound,
SourmashError::InvalidDNA { .. } => SourmashErrorCode::InvalidDNA,
SourmashError::InvalidProt { .. } => SourmashErrorCode::InvalidProt,
SourmashError::InvalidCodonLength { .. } => SourmashErrorCode::InvalidCodonLength,
Expand Down
10 changes: 5 additions & 5 deletions src/core/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,9 +888,9 @@ impl PartialEq for Signature {
}

impl TryInto<KmerMinHash> for Signature {
type Error = crate::Error;
type Error = Error;

fn try_into(self) -> Result<KmerMinHash, Self::Error> {
fn try_into(self) -> Result<KmerMinHash, Error> {
match self.signatures.len() {

Check failure on line 894 in src/core/src/signature.rs

View workflow job for this annotation

GitHub Actions / minimum_rust_version

non-exhaustive patterns: `_` not covered
1 => self
.signatures
Expand All @@ -902,9 +902,9 @@ impl TryInto<KmerMinHash> for Signature {
None
}
})
.ok_or_else(|| todo!("error, no minhash found")),
0 => todo!("error, empty signature"),
2.. => todo!("Multiple sketches found! Please run select first."),
.ok_or_else(|| Error::NoMinHashFound),
0 => Err(Error::EmptySignature),
2.. => Err(Error::MultipleSketchesFound),
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/core/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use typed_builder::TypedBuilder;
use crate::errors::ReadDataError;
use crate::prelude::*;
use crate::signature::SigsTrait;
use crate::sketch::minhash::KmerMinHash;
use crate::sketch::Sketch;
use crate::{Error, Result};

Expand Down Expand Up @@ -550,6 +551,15 @@ impl From<Signature> for SigStore {
}
}

impl TryInto<KmerMinHash> for SigStore {
type Error = crate::Error;

fn try_into(self) -> std::result::Result<KmerMinHash, Self::Error> {
let sig: Signature = self.into();
sig.try_into()
}
}

impl Comparable<SigStore> for SigStore {
fn similarity(&self, other: &SigStore) -> f64 {
let ng: &Signature = self.data().unwrap();
Expand Down

0 comments on commit 48d99c5

Please sign in to comment.