use std::collections::VecDeque;
use std::sync::{Mutex, PoisonError};
use async_trait::async_trait;
use crate::completion::Completion;
use crate::provider::{Provider, ProviderError};
use crate::request::ConversationRequest;
pub struct MockProvider {
script: Mutex<VecDeque<Result<Completion, ProviderError>>>,
}
impl MockProvider {
#[must_use]
pub fn new(script: Vec<Completion>) -> Self {
Self {
script: Mutex::new(script.into_iter().map(Ok).collect()),
}
}
#[must_use]
pub fn with_results(script: Vec<Result<Completion, ProviderError>>) -> Self {
Self {
script: Mutex::new(script.into_iter().collect()),
}
}
}
#[async_trait]
impl Provider for MockProvider {
#[allow(clippy::unnecessary_literal_bound)]
fn api_schema(&self) -> &str {
"mock"
}
async fn complete(&self, _request: &ConversationRequest) -> Result<Completion, ProviderError> {
let mut script = self.script.lock().unwrap_or_else(PoisonError::into_inner);
match script.pop_front() {
Some(result) => result,
None => panic!(
"MockProvider script exhausted: complete() was called more times than scripted"
),
}
}
}