/*-------------------------------------------------------------------------
 * Copyright (c) Microsoft Corporation.  All rights reserved.
 *
 * documentdb_gateway_core/src/protocol/mod.rs
 *
 *-------------------------------------------------------------------------
 */

use crate::error::{DocumentDBError, Result};

pub mod bson_writer;
pub mod header;
pub mod message;
pub mod op_insert;
pub mod op_query;
pub mod opcode;
pub mod reader;
pub mod util;

pub const MAX_BSON_OBJECT_SIZE: i32 = 16 * 1024 * 1024; // 16 MB
pub const MAX_MESSAGE_SIZE_BYTES: i32 = 48_000_000; // 48 MB
pub const MAX_PRE_AUTH_MESSAGE_SIZE_BYTES: i32 = 250_000; // 250 KB
pub const MESSAGE_SIZE_EXCEEDED_ERROR: &str = "Message size exceeds the maximum allowed size.";
pub const LOGICAL_SESSION_TIMEOUT_MINUTES: u8 = 30;

pub const OK_SUCCEEDED: f64 = 1.0;
pub const OK_FAILED: f64 = 0.0;

/// # Errors
///
/// Returns an error if the operation fails.
/// # Errors
/// Returns error if the operation fails.
pub fn extract_database_and_collection_names(path: &str) -> Result<(&str, &str)> {
    let pos = path.find('.').ok_or(DocumentDBError::bad_value(format!(
        "Collection path {path} does not contain a '.'"
    )))?;
    let db = &path[0..pos];
    let coll = &path[pos + 1..];
    Ok((db, coll))
}
