use crate::{
DocStore,
store::{StoreError, StoreResult, bytes, cell_bytes, cell_text, text},
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WebCaptureRow {
pub capture_id: String,
pub source_uri: String,
pub body: Vec<u8>,
pub exchange_json: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WebRepresentationRow {
pub representation_id: String,
pub capture_id: String,
pub text: String,
pub metadata_json: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WebAnchorRow {
pub anchor_id: String,
pub subject: String,
pub representation_id: String,
pub record_json: String,
}
impl DocStore {
pub fn save_web_capture(&mut self, row: &WebCaptureRow) -> StoreResult<()> {
if let Some(old) = self.load_web_capture(&row.capture_id)? {
if old.body != row.body {
return Err(StoreError::Invalid(
"capture id already names different bytes".into(),
));
}
return Ok(());
}
self.insert(
"web_captures",
&["capture_id", "source_uri", "body", "exchange_json"],
vec![
text(&row.capture_id),
text(&row.source_uri),
bytes(row.body.clone()),
text(&row.exchange_json),
],
)
}
pub fn save_web_representation(&mut self, row: &WebRepresentationRow) -> StoreResult<()> {
self.upsert(
"web_representations",
&["representation_id", "capture_id", "text", "metadata_json"],
vec![
text(&row.representation_id),
text(&row.capture_id),
text(&row.text),
text(&row.metadata_json),
],
)
}
pub fn load_web_representation(&self, id: &str) -> StoreResult<Option<WebRepresentationRow>> {
self.select(
"web_representations",
&["capture_id", "text", "metadata_json"],
&[("representation_id", text(id))],
&[],
Some(1),
)?
.into_iter()
.next()
.map(|r| {
Ok(WebRepresentationRow {
representation_id: id.into(),
capture_id: cell_text(&r, 0)?.into(),
text: cell_text(&r, 1)?.into(),
metadata_json: cell_text(&r, 2)?.into(),
})
})
.transpose()
}
pub fn load_web_capture(&self, id: &str) -> StoreResult<Option<WebCaptureRow>> {
self.select(
"web_captures",
&["source_uri", "body", "exchange_json"],
&[("capture_id", text(id))],
&[],
Some(1),
)?
.into_iter()
.next()
.map(|r| {
Ok(WebCaptureRow {
capture_id: id.into(),
source_uri: cell_text(&r, 0)?.into(),
body: cell_bytes(&r, 1)?.to_vec(),
exchange_json: cell_text(&r, 2)?.into(),
})
})
.transpose()
}
pub fn save_web_anchor(&mut self, row: &WebAnchorRow) -> StoreResult<()> {
self.upsert(
"web_evidence_anchors",
&["anchor_id", "subject", "representation_id", "record_json"],
vec![
text(&row.anchor_id),
text(&row.subject),
text(&row.representation_id),
text(&row.record_json),
],
)
}
pub fn load_web_anchor(&self, id: &str) -> StoreResult<Option<WebAnchorRow>> {
self.select(
"web_evidence_anchors",
&["subject", "representation_id", "record_json"],
&[("anchor_id", text(id))],
&[],
Some(1),
)?
.into_iter()
.next()
.map(|r| {
Ok(WebAnchorRow {
anchor_id: id.into(),
subject: cell_text(&r, 0)?.into(),
representation_id: cell_text(&r, 1)?.into(),
record_json: cell_text(&r, 2)?.into(),
})
})
.transpose()
}
}