Skip to content

Commit 6cf5c0f

Browse files
committed
Overrides into their own module
1 parent ff400bc commit 6cf5c0f

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

src/packages/override.gleam

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
pub fn is_ignored_package(name: String) -> Bool {
2+
case name {
3+
"bare_package1"
4+
| "bare_package_one"
5+
| "bare_package_two"
6+
| "first_gleam_publish_package"
7+
| "gleam_module_javascript_test"
8+
| // Reserved official sounding names.
9+
"gleam"
10+
| "gleam_deno"
11+
| "gleam_email"
12+
| "gleam_html"
13+
| "gleam_nodejs"
14+
| "gleam_tcp"
15+
| "gleam_test"
16+
| "gleam_toml"
17+
| "gleam_xml"
18+
| "gleam_mongo"
19+
| "gleam_bson"
20+
| "gleam_file"
21+
| "gleam_yaml"
22+
| // Unofficial packages impersonating the core team
23+
"gleam_dotenv"
24+
| "gleam_roman"
25+
| "gleam_sendgrid"
26+
| "gleam_bbmustache"
27+
| // Reserved unreleased project names.
28+
"glitter"
29+
| "sequin" -> True
30+
31+
_ -> False
32+
}
33+
}
34+
35+
/// Some words have common misspellings or associated words so we add those to
36+
/// the search to get all appropriate results.
37+
pub fn expand_search_term(term: String) -> List(String) {
38+
case term {
39+
"postgres" | "postgresql" -> ["postgres", "postgresql"]
40+
"mysql" | "mariadb" -> ["mysql", "mariadb"]
41+
"redis" | "valkey" -> ["redis", "valkey"]
42+
"regex" | "regexp" -> ["regex", "regexp"]
43+
"luster" -> ["luster", "lustre"]
44+
"mail" -> ["mail", "email"]
45+
term -> [term]
46+
}
47+
}

src/packages/storage.gleam

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import gleam/string
1313
import gleam/time/calendar
1414
import gleam/time/timestamp.{type Timestamp}
1515
import packages/error.{type Error}
16+
import packages/override
1617
import storail.{type Collection}
1718

1819
pub opaque type Database {
@@ -53,19 +54,6 @@ pub fn initialise(storage_path: String) -> Database {
5354
Database(hex_sync_times:, packages:, releases:)
5455
}
5556

56-
const ignored_packages = [
57-
"bare_package1", "bare_package_one", "bare_package_two",
58-
"first_gleam_publish_package", "gleam_module_javascript_test",
59-
// Reserved official sounding names.
60-
"gleam", "gleam_deno", "gleam_email", "gleam_html", "gleam_nodejs",
61-
"gleam_tcp", "gleam_test", "gleam_toml", "gleam_xml", "gleam_mongo",
62-
"gleam_bson", "gleam_file", "gleam_yaml",
63-
// Unofficial packages impersonating the core team
64-
"gleam_dotenv", "gleam_roman", "gleam_sendgrid", "gleam_bbmustache",
65-
// Reserved unreleased project names.
66-
"glitter", "sequin",
67-
]
68-
6957
fn gleam_package_epoch() -> Timestamp {
7058
timestamp.from_unix_seconds(1_635_092_380)
7159
}
@@ -305,7 +293,7 @@ pub fn upsert_package_from_hex(
305293
package: hexpm.Package,
306294
latest_version latest_version: String,
307295
) -> Result(Nil, Error) {
308-
case is_ignored_package(package.name) {
296+
case override.is_ignored_package(package.name) {
309297
True -> Ok(Nil)
310298
False -> {
311299
database.packages
@@ -316,10 +304,6 @@ pub fn upsert_package_from_hex(
316304
}
317305
}
318306

319-
pub fn is_ignored_package(name: String) -> Bool {
320-
list.contains(ignored_packages, name)
321-
}
322-
323307
pub fn get_package(database: Database, name: String) -> Result(Package, Error) {
324308
database.packages
325309
|> storail.key(name)
@@ -387,7 +371,8 @@ pub fn list_releases(
387371

388372
pub fn list_packages(database: Database) -> Result(List(String), Error) {
389373
case storail.list(database.packages, []) {
390-
Ok(packages) -> Ok(list.filter(packages, fn(p) { !is_ignored_package(p) }))
374+
Ok(packages) ->
375+
Ok(list.filter(packages, fn(p) { !override.is_ignored_package(p) }))
391376
Error(e) -> Error(error.StorageError(e))
392377
}
393378
}

src/packages/text_search.gleam

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import gleam/order
77
import gleam/result
88
import gleam/string
99
import packages/error.{type Error}
10-
import packages/storage
10+
import packages/override
1111
import porter_stemmer
1212

1313
pub opaque type TextSearchIndex {
@@ -23,7 +23,7 @@ pub fn insert(
2323
name name: String,
2424
description description: String,
2525
) -> Result(Nil, Error) {
26-
case storage.is_ignored_package(name) {
26+
case override.is_ignored_package(name) {
2727
True -> Ok(Nil)
2828
False ->
2929
name
@@ -52,7 +52,7 @@ pub fn lookup(
5252
) -> Result(List(String), Error) {
5353
let phrase = string.lowercase(phrase)
5454
stem_words(phrase)
55-
|> list.flat_map(expand_search_term)
55+
|> list.flat_map(override.expand_search_term)
5656
|> list.try_map(ethos.get(index.table, _))
5757
|> result.map(fn(names) {
5858
names
@@ -87,20 +87,6 @@ pub fn lookup(
8787
|> result.replace_error(error.EtsTableError)
8888
}
8989

90-
/// Some words have common misspellings or associated words so we add those to
91-
/// the search to get all appropriate results.
92-
fn expand_search_term(term: String) -> List(String) {
93-
case term {
94-
"postgres" | "postgresql" -> ["postgres", "postgresql"]
95-
"mysql" | "mariadb" -> ["mysql", "mariadb"]
96-
"redis" | "valkey" -> ["redis", "valkey"]
97-
"regex" | "regexp" -> ["regex", "regexp"]
98-
"luster" -> ["luster", "lustre"]
99-
"mail" -> ["mail", "email"]
100-
term -> [term]
101-
}
102-
}
103-
10490
fn remove(index: TextSearchIndex, name: String) -> Result(Nil, Error) {
10591
ethos.delete_value(index.table, name)
10692
|> result.replace_error(error.EtsTableError)

0 commit comments

Comments
 (0)