-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17e4fcf
commit f41b54b
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
/// Corresponds to the position of the Eye | ||
/// - TopLeft | ||
/// - TopRight | ||
/// - BottomRight | ||
pub enum EyePosition { | ||
/// Top left eye | ||
TopLeft, | ||
/// Top right eye | ||
TopRight, | ||
/// Bottom right eye | ||
BottomRight, | ||
} | ||
|
||
/// Converts an eye position to a custom svg | ||
/// | ||
/// # Example | ||
/// | ||
/// For the fully squared shape, the svg is `M{x},{y}h7v7h-7` | ||
/// | ||
/// The eye function for the eye frame should be max of 7x7. \ | ||
/// The eye function for the eye ball should be max of 3x3. | ||
/// | ||
/// ```rust | ||
/// fn square(_: EyePosition) -> String { | ||
/// format!("h7v7h-7") | ||
/// } | ||
/// ``` | ||
pub type EyeFunction = fn(EyePosition) -> String; | ||
|
||
// TODO: Find a way to use the same enum for wasm and not wasm | ||
// Current bug being that wasm_bindgen & #[cfg(not(target_arch = "wasm32"))] are not compatible(?) | ||
/// Different possible Shapes to represent modules in a [`crate::QRCode`] | ||
#[repr(C)] | ||
#[wasm_bindgen] | ||
#[cfg(feature = "wasm-bindgen")] | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] | ||
pub enum EyeFrameShape { | ||
/// Square shape | ||
Square, | ||
/// Rounded square shape | ||
Rounded, | ||
/// Circle shape | ||
Circle, | ||
/// Rounded square shape with the outer corner rounded | ||
RoundedSquaredOuterCorner, | ||
/// Leaf shape | ||
Leaf, | ||
/// Rounded square shape with all but the inner corner rounded | ||
RoundedSquaredInnerCorner, | ||
/// Square shape with a dot in the middle | ||
DottedSquare, | ||
/// Eye lash shape | ||
EyeLash, | ||
} | ||
|
||
/// Different possible Shapes to represent modules in a [`crate::QRCode`] | ||
#[cfg(not(feature = "wasm-bindgen"))] | ||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd)] | ||
pub enum EyeFrameShape { | ||
/// Square shape | ||
Square, | ||
/// Rounded square shape | ||
Rounded, | ||
/// Circle shape | ||
Circle, | ||
/// Rounded square shape with the outer corner rounded | ||
RoundedSquaredOuterCorner, | ||
/// Leaf shape | ||
Leaf, | ||
/// Rounded square shape with all but the inner corner rounded | ||
RoundedSquaredInnerCorner, | ||
/// Square shape with a dot in the middle | ||
DottedSquare, | ||
/// Eye lash shape | ||
EyeLash, | ||
/// Custom Shape with a function / closure | ||
/// # Example | ||
/// ```rust | ||
/// use fast_qr::convert::EyeFrameShape; | ||
/// let command_function = |eye_position| { | ||
/// match eye_position { | ||
/// EyePosition::TopLeft => String::from("..."), | ||
/// _ => String::from("..."), | ||
/// } | ||
/// }; | ||
/// let command = EyeFrameShape::Command(command_function); | ||
/// ``` | ||
Command(EyeFunction), | ||
} | ||
|
||
impl From<EyeFrameShape> for usize { | ||
fn from(shape: EyeFrameShape) -> Self { | ||
match shape { | ||
EyeFrameShape::Square => 0, | ||
EyeFrameShape::Rounded => 1, | ||
EyeFrameShape::Circle => 2, | ||
EyeFrameShape::RoundedSquaredOuterCorner => 3, | ||
EyeFrameShape::Leaf => 4, | ||
EyeFrameShape::RoundedSquaredInnerCorner => 5, | ||
EyeFrameShape::DottedSquare => 6, | ||
EyeFrameShape::EyeLash => 7, | ||
#[cfg(not(target_arch = "wasm32"))] | ||
EyeFrameShape::Command(_) => 8, | ||
} | ||
} | ||
} | ||
|
||
impl From<String> for EyeFrameShape { | ||
#[allow(clippy::match_same_arms)] | ||
fn from(shape: String) -> Self { | ||
match shape.as_ref() { | ||
"square" => Self::Square, | ||
"rounded" => Self::Rounded, | ||
"circle" => Self::Circle, | ||
"rounded_squared_side_1" => Self::RoundedSquaredOuterCorner, | ||
"rounded_squared_side_2" => Self::Leaf, | ||
"rounded_squared_side_3" => Self::RoundedSquaredInnerCorner, | ||
"dotted_square" => Self::DottedSquare, | ||
"eye_lash" => Self::EyeLash, | ||
|
||
_ => Self::Square, | ||
} | ||
} | ||
} | ||
|
||
impl From<EyeFrameShape> for &str { | ||
fn from(shape: EyeFrameShape) -> Self { | ||
match shape { | ||
EyeFrameShape::Square => "square", | ||
EyeFrameShape::Rounded => "rounded", | ||
EyeFrameShape::Circle => "circle", | ||
EyeFrameShape::RoundedSquaredOuterCorner => "rounded_squared_side_1", | ||
EyeFrameShape::Leaf => "rounded_squared_side_2", | ||
EyeFrameShape::RoundedSquaredInnerCorner => "rounded_squared_side_3", | ||
EyeFrameShape::DottedSquare => "dotted_square", | ||
EyeFrameShape::EyeLash => "eye_lash", | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
EyeFrameShape::Command(_) => "command", | ||
} | ||
} | ||
} | ||
|
||
impl EyeFrameShape { | ||
pub(crate) fn square(_: EyePosition) -> String { | ||
format!("h7v7h-7") | ||
} | ||
|
||
const FUNCTIONS: [EyeFunction; 1] = [EyeFrameShape::square]; | ||
} | ||
|
||
impl core::ops::Deref for EyeFrameShape { | ||
type Target = EyeFunction; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
let index: usize = (*self).into(); | ||
match self { | ||
#[cfg(not(target_arch = "wasm32"))] | ||
Self::Command(func) => func, | ||
_ => &Self::FUNCTIONS[index], | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters