Change validate_document_id to public and remove extra layer of result

This commit is contained in:
Louis Dureuil 2024-05-21 16:36:47 +02:00
parent e248d2a1e6
commit fd2c95999d
No known key found for this signature in database
2 changed files with 12 additions and 9 deletions

View File

@ -12,7 +12,10 @@ use bimap::BiHashMap;
pub use builder::DocumentsBatchBuilder; pub use builder::DocumentsBatchBuilder;
pub use enriched::{EnrichedDocument, EnrichedDocumentsBatchCursor, EnrichedDocumentsBatchReader}; pub use enriched::{EnrichedDocument, EnrichedDocumentsBatchCursor, EnrichedDocumentsBatchReader};
use obkv::KvReader; use obkv::KvReader;
pub use primary_key::{DocumentIdExtractionError, FieldIdMapper, PrimaryKey, DEFAULT_PRIMARY_KEY}; pub use primary_key::{
validate_document_id_value, DocumentIdExtractionError, FieldIdMapper, PrimaryKey,
DEFAULT_PRIMARY_KEY,
};
pub use reader::{DocumentsBatchCursor, DocumentsBatchCursorError, DocumentsBatchReader}; pub use reader::{DocumentsBatchCursor, DocumentsBatchCursorError, DocumentsBatchReader};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@ -60,7 +60,7 @@ impl<'a> PrimaryKey<'a> {
Some(document_id_bytes) => { Some(document_id_bytes) => {
let document_id = serde_json::from_slice(document_id_bytes) let document_id = serde_json::from_slice(document_id_bytes)
.map_err(InternalError::SerdeJson)?; .map_err(InternalError::SerdeJson)?;
match validate_document_id_value(document_id)? { match validate_document_id_value(document_id) {
Ok(document_id) => Ok(Ok(document_id)), Ok(document_id) => Ok(Ok(document_id)),
Err(user_error) => { Err(user_error) => {
Ok(Err(DocumentIdExtractionError::InvalidDocumentId(user_error))) Ok(Err(DocumentIdExtractionError::InvalidDocumentId(user_error)))
@ -88,7 +88,7 @@ impl<'a> PrimaryKey<'a> {
} }
match matching_documents_ids.pop() { match matching_documents_ids.pop() {
Some(document_id) => match validate_document_id_value(document_id)? { Some(document_id) => match validate_document_id_value(document_id) {
Ok(document_id) => Ok(Ok(document_id)), Ok(document_id) => Ok(Ok(document_id)),
Err(user_error) => { Err(user_error) => {
Ok(Err(DocumentIdExtractionError::InvalidDocumentId(user_error))) Ok(Err(DocumentIdExtractionError::InvalidDocumentId(user_error)))
@ -159,14 +159,14 @@ fn validate_document_id(document_id: &str) -> Option<&str> {
} }
} }
pub fn validate_document_id_value(document_id: Value) -> Result<StdResult<String, UserError>> { pub fn validate_document_id_value(document_id: Value) -> StdResult<String, UserError> {
match document_id { match document_id {
Value::String(string) => match validate_document_id(&string) { Value::String(string) => match validate_document_id(&string) {
Some(s) if s.len() == string.len() => Ok(Ok(string)), Some(s) if s.len() == string.len() => Ok(string),
Some(s) => Ok(Ok(s.to_string())), Some(s) => Ok(s.to_string()),
None => Ok(Err(UserError::InvalidDocumentId { document_id: Value::String(string) })), None => Err(UserError::InvalidDocumentId { document_id: Value::String(string) }),
}, },
Value::Number(number) if number.is_i64() => Ok(Ok(number.to_string())), Value::Number(number) if number.is_i64() => Ok(number.to_string()),
content => Ok(Err(UserError::InvalidDocumentId { document_id: content })), content => Err(UserError::InvalidDocumentId { document_id: content }),
} }
} }