use core::fmt;
use smol_bytes::Utf8Bytes;
use crate::{
BoundingBox, BoundingBoxFilter, BoundingBoxLeaf, ColumnEq, ColumnKind, ColumnName, ColumnShape,
CompositeFilterMarker, CompositeMarker, ConfidenceFilter, DefaultMarker, DefaultVecMarker,
EnumFilter, FieldName, OrderLevel, PayloadFilter, QuantifiedMarker, Score, TextFilter, shape,
};
use super::{Symbology, detection::CONFIDENCE, located::BBOX};
#[cfg(test)]
mod tests;
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct BarcodeDetection<T> {
pub(super) payload: Utf8Bytes,
pub(super) symbology: Symbology,
pub(super) confidence: Score,
pub(super) bbox: BoundingBox<T>,
}
impl<T> BarcodeDetection<T> {
#[inline]
#[must_use]
pub fn new(
payload: impl Into<Utf8Bytes>,
symbology: Symbology,
confidence: Score,
bbox: BoundingBox<T>,
) -> Self {
Self {
payload: payload.into(),
symbology,
confidence,
bbox,
}
}
#[inline]
#[must_use]
pub fn payload(&self) -> &str {
self.payload.as_str()
}
#[inline]
#[must_use]
pub const fn payload_bytes(&self) -> &Utf8Bytes {
&self.payload
}
#[inline]
#[must_use]
pub const fn symbology(&self) -> &Symbology {
&self.symbology
}
#[inline]
#[must_use]
pub const fn confidence(&self) -> Score {
self.confidence
}
#[inline]
#[must_use]
pub const fn bbox(&self) -> &BoundingBox<T> {
&self.bbox
}
}
impl<T: fmt::Debug> fmt::Debug for BarcodeDetection<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BarcodeDetection")
.field("payload", &self.payload.as_str())
.field("symbology", &self.symbology)
.field("confidence", &self.confidence.get())
.field("bbox", &self.bbox)
.finish()
}
}
pub(super) const PAYLOAD: &FieldName = FieldName::new("payload");
pub(super) const SYMBOLOGY: &FieldName = FieldName::new("symbology");
impl<T: ColumnKind> ColumnKind for BarcodeDetection<T> {
const SEGMENTS: &'static [ColumnShape] = &[
ColumnShape::Plain(ColumnName::of(PAYLOAD)),
ColumnShape::Plain(ColumnName::of(SYMBOLOGY)),
ColumnShape::Plain(ColumnName::of(CONFIDENCE)),
shape::<BoundingBox<T>>(ColumnName::of(BBOX)),
];
const ORDER: &'static [OrderLevel] = ORDER;
}
impl<T: ColumnEq> ColumnEq for BarcodeDetection<T> {
#[inline]
fn column_eq(&self, other: &Self) -> bool {
self.payload_bytes().column_eq(other.payload_bytes())
&& self.symbology().column_eq(other.symbology())
&& self.confidence().column_eq(&other.confidence())
&& self.bbox().column_eq(other.bbox())
}
}
pub(super) const ORDER: &[OrderLevel] = &[
OrderLevel::new(0),
OrderLevel::new(1),
OrderLevel::new(2),
OrderLevel::new(3),
OrderLevel::new(4),
OrderLevel::new(5),
OrderLevel::new(6),
];
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum BarcodeDetectionLeaf<T> {
Payload(Utf8Bytes),
Symbology(Symbology),
Confidence(Score),
Bbox(BoundingBoxLeaf<T>),
}
impl<T: ColumnEq> ColumnEq for BarcodeDetectionLeaf<T> {
fn column_eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Payload(this), Self::Payload(that)) => this.column_eq(that),
(Self::Symbology(this), Self::Symbology(that)) => this.column_eq(that),
(Self::Confidence(this), Self::Confidence(that)) => this.column_eq(that),
(Self::Bbox(this), Self::Bbox(that)) => this.column_eq(that),
_ => false,
}
}
}
impl<T: ColumnKind + Copy> crate::OrderLeaves for BarcodeDetection<T> {
type Leaf = BarcodeDetectionLeaf<T>;
fn leaf(&self, at: usize) -> Option<BarcodeDetectionLeaf<T>> {
match at {
0 => Some(BarcodeDetectionLeaf::Payload(self.payload.clone())),
1 => Some(BarcodeDetectionLeaf::Symbology(self.symbology.clone())),
2 => Some(BarcodeDetectionLeaf::Confidence(self.confidence)),
3..=7 => crate::OrderLeaves::leaf(&self.bbox, at - 3).map(BarcodeDetectionLeaf::Bbox),
_ => None,
}
}
}
impl<T: crate::CursorValue> crate::CursorValue for BarcodeDetectionLeaf<T> {
fn write_cursor(&self, out: &mut Vec<u8>) {
match self {
Self::Payload(payload) => {
out.push(0);
crate::CursorValue::write_cursor(payload, out);
}
Self::Symbology(symbology) => {
out.push(1);
crate::CursorValue::write_cursor(symbology, out);
}
Self::Confidence(confidence) => {
out.push(2);
crate::CursorValue::write_cursor(confidence, out);
}
Self::Bbox(leaf) => {
out.push(3);
crate::CursorValue::write_cursor(leaf, out);
}
}
}
fn read_cursor(bytes: &[u8]) -> Option<Self> {
let (tag, value) = bytes.split_first()?;
match tag {
0 => <Utf8Bytes as crate::CursorValue>::read_cursor(value).map(Self::Payload),
1 => <Symbology as crate::CursorValue>::read_cursor(value).map(Self::Symbology),
2 => <Score as crate::CursorValue>::read_cursor(value).map(Self::Confidence),
3 => <BoundingBoxLeaf<T> as crate::CursorValue>::read_cursor(value).map(Self::Bbox),
_ => None,
}
}
}
#[cfg(feature = "sqlx")]
impl<Db, T> crate::Carrier<Db> for BarcodeDetectionLeaf<T>
where
Db: sqlx::Database,
Utf8Bytes: crate::Carrier<Db>,
Symbology: crate::Carrier<Db>,
Score: crate::Carrier<Db>,
BoundingBoxLeaf<T>: crate::Carrier<Db>,
{
fn push_bind(&self, qb: &mut sqlx::QueryBuilder<Db>) {
match self {
Self::Payload(payload) => <Utf8Bytes as crate::Carrier<Db>>::push_bind(payload, qb),
Self::Symbology(symbology) => <Symbology as crate::Carrier<Db>>::push_bind(symbology, qb),
Self::Confidence(confidence) => <Score as crate::Carrier<Db>>::push_bind(confidence, qb),
Self::Bbox(leaf) => <BoundingBoxLeaf<T> as crate::Carrier<Db>>::push_bind(leaf, qb),
}
}
}
impl<Db, T> crate::StoredAs<Db> for BarcodeDetection<T>
where
Utf8Bytes: crate::StoredAs<Db>,
Symbology: crate::StoredAs<Db>,
Score: crate::StoredAs<Db>,
BoundingBox<T>: crate::StoredAs<Db>,
{
const WORDS: &'static [&'static str] = &[
<Utf8Bytes as crate::StoredAs<Db>>::WORDS[0],
<Symbology as crate::StoredAs<Db>>::WORDS[0],
<Score as crate::StoredAs<Db>>::WORDS[0],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[0],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[1],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[2],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[3],
<BoundingBox<T> as crate::StoredAs<Db>>::WORDS[4],
];
}
impl<T: crate::CollectionElement> crate::CollectionElement for BarcodeDetection<T> {}
#[cfg(feature = "sqlx")]
impl<Db, T> crate::Carrier<Db> for BarcodeDetection<T>
where
Db: sqlx::Database,
Utf8Bytes: crate::Carrier<Db>,
Symbology: crate::Carrier<Db>,
Score: crate::Carrier<Db>,
BoundingBox<T>: crate::Carrier<Db>,
{
fn push_bind(&self, qb: &mut sqlx::QueryBuilder<Db>) {
self.push_leading(qb);
<BoundingBox<T> as crate::Carrier<Db>>::push_bind(&self.bbox, qb);
}
fn push_value_bind(&self, qb: &mut sqlx::QueryBuilder<Db>) {
self.push_leading(qb);
<BoundingBox<T> as crate::Carrier<Db>>::push_value_bind(&self.bbox, qb);
}
}
#[cfg(feature = "sqlx")]
impl<T> BarcodeDetection<T> {
fn push_leading<Db>(&self, qb: &mut sqlx::QueryBuilder<Db>)
where
Db: sqlx::Database,
Utf8Bytes: crate::Carrier<Db>,
Symbology: crate::Carrier<Db>,
Score: crate::Carrier<Db>,
{
<Utf8Bytes as crate::Carrier<Db>>::push_bind(&self.payload, qb);
qb.push(", ");
<Symbology as crate::Carrier<Db>>::push_bind(&self.symbology, qb);
qb.push(", ");
<Score as crate::Carrier<Db>>::push_bind(&self.confidence, qb);
qb.push(", ");
}
}
#[cfg(feature = "sqlx")]
impl<'a, R, T> crate::Assemble<'a, R> for BarcodeDetection<T>
where
R: sqlx::Row,
Utf8Bytes: crate::Assemble<'a, R>,
Symbology: crate::Assemble<'a, R>,
Score: crate::Assemble<'a, R>,
BoundingBox<T>: crate::Assemble<'a, R>,
{
fn assemble(row: &'a R, under: Option<&crate::Path<'_>>) -> sqlx::Result<Self> {
let payload: Utf8Bytes = crate::assembled(row, PAYLOAD, under)?;
let symbology: Symbology = crate::assembled(row, SYMBOLOGY, under)?;
let confidence: Score = crate::assembled(row, CONFIDENCE, under)?;
let bbox: BoundingBox<T> = crate::assembled(row, BBOX, under)?;
Ok(Self::new(payload, symbology, confidence, bbox))
}
}
impl<T: crate::CursorValue> crate::CursorValue for BarcodeDetection<T> {
fn write_cursor(&self, out: &mut Vec<u8>) {
crate::cursor::write_member(&self.payload, out);
crate::cursor::write_member(&self.symbology, out);
crate::cursor::write_member(&self.confidence, out);
crate::cursor::write_member(&self.bbox, out);
}
fn read_cursor(bytes: &[u8]) -> Option<Self> {
let mut rest = bytes;
let payload = crate::cursor::read_member::<Utf8Bytes>(&mut rest)?;
let symbology = crate::cursor::read_member::<Symbology>(&mut rest)?;
let confidence = crate::cursor::read_member::<Score>(&mut rest)?;
let bbox = crate::cursor::read_member::<BoundingBox<T>>(&mut rest)?;
rest
.is_empty()
.then(|| Self::new(payload, symbology, confidence, bbox))
}
}
impl<T> CompositeFilterMarker for BarcodeDetection<T>
where
T: DefaultMarker,
{
type Filter = BarcodeDetectionFilter<PayloadFilter<T>>;
type ColumnFilter = BarcodeDetectionFilter<PayloadFilter<T>>;
}
impl<T: DefaultMarker> DefaultMarker for BarcodeDetection<T> {
type Marker = CompositeMarker<BarcodeDetection<T>>;
}
impl<T: DefaultMarker> DefaultVecMarker for BarcodeDetection<T> {
type Marker = QuantifiedMarker<Vec<BarcodeDetection<T>>>;
}
pub struct BarcodeDetectionFilter<CF> {
pub(super) payload: Option<TextFilter<Utf8Bytes>>,
pub(super) symbology: Option<EnumFilter<Symbology>>,
pub(super) confidence: Option<ConfidenceFilter>,
pub(super) bbox: Option<BoundingBoxFilter<CF>>,
}
impl<CF> BarcodeDetectionFilter<CF> {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
payload: None,
symbology: None,
confidence: None,
bbox: None,
}
}
crate::graphql::filter::opt_accessor!(
payload: TextFilter<Utf8Bytes> => with_payload, set_payload, maybe_payload, update_payload
);
crate::graphql::filter::opt_accessor!(
symbology: EnumFilter<Symbology> => with_symbology, set_symbology, maybe_symbology, update_symbology
);
crate::graphql::filter::opt_accessor!(
confidence: ConfidenceFilter => with_confidence, set_confidence, maybe_confidence, update_confidence
);
crate::graphql::filter::opt_accessor!(
bbox: BoundingBoxFilter<CF> => with_bbox, set_bbox, maybe_bbox, update_bbox
);
}
impl<CF: crate::GraphqlFilter> BarcodeDetectionFilter<CF> {
pub fn is_empty(&self) -> bool {
self
.payload
.as_ref()
.is_none_or(crate::GraphqlFilter::is_empty)
&& self.symbology.as_ref().is_none_or(EnumFilter::is_empty)
&& self
.confidence
.as_ref()
.is_none_or(ConfidenceFilter::is_empty)
&& self
.bbox
.as_ref()
.is_none_or(crate::GraphqlFilter::is_empty)
}
}
impl<CF: crate::GraphqlFilter> crate::GraphqlFilter for BarcodeDetectionFilter<CF> {
#[inline]
fn is_empty(&self) -> bool {
Self::is_empty(self)
}
}
impl<CF> Default for BarcodeDetectionFilter<CF> {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl<CF: fmt::Debug> fmt::Debug for BarcodeDetectionFilter<CF> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BarcodeDetectionFilter")
.field("payload", &self.payload)
.field("symbology", &self.symbology)
.field("confidence", &self.confidence)
.field("bbox", &self.bbox)
.finish()
}
}
impl<CF: Clone> Clone for BarcodeDetectionFilter<CF> {
fn clone(&self) -> Self {
Self {
payload: self.payload.clone(),
symbology: self.symbology.clone(),
confidence: self.confidence.clone(),
bbox: self.bbox.clone(),
}
}
}
impl<CF: PartialEq> PartialEq for BarcodeDetectionFilter<CF> {
fn eq(&self, other: &Self) -> bool {
self.payload == other.payload
&& self.symbology == other.symbology
&& self.confidence == other.confidence
&& self.bbox == other.bbox
}
}