Something went wrong. Try again.
Unofficial implementation of analysis for activPAL™ data
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384// Copyright (C) 2026 Jannik Schürg <jannik.schuerg@posteo.de>//// SPDX-License-Identifier: Apache-2.0// SPDX-License-Identifier: MIT
use nom::{ AsChar, IResult, Parser, character::complete::{char, satisfy, u16, u32}, combinator::{complete, map, map_res}, multi::fold_many_m_n,};
use super::ActivPalTime;use crate::HZ;
#[inline]pub(crate) fn parse_seconds(s: &[u8]) -> IResult<&[u8], u32> { complete(map_res( (u32, char('.'), satisfy(|c| c.is_dec_digit())), |(sec, _, sub)| { sec.checked_mul(HZ) .and_then(|whole| whole.checked_add((sub as u32 - '0' as u32) * HZ / 10)) .ok_or(()) }, )) .parse(s)}
#[inline]fn parse_fixed_float(s: &[u8]) -> IResult<&[u8], f64> { map( ( u16, char('.'), fold_many_m_n( 10, 10, satisfy(|c| c.is_dec_digit()), || (0u64, 1u64), |(num, denum), new| (num * 10 + (new as u64 - '0' as u64), denum * 10), ), ), |(a, _, b): (u16, _, (u64, u64))| a as f64 + b.0 as f64 / b.1 as f64, ) .parse(s)}
#[inline]pub(crate) fn parse_activpal_time(s: &[u8]) -> IResult<&[u8], ActivPalTime> { let (i, f) = parse_fixed_float(s)?; Ok((i, ActivPalTime(f)))}
#[test]fn test_from_str_rejects_input_shorter_than_header() { // zip() stops at the shorter side, so without a length check any input // shorter than the header line would vacuously "match" the header and // silently parse to zero events instead of returning an error. assert!("".parse::<crate::Events>().is_err()); assert!("time,datacount".parse::<crate::Events>().is_err());}
#[test]fn test_parse_activpal_time() { let (i, f) = parse_activpal_time(b"45427.0000347222").unwrap(); assert_eq!(i, b""); assert_eq!(f, ActivPalTime::from(45427.0000347222));}
#[test]fn test_parse_seconds() { assert_eq!(parse_seconds(b"1.2").map(|x| x.1), Ok(12),); assert_eq!(parse_seconds(b"10.0").map(|x| x.1), Ok(100),); assert!(parse_seconds(b"4294967295.0").is_err());}
#[test]fn test_parse_fixed_float() { assert_eq!( parse_fixed_float(b"29.0000347222").map(|x| x.1), Ok(29.0000347222) );}