upend/src/extractors/mod.rs

47 lines
1.1 KiB
Rust
Raw Normal View History

2022-02-15 13:32:46 +01:00
use crate::{
addressing::Address,
database::{entry::Entry, UpEndConnection},
util::jobs::JobContainer,
};
use anyhow::Result;
use std::sync::{Arc, RwLock};
2022-02-10 11:38:45 +01:00
#[cfg(feature = "extractors-web")]
pub mod web;
2022-02-15 13:32:46 +01:00
2022-02-28 21:36:55 +01:00
#[cfg(feature = "extractors-audio")]
pub mod audio;
2022-02-15 13:32:46 +01:00
pub trait Extractor {
fn get(
&self,
address: &Address,
2022-02-28 21:36:55 +01:00
connection: &UpEndConnection,
job_container: Arc<RwLock<JobContainer>>,
) -> Result<Vec<Entry>>;
fn is_needed(&self, _address: &Address, _connection: &UpEndConnection) -> Result<bool> {
Ok(true)
}
2022-02-15 13:32:46 +01:00
fn insert_info(
2022-02-15 13:32:46 +01:00
&self,
address: &Address,
connection: &UpEndConnection,
2022-02-15 13:32:46 +01:00
job_container: Arc<RwLock<JobContainer>>,
2022-02-28 21:36:55 +01:00
) -> Result<usize> {
if self.is_needed(address, connection)? {
2022-02-28 21:36:55 +01:00
let entries = self.get(address, connection, job_container)?;
2022-02-15 13:32:46 +01:00
connection.transaction(|| {
2022-02-28 21:36:55 +01:00
let len = entries.len();
for entry in entries {
connection.insert_entry(entry)?;
}
2022-02-28 21:36:55 +01:00
Ok(len)
})
} else {
2022-02-28 21:36:55 +01:00
Ok(0)
}
2022-02-15 13:32:46 +01:00
}
2022-02-28 21:36:55 +01:00
}