use crate::{ConvertOptions, Format, Geometry, Result};
use std::path::Path;
pub trait Codec: Send + Sync {
fn can_read(&self, format: Format) -> bool;
fn can_write(&self, format: Format) -> bool;
fn read_path(&self, path: &Path, format: Format, options: &ConvertOptions) -> Result<Geometry>;
fn write_path(
&self,
path: &Path,
format: Format,
geometry: &Geometry,
options: &ConvertOptions,
) -> Result<()>;
}
#[derive(Default)]
pub struct CodecRegistry {
codecs: Vec<Box<dyn Codec>>,
}
impl CodecRegistry {
pub fn new() -> Self {
Self::default()
}
pub fn with_codec(mut self, codec: Box<dyn Codec>) -> Self {
self.codecs.push(codec);
self
}
pub fn register(&mut self, codec: Box<dyn Codec>) {
self.codecs.push(codec);
}
pub fn reader(&self, format: Format) -> Option<&dyn Codec> {
self.codecs
.iter()
.find(|codec| codec.can_read(format))
.map(|codec| codec.as_ref())
}
pub fn writer(&self, format: Format) -> Option<&dyn Codec> {
self.codecs
.iter()
.find(|codec| codec.can_write(format))
.map(|codec| codec.as_ref())
}
}