Move ItemType u8 representations into index module

Previosuly, we used the u8 values for the ItemType variants used in the
search index as the discriminant.  But as the ID used in the search
index is an implementation detail of the index parser, this patch moves
the mapping to the index module.
This commit is contained in:
Robin Krahl 2020-08-17 13:42:10 +02:00
parent 861a2e8861
commit e74ef86ebe
No known key found for this signature in database
GPG Key ID: 8E9B0870524F69D8
4 changed files with 66 additions and 41 deletions

12
Cargo.lock generated
View File

@ -813,7 +813,6 @@ dependencies = [
"pager 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_repr 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_tuple 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"structopt 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"syntect 4.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -892,16 +891,6 @@ dependencies = [
"serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_repr"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 1.0.34 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "serde_tuple"
version = "0.5.0"
@ -1356,7 +1345,6 @@ dependencies = [
"checksum serde 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)" = "5317f7588f0a5078ee60ef675ef96735a1442132dc645eb1d12c018620ed8cd3"
"checksum serde_derive 1.0.114 (registry+https://github.com/rust-lang/crates.io-index)" = "2a0be94b04690fbaed37cddffc5c134bf537c8e3329d53e982fe04c374978f8e"
"checksum serde_json 1.0.56 (registry+https://github.com/rust-lang/crates.io-index)" = "3433e879a558dde8b5e8feb2a04899cf34fdde1fafb894687e52105fc1162ac3"
"checksum serde_repr 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2dc6b7951b17b051f3210b063f12cc17320e2fe30ae05b0fe2a3abb068551c76"
"checksum serde_tuple 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f4f025b91216f15a2a32aa39669329a475733590a015835d1783549a56d09427"
"checksum serde_tuple_macros 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4076151d1a2b688e25aaf236997933c66e18b870d0369f8b248b8ab2be630d7e"
"checksum serde_yaml 0.8.13 (registry+https://github.com/rust-lang/crates.io-index)" = "ae3e2dd40a7cdc18ca80db804b7f461a39bb721160a85c9a1fa30134bf3c02a5"

View File

@ -24,7 +24,6 @@ log = "0.4.11"
markup5ever = "0.10.0"
pager = "0.15.0"
serde_json = "1.0.56"
serde_repr = "0.1.6"
serde_tuple = "0.5.0"
toml = "0.5.6"
xdg = "2.2.0"

View File

@ -29,35 +29,34 @@ pub struct Text {
#[derive(Clone, Debug, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct Code(String);
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, serde_repr::Deserialize_repr)]
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum ItemType {
Module = 0,
ExternCrate = 1,
Import = 2,
Struct = 3,
Enum = 4,
Function = 5,
Typedef = 6,
Static = 7,
Trait = 8,
Impl = 9,
TyMethod = 10,
Method = 11,
StructField = 12,
Variant = 13,
Macro = 14,
Primitive = 15,
AssocType = 16,
Constant = 17,
AssocConst = 18,
Union = 19,
ForeignType = 20,
Keyword = 21,
OpaqueTy = 22,
ProcAttribute = 23,
ProcDerive = 24,
TraitAlias = 25,
Module,
ExternCrate,
Import,
Struct,
Enum,
Function,
Typedef,
Static,
Trait,
Impl,
TyMethod,
Method,
StructField,
Variant,
Macro,
Primitive,
AssocType,
Constant,
AssocConst,
Union,
ForeignType,
Keyword,
OpaqueTy,
ProcAttribute,
ProcDerive,
TraitAlias,
}
#[derive(Clone, Debug)]

View File

@ -60,6 +60,7 @@ struct CrateData {
#[derive(Debug, PartialEq, serde_tuple::Deserialize_tuple)]
struct ItemData {
#[serde(deserialize_with = "deserialize_item_type")]
ty: doc::ItemType,
name: String,
path: String,
@ -68,6 +69,44 @@ struct ItemData {
_ignored: serde_json::Value,
}
fn deserialize_item_type<'de, D>(d: D) -> Result<doc::ItemType, D::Error>
where
D: serde::de::Deserializer<'de>,
{
use doc::ItemType;
use serde::de::{Deserialize, Error};
match u8::deserialize(d)? {
0 => Ok(ItemType::Module),
1 => Ok(ItemType::ExternCrate),
2 => Ok(ItemType::Import),
3 => Ok(ItemType::Struct),
4 => Ok(ItemType::Enum),
5 => Ok(ItemType::Function),
6 => Ok(ItemType::Typedef),
7 => Ok(ItemType::Static),
8 => Ok(ItemType::Trait),
9 => Ok(ItemType::Impl),
10 => Ok(ItemType::TyMethod),
11 => Ok(ItemType::Method),
12 => Ok(ItemType::StructField),
13 => Ok(ItemType::Variant),
14 => Ok(ItemType::Macro),
15 => Ok(ItemType::Primitive),
16 => Ok(ItemType::AssocType),
17 => Ok(ItemType::Constant),
18 => Ok(ItemType::AssocConst),
19 => Ok(ItemType::Union),
20 => Ok(ItemType::ForeignType),
21 => Ok(ItemType::Keyword),
22 => Ok(ItemType::OpaqueTy),
23 => Ok(ItemType::ProcAttribute),
24 => Ok(ItemType::ProcDerive),
25 => Ok(ItemType::TraitAlias),
_ => Err(D::Error::custom("Unexpected item type")),
}
}
impl Index {
pub fn load(path: impl AsRef<path::Path>) -> anyhow::Result<Option<Self>> {
use std::io::BufRead;