use crate::pages::PageSet;
use crate::ui::Ui;
use crate::component::ComponentId;
#[derive(Clone, Copy)]
pub enum Provided<'a> {
Barcode(&'a dyn BarcodeProvider),
Export(&'a dyn ExportProvider),
Pdf(&'a dyn PdfProvider),
MapTiles(&'a dyn MapTileProvider),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BarcodeImage {
Linear { modules: Vec<bool>, text: String },
Matrix { width: usize, modules: Vec<bool> },
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ErrorCorrection {
Low,
#[default]
Medium,
Quartile,
High,
}
impl crate::prop::PropEnum for ErrorCorrection {
const NAMES: &'static [&'static str] = &["low", "medium", "quartile", "high"];
fn from_index(index: usize) -> Option<Self> {
[ErrorCorrection::Low, ErrorCorrection::Medium, ErrorCorrection::Quartile, ErrorCorrection::High].get(index).copied()
}
fn index(self) -> usize {
self as usize
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct BarcodeOptions {
pub error_correction: ErrorCorrection,
}
pub trait BarcodeProvider {
fn formats(&self) -> Vec<&'static str>;
fn encode(&self, format: &str, data: &str, options: &BarcodeOptions) -> Result<BarcodeImage, String>;
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ColumnKind {
#[default]
Text,
Integer,
Number,
Date,
DateTime,
Bool,
}
#[derive(Clone, Debug, PartialEq)]
pub enum CellValue {
Empty,
Text(String),
Number(f64),
Bool(bool),
Date(i32, u32, u32),
DateTime(i32, u32, u32, u32, u32, u32),
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ExportColumn {
pub title: String,
pub kind: ColumnKind,
pub format: String,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ExportTable {
pub name: String,
pub columns: Vec<ExportColumn>,
pub rows: Vec<Vec<CellValue>>,
}
pub trait ExportProvider {
fn extension(&self) -> &'static str;
fn mime(&self) -> &'static str;
fn write(&self, tables: &[ExportTable]) -> Result<Vec<u8>, String>;
}
pub trait PdfProvider {
fn write(&self, pages: &PageSet) -> Result<Vec<u8>, String>;
}
pub struct BuiltinPdf;
impl PdfProvider for BuiltinPdf {
fn write(&self, pages: &PageSet) -> Result<Vec<u8>, String> {
Ok(pages.to_pdf())
}
}
pub trait MapTileProvider {
fn tile_url(&self, z: u32, x: u32, y: u32) -> String;
fn attribution(&self) -> String;
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TileServer {
pub url: String,
pub attribution: String,
}
impl Default for TileServer {
fn default() -> Self {
Self { url: "https://tile.openstreetmap.org/{z}/{x}/{y}.png".into(), attribution: "© OpenStreetMap".into() }
}
}
impl TileServer {
pub fn expand(template: &str, z: u32, x: u32, y: u32) -> String {
let shard = ["a", "b", "c"][((x + y) % 3) as usize];
template.replace("{z}", &z.to_string()).replace("{x}", &x.to_string()).replace("{y}", &y.to_string()).replace("{s}", shard)
}
}
impl MapTileProvider for TileServer {
fn tile_url(&self, z: u32, x: u32, y: u32) -> String {
Self::expand(&self.url, z, x, y)
}
fn attribution(&self) -> String {
self.attribution.clone()
}
}
impl Ui {
pub fn provided(&self, id: ComponentId) -> Option<Provided<'_>> {
self.component(id)?.provided()
}
}