Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add category filters #78

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions backend/src/driver_db/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,60 @@ pub enum Category {
Oled,

// Timer
/// Chips measuring time
Timer,

#[serde(rename = "Timer::RTC")]
/// Clocks that keep track of wall time
///
/// Often allow to measure time with an external battery.
Rtc,
}

impl Category {
/// Get a list of all categories that contain this category
pub fn parents(&self) -> Vec<Self> {
let mut parents = vec![];

let mut me = *self;
while let Some(parent) = me.parent() {
parents.push(parent);
me = parent;
}

parents
}

fn parent(&self) -> Option<Self> {
Some(match self {
Self::Analog => {
return None;
}
Self::Adc => Self::Analog,
Self::Dac => Self::Analog,
Self::Sensor => {
return None;
}
Self::PowerMeter => Self::Sensor,
Self::Accelerometer => Self::Sensor,
Self::Gyroscope => Self::Sensor,
Self::Magnetometer => Self::Sensor,
Self::IoExpander => {
return None;
}
Self::PwmExpander => Self::IoExpander,
Self::Actor => {
return None;
}
Self::MotorController => Self::Actor,
Self::Display => {
return None;
}
Self::Oled => Self::Display,
Self::Timer => {
return None;
}
Self::Rtc => Self::Timer,
})
}
}
5 changes: 4 additions & 1 deletion backend/src/website_db/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ impl From<&[FullCrate]> for Indexes {
let mut has_dev_board = BTreeSet::new();

for (i, krate) in value.iter().enumerate() {
// TODO also match sub categories
for cat in &krate.chip_meta.categories {
category.add(*cat, i);

for parent in cat.parents() {
category.add(parent, i);
}
}

for l in krate.licenses() {
Expand Down
17 changes: 10 additions & 7 deletions driver-db-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@
},
"Category": {
"oneOf": [
{
"type": "string",
"enum": [
"Timer",
"Timer::RTC"
]
},
{
"description": "Devices interacting with analog signals",
"type": "string",
Expand Down Expand Up @@ -124,6 +117,16 @@
"description": "OLED Screens",
"type": "string",
"const": "Display::OLED"
},
{
"description": "Chips measuring time",
"type": "string",
"const": "Timer"
},
{
"description": "Clocks that keep track of wall time\n\n Often allow to measure time with an external battery.",
"type": "string",
"const": "Timer::RTC"
}
]
},
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
const t_crates = crates as FullCrate[];
let t_indexes = indexes as Indexes;

let selected_c: number[][] = [];
let selected_d: number[][] = [];
let selected_l: number[][] = [];
let selected_r: number[][] = [];
Expand Down Expand Up @@ -42,7 +43,7 @@
}
}

$: selected_crates = combine_filters(t_crates.length, [selected_d, selected_l, selected_r, selected_i, selected_f]);
$: selected_crates = combine_filters(t_crates.length, [selected_c, selected_d, selected_l, selected_r, selected_i, selected_f]);

</script>

Expand All @@ -53,8 +54,9 @@
<h1>{selected_crates.length} awesome drivers waiting for you!</h1>
<main>
<div class="filters">
<SortFilter name="Sort by" />
<SortFilter name="Sort by"/>
<TextFilter crates={t_crates} bind:selected={selected_f}/>
<Filter name="Categories" values={t_indexes.category} bind:selected={selected_c}/>
<Filter name="Dependencies" values={t_indexes.dependencies} bind:selected={selected_d}/>
<Filter name="Interfaces" values={t_indexes.interfaces} bind:selected={selected_i}/>
<Filter name="👮 License" values={t_indexes.license} bind:selected={selected_l}/>
Expand Down