Skip to content

Commit 4824652

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

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};
@@ -526,7 +527,7 @@ fn metadata_config<'a>(
526527
source_files: &[Utf8PathBuf],
527528
generated_files: &[(Utf8PathBuf, String)],
528529
) -> Result<String> {
529-
let repo_url = http::Uri::try_from(
530+
let repo_url = Uri::try_from(
530531
config
531532
.repository
532533
.as_ref()
@@ -547,19 +548,23 @@ fn metadata_config<'a>(
547548
}),
548549
})
549550
.collect();
551+
let links: Vec<(&str, Uri)> = config
552+
.links
553+
.iter()
554+
.map(|l| (l.title.as_str(), l.href.clone()))
555+
.filter(|(_, href)| !href.clone().is_internal())
556+
.map(|(title, href)| (title, href.as_uri().unwrap()))
557+
.chain(repo_url.into_iter().map(|u| ("Repository", u)))
558+
.collect();
559+
550560
let metadata = ReleaseMetadata {
551561
name: &config.name,
552562
version: &config.version,
553563
description: &config.description,
554564
source_files,
555565
generated_files,
556566
licenses: &config.licences,
557-
links: config
558-
.links
559-
.iter()
560-
.map(|l| (l.title.as_str(), l.href.clone()))
561-
.chain(repo_url.into_iter().map(|u| ("Repository", u)))
562-
.collect(),
567+
links: links,
563568
requirements: requirements?,
564569
build_tools: vec!["gleam"],
565570
}
@@ -696,7 +701,7 @@ pub struct ReleaseMetadata<'a> {
696701
source_files: &'a [Utf8PathBuf],
697702
generated_files: &'a [(Utf8PathBuf, String)],
698703
licenses: &'a Vec<SpdxLicense>,
699-
links: Vec<(&'a str, http::Uri)>,
704+
links: Vec<(&'a str, Uri)>,
700705
requirements: Vec<ReleaseRequirement<'a>>,
701706
build_tools: Vec<&'a str>,
702707
// What should this be? I can't find it in the API anywhere.
@@ -705,7 +710,7 @@ pub struct ReleaseMetadata<'a> {
705710

706711
impl ReleaseMetadata<'_> {
707712
pub fn as_erlang(&self) -> String {
708-
fn link(link: &(&str, http::Uri)) -> String {
713+
fn link(link: &(&str, Uri)) -> String {
709714
format!(
710715
"\n {{<<\"{name}\">>, <<\"{url}\">>}}",
711716
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)