Skip to content

Commit

Permalink
refactor: add WappAnalyzer::from_bytes()
Browse files Browse the repository at this point in the history
  • Loading branch information
piggynl committed Oct 7, 2024
1 parent 5e41f96 commit 07f8de8
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 99 deletions.
39 changes: 0 additions & 39 deletions src/category.rs

This file was deleted.

38 changes: 0 additions & 38 deletions src/group.rs

This file was deleted.

103 changes: 91 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
mod category;
mod group;
mod tech;

use std::{collections::HashMap, fmt::Debug, iter, path::Path};
use std::{collections::HashMap, fmt::Debug, fs, iter, path::Path};

use anyhow::{Context, Error};
pub use category::WappTechCategory;
pub use group::WappTechGroup;
use serde::Deserialize;
pub use tech::WappTech;

#[derive(Debug)]
pub struct WappAnalyzer {
pub cats: HashMap<i32, WappTechCategory>,
pub groups: HashMap<i32, WappTechGroup>,
pub cats: HashMap<i32, WappTechCategory>,
pub techs: HashMap<String, WappTech>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WappTechGroup {
#[serde(skip_deserializing)]
pub id: i32,
pub name: String,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WappTechCategory {
#[serde(skip_deserializing)]
pub id: i32,
pub groups: Vec<i32>,
pub name: String,
pub priority: i32,
}

pub trait WappPage {
fn url(&self) -> Option<&str> {
None
Expand Down Expand Up @@ -63,17 +78,45 @@ impl WappAnalyzer {
P: AsRef<Path> + Debug,
I: Iterator<Item = P>,
{
let cat_bytes = fs::read(&cat_file).with_context(|| {
let filename = cat_file.as_ref().to_string_lossy();
format!("Failed to open file {filename}",)
})?;

let group_bytes = fs::read(&group_file).with_context(|| {
let filename = group_file.as_ref().to_string_lossy();
format!("Failed to open file {filename}",)
})?;

let mut tech_bytes_vec = Vec::new();
for path in tech_files {
let bytes = fs::read(&path).with_context(|| {
let filename = path.as_ref().to_string_lossy();
format!("Failed to open file {filename}",)
})?;
tech_bytes_vec.push(bytes);
}
let tech_bytes: Vec<&[u8]> = tech_bytes_vec.iter().map(|b| b.as_slice()).collect();

Self::from_bytes(&cat_bytes, &group_bytes, &tech_bytes)
}

pub fn from_bytes(
cat_bytes: &[u8],
group_bytes: &[u8],
tech_bytes: &[&[u8]],
) -> Result<Self, Error> {
Ok(Self {
cats: WappTechCategory::load_from_file(cat_file)
.context("Loading wapp technology categories")?,
groups: WappTechGroup::load_from_file(group_file)
groups: WappTechGroup::load_from_bytes(group_bytes)
.context("Loading wapp technology groups")?,
cats: WappTechCategory::load_from_bytes(cat_bytes)
.context("Loading wapp technology categories")?,
techs: {
let mut techs = HashMap::new();
for path in tech_files {
for (i, data) in tech_bytes.iter().enumerate() {
techs.extend(
WappTech::load_from_file(&path)
.with_context(|| format!("Loading {path:?}"))?,
WappTech::load_from_bytes(data)
.with_context(|| format!("Loading wapp technology (file #{i})"))?,
)
}
techs
Expand All @@ -82,6 +125,42 @@ impl WappAnalyzer {
}
}

impl WappTechGroup {
pub(crate) fn load_from_bytes(bytes: &[u8]) -> Result<HashMap<i32, Self>, Error> {
let data = serde_json::from_slice::<HashMap<&str, Self>>(bytes)
.context("Failed to parse JSON from bytes")?;

let mut result = HashMap::<i32, Self>::with_capacity(data.len());

for (id, item) in data {
let id = id
.parse::<i32>()
.with_context(|| format!("Group {} should has an interger ID", item.name))?;
result.insert(id, Self { id, ..item });
}

Ok(result)
}
}

impl WappTechCategory {
pub(crate) fn load_from_bytes(bytes: &[u8]) -> Result<HashMap<i32, Self>, Error> {
let data = serde_json::from_slice::<HashMap<&str, Self>>(bytes)
.context("Failed to parse JSON from bytes")?;

let mut result = HashMap::<i32, WappTechCategory>::with_capacity(data.len());

for (id, item) in data {
let id = id
.parse::<i32>()
.with_context(|| format!("Category {} should has an interger ID", item.name))?;
result.insert(id, Self { id, ..item });
}

Ok(result)
}
}

impl WappAnalyzer {
pub fn check<P: WappPage>(&self, page: &P) -> Vec<WappCheckResult> {
let mut result = Vec::new();
Expand Down
11 changes: 1 addition & 10 deletions src/tech/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, fs, path::Path, sync::OnceLock};
use std::{collections::HashMap, sync::OnceLock};

use anyhow::{anyhow, bail, Context, Error};
use regex::Regex;
Expand Down Expand Up @@ -50,15 +50,6 @@ struct WappTechRaw {
pub scripts: Option<serde_json::Value>,
}

impl WappTech {
pub(crate) fn load_from_file<P: AsRef<Path>>(path: &P) -> Result<HashMap<String, Self>, Error> {
let bytes = fs::read(path)
.with_context(|| format!("Failed to open file {}", path.as_ref().to_string_lossy()))?;

Self::load_from_bytes(&bytes)
}
}

/// Transform a `Option<serde_json::Value>` to a `Vec<T>` with `f`.
///
/// Mapping:
Expand Down

0 comments on commit 07f8de8

Please sign in to comment.