Skip to content

Commit

Permalink
Put ExampleError in the sample crate
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Jun 18, 2024
1 parent c3d7bfe commit 31855fb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub async fn stream_fractal() {
Ok(inner) => inner,
Err(_) => continue,
};
if let Some(fractal_image) = received_frame {
if let Ok(fractal_image) = received_frame {
// Stream the image data to Dart.
SampleFractal {
current_scale,
Expand Down
3 changes: 0 additions & 3 deletions flutter_ffi_plugin/example/native/sample_crate/src/common.rs

This file was deleted.

18 changes: 18 additions & 0 deletions flutter_ffi_plugin/example/native/sample_crate/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::error::Error;
use std::fmt;

#[derive(Debug)]
pub struct ExampleError(pub Box<dyn Error + Send + Sync>);

impl fmt::Display for ExampleError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let source = self.0.as_ref();
write!(f, "An error occured inside the example code.\n{source}")
}
}

impl Error for ExampleError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Some(self.0.as_ref())
}
}
7 changes: 4 additions & 3 deletions flutter_ffi_plugin/example/native/sample_crate/src/fractal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! Copied and modified from
//! https://github.com/abour/fractal repository.
use crate::error::ExampleError;
use image::ImageEncoder;

const WIDTH: u32 = 384;
Expand All @@ -10,7 +11,7 @@ const BUF_SIZE: u32 = WIDTH * HEIGHT * 3;
const SIZE: f64 = 0.000000001;
const MAX_ITER: u32 = 1000;

pub fn draw_fractal_image(scale: f64) -> Option<Vec<u8>> {
pub fn draw_fractal_image(scale: f64) -> Result<Vec<u8>, ExampleError> {
let point_x: f64 = -0.5557506;
let point_y: f64 = -0.55560;
let mut buffer: Vec<u8> = vec![0; BUF_SIZE as usize];
Expand All @@ -27,8 +28,8 @@ pub fn draw_fractal_image(scale: f64) -> Option<Vec<u8>> {
);

match result {
Ok(_) => Some(image_data),
Err(_) => None,
Ok(_) => Ok(image_data),
Err(error) => Err(ExampleError(error.into())),
}
}

Expand Down
19 changes: 13 additions & 6 deletions flutter_ffi_plugin/example/native/sample_crate/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
//! This crate is written for Rinf demonstrations.
mod common;
mod error;
mod fractal;

use common::*;
use error::ExampleError;

pub use fractal::draw_fractal_image;

// `machineid_rs` only supports desktop platforms.
#[cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))]
pub fn get_hardward_id() -> Result<String> {
pub fn get_hardward_id() -> Result<String, ExampleError> {
let mut builder = machineid_rs::IdBuilder::new(machineid_rs::Encryption::MD5);
builder
.add_component(machineid_rs::HWIDComponent::SystemID)
.add_component(machineid_rs::HWIDComponent::CPUCores);
let hwid = builder.build("mykey")?;
let hwid = builder
.build("mykey")
.map_err(|error| ExampleError(error.into()))?;
Ok(hwid)
}
#[cfg(not(any(target_os = "windows", target_os = "macos", target_os = "linux")))]
Expand All @@ -29,7 +31,12 @@ pub fn get_current_time() -> DateTime<offset::Local> {
}

// `reqwest` supports all platforms, including web.
pub async fn fetch_from_web_api(url: &str) -> Result<String> {
let fetched = reqwest::get(url).await?.text().await?;
pub async fn fetch_from_web_api(url: &str) -> Result<String, ExampleError> {
let fetched = reqwest::get(url)
.await
.map_err(|error| ExampleError(error.into()))?
.text()
.await
.map_err(|error| ExampleError(error.into()))?;
Ok(fetched)
}

0 comments on commit 31855fb

Please sign in to comment.