Skip to content

Commit 5c6f07d

Browse files
Move named color & color literal parsing to slint common (slint-ui#9981)
* Move color_parsing to common * [autofix.ci] apply automated fixes * Fixes * Change how feature flags work * Use std feature for common in compiler * Add color-parsing feature instead --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent dcacddc commit 5c6f07d

File tree

11 files changed

+65
-65
lines changed

11 files changed

+65
-65
lines changed

internal/common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ path = "lib.rs"
1919
[features]
2020
default = []
2121
shared-fontique = ["dep:fontique", "dep:ttf-parser"]
22+
color-parsing = []
2223

2324
[dependencies]
2425
ttf-parser = { workspace = true, optional = true }

internal/compiler/lookup/named_colors.rs renamed to internal/common/color_parsing.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,58 @@
33

44
use std::collections::HashMap;
55

6-
use crate::literals::parse_color_literal;
6+
/// Returns `0xaarrggbb`
7+
pub fn parse_color_literal(str: &str) -> Option<u32> {
8+
if !str.starts_with('#') {
9+
return None;
10+
}
11+
if !str.is_ascii() {
12+
return None;
13+
}
14+
let str = &str[1..];
15+
let (r, g, b, a) = match str.len() {
16+
3 => (
17+
u8::from_str_radix(&str[0..=0], 16).ok()? * 0x11,
18+
u8::from_str_radix(&str[1..=1], 16).ok()? * 0x11,
19+
u8::from_str_radix(&str[2..=2], 16).ok()? * 0x11,
20+
255u8,
21+
),
22+
4 => (
23+
u8::from_str_radix(&str[0..=0], 16).ok()? * 0x11,
24+
u8::from_str_radix(&str[1..=1], 16).ok()? * 0x11,
25+
u8::from_str_radix(&str[2..=2], 16).ok()? * 0x11,
26+
u8::from_str_radix(&str[3..=3], 16).ok()? * 0x11,
27+
),
28+
6 => (
29+
u8::from_str_radix(&str[0..2], 16).ok()?,
30+
u8::from_str_radix(&str[2..4], 16).ok()?,
31+
u8::from_str_radix(&str[4..6], 16).ok()?,
32+
255u8,
33+
),
34+
8 => (
35+
u8::from_str_radix(&str[0..2], 16).ok()?,
36+
u8::from_str_radix(&str[2..4], 16).ok()?,
37+
u8::from_str_radix(&str[4..6], 16).ok()?,
38+
u8::from_str_radix(&str[6..8], 16).ok()?,
39+
),
40+
_ => return None,
41+
};
42+
Some((a as u32) << 24 | (r as u32) << 16 | (g as u32) << 8 | (b as u32))
43+
}
44+
45+
#[test]
46+
fn test_parse_color_literal() {
47+
assert_eq!(parse_color_literal("#abc"), Some(0xffaabbcc));
48+
assert_eq!(parse_color_literal("#ABC"), Some(0xffaabbcc));
49+
assert_eq!(parse_color_literal("#AbC"), Some(0xffaabbcc));
50+
assert_eq!(parse_color_literal("#AbCd"), Some(0xddaabbcc));
51+
assert_eq!(parse_color_literal("#01234567"), Some(0x67012345));
52+
assert_eq!(parse_color_literal("#012345"), Some(0xff012345));
53+
assert_eq!(parse_color_literal("_01234567"), None);
54+
assert_eq!(parse_color_literal("→↓←"), None);
55+
assert_eq!(parse_color_literal("#→↓←"), None);
56+
assert_eq!(parse_color_literal("#1234567890"), None);
57+
}
758

859
static NAMED_COLORS: std::sync::OnceLock<HashMap<&'static str, u32>> = std::sync::OnceLock::new();
960

internal/common/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
#![doc = include_str!("README.md")]
55
#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
6-
#![cfg_attr(not(feature = "shared-fontique"), no_std)]
6+
#![cfg_attr(not(any(feature = "shared-fontique", feature = "color-parsing")), no_std)]
77

88
pub mod builtin_structs;
9+
#[cfg(feature = "color-parsing")]
10+
pub mod color_parsing;
911
pub mod enums;
1012
pub mod key_codes;
1113

internal/compiler/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ experimental-library-module = []
4141
default = []
4242

4343
[dependencies]
44-
i-slint-common = { workspace = true, features = ["default"] }
44+
i-slint-common = { workspace = true, features = ["default", "color-parsing"] }
4545

4646
num_enum = "0.7"
4747
strum = { workspace = true }

internal/compiler/literals.rs

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,59 +7,6 @@ use itertools::Itertools;
77
use smol_str::SmolStr;
88
use strum::IntoEnumIterator;
99

10-
/// Returns `0xaarrggbb`
11-
pub fn parse_color_literal(str: &str) -> Option<u32> {
12-
if !str.starts_with('#') {
13-
return None;
14-
}
15-
if !str.is_ascii() {
16-
return None;
17-
}
18-
let str = &str[1..];
19-
let (r, g, b, a) = match str.len() {
20-
3 => (
21-
u8::from_str_radix(&str[0..=0], 16).ok()? * 0x11,
22-
u8::from_str_radix(&str[1..=1], 16).ok()? * 0x11,
23-
u8::from_str_radix(&str[2..=2], 16).ok()? * 0x11,
24-
255u8,
25-
),
26-
4 => (
27-
u8::from_str_radix(&str[0..=0], 16).ok()? * 0x11,
28-
u8::from_str_radix(&str[1..=1], 16).ok()? * 0x11,
29-
u8::from_str_radix(&str[2..=2], 16).ok()? * 0x11,
30-
u8::from_str_radix(&str[3..=3], 16).ok()? * 0x11,
31-
),
32-
6 => (
33-
u8::from_str_radix(&str[0..2], 16).ok()?,
34-
u8::from_str_radix(&str[2..4], 16).ok()?,
35-
u8::from_str_radix(&str[4..6], 16).ok()?,
36-
255u8,
37-
),
38-
8 => (
39-
u8::from_str_radix(&str[0..2], 16).ok()?,
40-
u8::from_str_radix(&str[2..4], 16).ok()?,
41-
u8::from_str_radix(&str[4..6], 16).ok()?,
42-
u8::from_str_radix(&str[6..8], 16).ok()?,
43-
),
44-
_ => return None,
45-
};
46-
Some((a as u32) << 24 | (r as u32) << 16 | (g as u32) << 8 | (b as u32))
47-
}
48-
49-
#[test]
50-
fn test_parse_color_literal() {
51-
assert_eq!(parse_color_literal("#abc"), Some(0xffaabbcc));
52-
assert_eq!(parse_color_literal("#ABC"), Some(0xffaabbcc));
53-
assert_eq!(parse_color_literal("#AbC"), Some(0xffaabbcc));
54-
assert_eq!(parse_color_literal("#AbCd"), Some(0xddaabbcc));
55-
assert_eq!(parse_color_literal("#01234567"), Some(0x67012345));
56-
assert_eq!(parse_color_literal("#012345"), Some(0xff012345));
57-
assert_eq!(parse_color_literal("_01234567"), None);
58-
assert_eq!(parse_color_literal("→↓←"), None);
59-
assert_eq!(parse_color_literal("#→↓←"), None);
60-
assert_eq!(parse_color_literal("#1234567890"), None);
61-
}
62-
6310
pub fn unescape_string(string: &str) -> Option<SmolStr> {
6411
if string.contains('\n') {
6512
// FIXME: new line in string literal not yet supported

internal/compiler/lookup.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ use crate::typeregister::TypeRegister;
1717
use smol_str::{SmolStr, ToSmolStr};
1818
use std::cell::RefCell;
1919

20-
mod named_colors;
21-
22-
pub use named_colors::named_colors;
20+
pub use i_slint_common::color_parsing::named_colors;
2321

2422
/// Contains information which allow to lookup identifier in expressions
2523
pub struct LookupCtx<'a> {
@@ -619,15 +617,15 @@ impl LookupObject for ColorSpecific {
619617
_ctx: &LookupCtx,
620618
f: &mut impl FnMut(&SmolStr, LookupResult) -> Option<R>,
621619
) -> Option<R> {
622-
for (name, c) in named_colors::named_colors().iter() {
620+
for (name, c) in named_colors().iter() {
623621
if let Some(r) = f(&SmolStr::new_static(name), Self::as_result(*c)) {
624622
return Some(r);
625623
}
626624
}
627625
None
628626
}
629627
fn lookup(&self, _ctx: &LookupCtx, name: &SmolStr) -> Option<LookupResult> {
630-
named_colors::named_colors().get(name.as_str()).map(|c| Self::as_result(*c))
628+
named_colors().get(name.as_str()).map(|c| Self::as_result(*c))
631629
}
632630
}
633631
impl ColorSpecific {

internal/compiler/passes/resolving.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ impl Expression {
410410
),
411411
),
412412
SyntaxKind::ColorLiteral => Some(
413-
crate::literals::parse_color_literal(token.text())
413+
i_slint_common::color_parsing::parse_color_literal(token.text())
414414
.map(|i| Expression::Cast {
415415
from: Box::new(Expression::NumberLiteral(i as _, Unit::None)),
416416
to: Type::Color,

internal/interpreter/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn value_from_json(t: &langtype::Type, v: &serde_json::Value) -> Result<Valu
5252
use smol_str::ToSmolStr;
5353

5454
fn string_to_color(s: &str) -> Option<i_slint_core::Color> {
55-
i_slint_compiler::literals::parse_color_literal(s).map(Color::from_argb_encoded)
55+
i_slint_common::color_parsing::parse_color_literal(s).map(Color::from_argb_encoded)
5656
}
5757

5858
match v {

tools/lsp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ default = ["backend-default", "renderer-femtovg", "renderer-software", "preview"
8787

8888
[dependencies]
8989
i-slint-compiler = { workspace = true, features = ["display-diagnostics"] }
90+
i-slint-common = { workspace = true }
9091

9192
by_address = { workspace = true }
9293
clru = { workspace = true }

tools/lsp/language.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ fn get_document_color(
11631163
if token.kind() == SyntaxKind::ColorLiteral {
11641164
(|| -> Option<()> {
11651165
let range = util::token_to_lsp_range(&token);
1166-
let col = i_slint_compiler::literals::parse_color_literal(token.text())?;
1166+
let col = i_slint_common::color_parsing::parse_color_literal(token.text())?;
11671167
let shift = |s: u32| -> f32 { ((col >> s) & 0xff) as f32 / 255. };
11681168
result.push(ColorInformation {
11691169
range,

0 commit comments

Comments
 (0)