Allow compiling with Rust code (#3137)
Co-authored-by: Yuri Astrakhan <YuriAstrakhan@gmail.com> Co-authored-by: Ian Wagner <ianwagner+github@switzerlandmail.ch>
This commit is contained in:
35
rustutils/src/color.rs
Normal file
35
rustutils/src/color.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use csscolorparser::Color;
|
||||
|
||||
#[cxx::bridge(namespace = "rustutils")]
|
||||
mod ffi {
|
||||
struct ParsedColor {
|
||||
pub success: bool,
|
||||
pub r: f32,
|
||||
pub g: f32,
|
||||
pub b: f32,
|
||||
pub a: f32,
|
||||
}
|
||||
|
||||
extern "Rust" {
|
||||
fn parse_css_color(css_str: &str) -> ParsedColor;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_css_color(css_str: &str) -> ffi::ParsedColor {
|
||||
css_str
|
||||
.parse::<Color>()
|
||||
.map(|color| ffi::ParsedColor {
|
||||
success: true,
|
||||
r: color.r as f32,
|
||||
g: color.g as f32,
|
||||
b: color.b as f32,
|
||||
a: color.a as f32,
|
||||
})
|
||||
.unwrap_or_else(|_| ffi::ParsedColor {
|
||||
success: false,
|
||||
r: 0.0,
|
||||
g: 0.0,
|
||||
b: 0.0,
|
||||
a: 0.0,
|
||||
})
|
||||
}
|
||||
1
rustutils/src/lib.rs
Normal file
1
rustutils/src/lib.rs
Normal file
@@ -0,0 +1 @@
|
||||
mod color;
|
||||
Reference in New Issue
Block a user