const KEY: &[u8] = b"primary_smart_plane_id=";
#[must_use]
pub fn parse_multirect_parent(caps_text: &[u8]) -> Option<u32> {
let mut from = 0;
while let Some(offset) = caps_text[from..]
.windows(KEY.len())
.position(|window| window == KEY)
{
let pos = from + offset;
if pos == 0 || caps_text[pos - 1] == b'\n' {
let value = &caps_text[pos + KEY.len()..];
let digits = value.iter().take_while(|b| b.is_ascii_digit()).count();
return std::str::from_utf8(&value[..digits])
.ok()
.and_then(|text| text.parse::<u32>().ok())
.filter(|id| *id != 0);
}
from = pos + KEY.len();
}
None
}
#[must_use]
pub fn multirect_pairing_ok(parent: Option<u32>, in_use: impl Fn(u32) -> bool) -> bool {
parent.is_none_or(in_use)
}
#[must_use]
pub fn parse_blend_stage_limit(caps_text: &[u8]) -> Option<usize> {
let max = key_value(caps_text, b"max_blendstages=")?;
let first_unusable = key_value(caps_text, b"sec_ui_blendstage=")
.filter(|stage| *stage < max)
.unwrap_or(max);
usize::try_from(first_unusable.checked_sub(1)?)
.ok()
.filter(|limit| *limit > 0)
}
fn key_value(caps_text: &[u8], key: &[u8]) -> Option<u64> {
let mut from = 0;
while let Some(offset) = caps_text[from..]
.windows(key.len())
.position(|window| window == key)
{
let pos = from + offset;
if pos == 0 || caps_text[pos - 1] == b'\n' {
let value = &caps_text[pos + key.len()..];
let digits = value.iter().take_while(|b| b.is_ascii_digit()).count();
return std::str::from_utf8(&value[..digits]).ok()?.parse().ok();
}
from = pos + key.len();
}
None
}