pub trait GraphicsConverter: Send + Sync {
// Required methods
fn name(&self) -> &str;
fn offers(&self, from: ImageFormat, to: ImageFormat) -> bool;
fn convert(
&self,
request: &ConversionRequest<'_>,
progress: &Scope,
) -> Result<Vec<u8>, GraphicsConvertError>;
}Expand description
A converter: a service that turns an image of one format into
another, which a host or an extension contributes to the service slot
CONVERTERS_SLOT. See the module documentation.
A converter may keep the results it has computed, provided that reusing one cannot be told from converting again.
§Examples
use std::sync::Arc;
use flm_core::core::registry::RegistryBuilder;
use flm_core::progress::Scope;
use flm_core::stdlib::features::graphics::{
CONVERTERS_SLOT, ConversionRequest, GraphicsConvertError, GraphicsConverter, ImageFormat,
};
/// Turns a GIF into a PNG by keeping its bytes: a stand-in for a real one.
struct GifToPng;
impl GraphicsConverter for GifToPng {
fn name(&self) -> &str {
"gif-to-png"
}
fn offers(&self, from: ImageFormat, to: ImageFormat) -> bool {
from == ImageFormat::Gif && to == ImageFormat::Png
}
fn convert(&self, request: &ConversionRequest<'_>, _progress: &Scope) -> Result<Vec<u8>, GraphicsConvertError> {
Ok(request.content.to_vec())
}
}
let mut builder = RegistryBuilder::new();
builder.contribute_to(CONVERTERS_SLOT, Arc::new(GifToPng) as Arc<dyn GraphicsConverter>);Required Methods§
Sourcefn name(&self) -> &str
fn name(&self) -> &str
Returns the converter’s name, by which a rule’s via lists it, such as
raster.
Sourcefn offers(&self, from: ImageFormat, to: ImageFormat) -> bool
fn offers(&self, from: ImageFormat, to: ImageFormat) -> bool
Returns whether the converter can turn content of the format from
into the format to.
Sourcefn convert(
&self,
request: &ConversionRequest<'_>,
progress: &Scope,
) -> Result<Vec<u8>, GraphicsConvertError>
fn convert( &self, request: &ConversionRequest<'_>, progress: &Scope, ) -> Result<Vec<u8>, GraphicsConvertError>
Returns the content request.content, of the format request.from,
turned into the format request.to under request.options.
progress is the scope of the conversion’s row on the progress sheet
of the build, a row of the kind
CONVERT_KIND that the publish stage
declares for this image. A converter that does slow work in steps
declares its rows under it (Scope::child), and may ask
cancelled between two steps; a converter that
does the work in one step ignores it. In a build that reports nowhere,
the scope is Scope::none, whose methods do nothing.
§Errors
A GraphicsConvertError in the converter’s own words when the
content cannot be read or the conversion fails.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".