Skip to content

Commit da43ad0

Browse files
committed
fix: relative links in gleam toml
Signed-off-by: Courtcircuits <[email protected]>
1 parent c9ad0e0 commit da43ad0

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

compiler-cli/src/publish.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use gleam_core::{
1616
type_,
1717
};
1818
use hexpm::version::{Range, Version};
19+
use http::Uri;
1920
use itertools::Itertools;
2021
use sha2::Digest;
2122
use std::{collections::HashMap, io::Write, path::PathBuf, time::Instant};
@@ -501,7 +502,7 @@ fn metadata_config<'a>(
501502
source_files: &[Utf8PathBuf],
502503
generated_files: &[(Utf8PathBuf, String)],
503504
) -> Result<String> {
504-
let repo_url = http::Uri::try_from(
505+
let repo_url = Uri::try_from(
505506
config
506507
.repository
507508
.as_ref()
@@ -522,19 +523,23 @@ fn metadata_config<'a>(
522523
}),
523524
})
524525
.collect();
526+
let links: Vec<(&str, Uri)> = config
527+
.links
528+
.iter()
529+
.map(|l| (l.title.as_str(), l.href.clone()))
530+
.filter(|(_, href)| !href.clone().is_internal())
531+
.map(|(title, href)| (title, href.as_uri().unwrap()))
532+
.chain(repo_url.into_iter().map(|u| ("Repository", u)))
533+
.collect();
534+
525535
let metadata = ReleaseMetadata {
526536
name: &config.name,
527537
version: &config.version,
528538
description: &config.description,
529539
source_files,
530540
generated_files,
531541
licenses: &config.licences,
532-
links: config
533-
.links
534-
.iter()
535-
.map(|l| (l.title.as_str(), l.href.clone()))
536-
.chain(repo_url.into_iter().map(|u| ("Repository", u)))
537-
.collect(),
542+
links: links,
538543
requirements: requirements?,
539544
build_tools: vec!["gleam"],
540545
}
@@ -671,7 +676,7 @@ pub struct ReleaseMetadata<'a> {
671676
source_files: &'a [Utf8PathBuf],
672677
generated_files: &'a [(Utf8PathBuf, String)],
673678
licenses: &'a Vec<SpdxLicense>,
674-
links: Vec<(&'a str, http::Uri)>,
679+
links: Vec<(&'a str, Uri)>,
675680
requirements: Vec<ReleaseRequirement<'a>>,
676681
build_tools: Vec<&'a str>,
677682
// What should this be? I can't find it in the API anywhere.
@@ -680,7 +685,7 @@ pub struct ReleaseMetadata<'a> {
680685

681686
impl ReleaseMetadata<'_> {
682687
pub fn as_erlang(&self) -> String {
683-
fn link(link: &(&str, http::Uri)) -> String {
688+
fn link(link: &(&str, Uri)) -> String {
684689
format!(
685690
"\n {{<<\"{name}\">>, <<\"{url}\">>}}",
686691
name = link.0,

compiler-core/src/config.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,11 +980,44 @@ pub struct DocsPage {
980980
pub source: Utf8PathBuf,
981981
}
982982

983+
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
984+
#[serde(untagged)]
985+
pub enum Href {
986+
#[serde(with = "uri_serde")]
987+
External(Uri),
988+
Internal(std::path::PathBuf),
989+
}
990+
991+
impl fmt::Display for Href {
992+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
993+
match self {
994+
Href::External(uri) => write!(f, "{}", uri),
995+
Href::Internal(path) => write!(f, "{}", path.display()),
996+
}
997+
}
998+
}
999+
1000+
impl Href {
1001+
pub fn is_internal(&self) -> bool {
1002+
match self {
1003+
Href::Internal(_) => true,
1004+
_ => false,
1005+
}
1006+
}
1007+
1008+
// only URIs are considered as external links
1009+
pub fn as_uri(&self) -> Option<Uri> {
1010+
match self {
1011+
Href::External(uri) => Some(uri.clone()),
1012+
Href::Internal(_) => None,
1013+
}
1014+
}
1015+
}
1016+
9831017
#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone)]
9841018
pub struct Link {
9851019
pub title: String,
986-
#[serde(with = "uri_serde")]
987-
pub href: Uri,
1020+
pub href: Href,
9881021
}
9891022

9901023
// Note we don't use http-serde since we also want to validate the scheme and host is set.
@@ -1141,7 +1174,7 @@ licences = ["Apache-2.0", "MIT"]
11411174
description = "Pretty complex config"
11421175
target = "erlang"
11431176
repository = { type = "github", user = "example", repo = "my_dep" }
1144-
links = [{ title = "Home page", href = "https://example.com" }]
1177+
links = [{ title = "Home page", href = "https://example.com" }, { title = "Internal link", href = "./internal" }, { title = "Second internal link", href = "../internal" }, { title = "Third internal link", href = "internal" } ]
11451178
internal_modules = ["my_app/internal"]
11461179
gleam = ">= 0.30.0"
11471180

compiler-core/src/snapshots/gleam_core__config__package_config_to_json.snap

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
---
22
source: compiler-core/src/config.rs
3-
assertion_line: 1171
43
expression: output
5-
snapshot_kind: text
64
---
75
--- GLEAM.TOML
86

@@ -12,7 +10,7 @@ licences = ["Apache-2.0", "MIT"]
1210
description = "Pretty complex config"
1311
target = "erlang"
1412
repository = { type = "github", user = "example", repo = "my_dep" }
15-
links = [{ title = "Home page", href = "https://example.com" }]
13+
links = [{ title = "Home page", href = "https://example.com" }, { title = "Internal link", href = "./internal" }, { title = "Second internal link", href = "../internal" }, { title = "Third internal link", href = "internal" } ]
1614
internal_modules = ["my_app/internal"]
1715
gleam = ">= 0.30.0"
1816

@@ -86,6 +84,18 @@ allow_read = ["./database.sqlite"]
8684
{
8785
"title": "Home page",
8886
"href": "https://example.com/"
87+
},
88+
{
89+
"title": "Internal link",
90+
"href": "./internal"
91+
},
92+
{
93+
"title": "Second internal link",
94+
"href": "../internal"
95+
},
96+
{
97+
"title": "Third internal link",
98+
"href": "internal"
8999
}
90100
],
91101
"erlang": {

0 commit comments

Comments
 (0)