Skip to content

Commit

Permalink
Use more nom parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
Gekkio committed Oct 27, 2024
1 parent ed6dfab commit cd5fbb5
Show file tree
Hide file tree
Showing 58 changed files with 3,249 additions and 2,561 deletions.
103 changes: 50 additions & 53 deletions backend/src/config/cartridge.rs

Large diffs are not rendered by default.

379 changes: 208 additions & 171 deletions backend/src/parser.rs

Large diffs are not rendered by default.

82 changes: 48 additions & 34 deletions backend/src/parser/accelerometer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,63 @@
//
// SPDX-License-Identifier: MIT

use super::{week2, year2, ChipYearWeek, LabelParser, Manufacturer};
use crate::macros::{multi_parser, single_parser};
use nom::{bytes::streaming::tag, character::streaming::char, sequence::tuple, Parser as _};

pub type Accelerometer = ChipYearWeek;
use super::{
for_nom::{digits, uppers, year2_week2},
GenericPart, LabelParser, Manufacturer, NomParser,
};
use crate::macros::multi_parser;

pub type Accelerometer = GenericPart;

/// ```
/// use gbhwdb_backend::parser::{self, LabelParser};
/// assert!(parser::accelerometer::accelerometer().parse("2738109451 0028 ADXL202JQC").is_ok());
/// assert!(parser::accelerometer::ANALOG_ADXL202JQC.parse("2738109451 0028 ADXL202JQC").is_ok());
/// ```
pub fn analog_adxl202jqc() -> &'static impl LabelParser<ChipYearWeek> {
single_parser!(
ChipYearWeek,
r#"^[0-9]{10}\ ([0-9]{2})([0-9]{2})\ ADXL202JQC$"#,
move |c| {
Ok(ChipYearWeek {
kind: "ADXL202JQC".to_owned(),
manufacturer: Some(Manufacturer::Analog),
year: Some(year2(&c[1])?),
week: Some(week2(&c[2])?),
})
}
)
}
pub static ANALOG_ADXL202JQC: NomParser<Accelerometer> = NomParser {
name: "Analog ADXL202JQC",
f: |input| {
tuple((
digits(10),
char(' '),
year2_week2,
char(' '),
tag("ADXL202JQC"),
))
.map(|(_, _, date_code, _, kind)| Accelerometer {
kind: String::from(kind),
manufacturer: Some(Manufacturer::Analog),
date_code: Some(date_code),
})
.parse(input)
},
};

/// ```
/// use gbhwdb_backend::parser::{self, LabelParser};
/// assert!(parser::accelerometer::accelerometer().parse("06245 202JE 0501A").is_ok());
/// assert!(parser::accelerometer::ANALOG_ADXL202JE.parse("06245 202JE 0501A").is_ok());
/// ```
pub fn analog_adxl202je() -> &'static impl LabelParser<ChipYearWeek> {
single_parser!(
ChipYearWeek,
r#"^[0-9]{5}\ 202JE\ [0-9]{4}[A-Z]$"#,
move |_| {
Ok(ChipYearWeek {
kind: "ADXL202JE".to_owned(),
manufacturer: Some(Manufacturer::Analog),
year: None,
week: None,
})
}
)
}
pub static ANALOG_ADXL202JE: NomParser<Accelerometer> = NomParser {
name: "Analog ADXL202JE",
f: |input| {
tuple((
digits(5),
char(' '),
tag("202JE"),
char(' '),
digits(4),
uppers(1),
))
.map(|(_, _, kind, _, _, _)| Accelerometer {
kind: format!("ADXL{kind}"),
manufacturer: Some(Manufacturer::Analog),
date_code: None,
})
.parse(input)
},
};

pub fn accelerometer() -> &'static impl LabelParser<Accelerometer> {
multi_parser!(Accelerometer, analog_adxl202jqc(), analog_adxl202je(),)
multi_parser!(Accelerometer, &ANALOG_ADXL202JQC, &ANALOG_ADXL202JE)
}
50 changes: 0 additions & 50 deletions backend/src/parser/agb_amp.rs

This file was deleted.

88 changes: 0 additions & 88 deletions backend/src/parser/agb_pmic.rs

This file was deleted.

31 changes: 0 additions & 31 deletions backend/src/parser/agb_reg.rs

This file was deleted.

36 changes: 11 additions & 25 deletions backend/src/parser/ags_charge_ctrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,13 @@
//
// SPDX-License-Identifier: MIT

use super::{week2, year1, ChipYearWeek, LabelParser, Manufacturer};
use crate::macros::{multi_parser, single_parser};
use super::{week2, year1, GenericPart, LabelParser};
use crate::{
macros::{multi_parser, single_parser},
parser::{mitsumi, PartDateCode},
};

pub type AgsChargeController = ChipYearWeek;

/// ```
/// use gbhwdb_backend::parser::{self, LabelParser};
/// assert!(parser::ags_charge_ctrl::mitsumi_mm1581a().parse("422 1581A").is_ok());
/// ```
pub fn mitsumi_mm1581a() -> &'static impl LabelParser<AgsChargeController> {
single_parser!(
AgsChargeController,
r#"^([0-9])([0-9]{2})\ 1581A$"#,
move |c| {
Ok(AgsChargeController {
kind: "MM1581A".to_owned(),
manufacturer: Some(Manufacturer::Mitsumi),
year: Some(year1(&c[1])?),
week: Some(week2(&c[2])?),
})
}
)
}
pub type AgsChargeController = GenericPart;

/// ```
/// use gbhwdb_backend::parser::{self, LabelParser};
Expand All @@ -38,13 +22,15 @@ pub fn unknown() -> &'static impl LabelParser<AgsChargeController> {
Ok(AgsChargeController {
kind: "2253B".to_owned(),
manufacturer: None,
year: Some(year1(&c[1])?),
week: Some(week2(&c[2])?),
date_code: Some(PartDateCode::YearWeek {
year: year1(&c[1])?,
week: week2(&c[2])?,
}),
})
}
)
}

pub fn ags_charge_ctrl() -> &'static impl LabelParser<AgsChargeController> {
multi_parser!(AgsChargeController, mitsumi_mm1581a(), unknown())
multi_parser!(AgsChargeController, &mitsumi::MITSUMI_MM1581A, unknown())
}
32 changes: 0 additions & 32 deletions backend/src/parser/ags_pmic_new.rs

This file was deleted.

Loading

0 comments on commit cd5fbb5

Please sign in to comment.