From 48c49fa57023229f7600b557e67a9a583c3e7f5f Mon Sep 17 00:00:00 2001 From: John DiSanti Date: Tue, 29 Jun 2021 15:51:18 -0700 Subject: [PATCH] Fix bare links in docs against Rust 1.53 --- aws/rust-runtime/aws-types/src/region.rs | 2 +- aws/sdk/examples/dynamo-movies/src/main.rs | 2 +- .../examples/eks/src/bin/create-cluster.rs | 2 +- .../rust/codegen/rustlang/RustWriter.kt | 17 +++++++++++++++- .../amazon/smithy/rust/lang/RustWriterTest.kt | 20 +++++++++++++++++++ rust-runtime/smithy-types/src/lib.rs | 2 +- 6 files changed, 40 insertions(+), 5 deletions(-) diff --git a/aws/rust-runtime/aws-types/src/region.rs b/aws/rust-runtime/aws-types/src/region.rs index d99182d4499..2fca8deb1c5 100644 --- a/aws/rust-runtime/aws-types/src/region.rs +++ b/aws/rust-runtime/aws-types/src/region.rs @@ -12,7 +12,7 @@ use std::env::VarError; /// per-client basis unless otherwise noted. A full list of regions is found in the /// "Regions and Endpoints" document. /// -/// See http://docs.aws.amazon.com/general/latest/gr/rande.html for +/// See for /// information on AWS regions. #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct Region( diff --git a/aws/sdk/examples/dynamo-movies/src/main.rs b/aws/sdk/examples/dynamo-movies/src/main.rs index 2a62df5d1be..6c34ec5aff2 100644 --- a/aws/sdk/examples/dynamo-movies/src/main.rs +++ b/aws/sdk/examples/dynamo-movies/src/main.rs @@ -22,7 +22,7 @@ use smithy_types::retry::RetryKind; use std::collections::HashMap; use std::time::Duration; -/// A partial reimplementation of https://docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/GettingStarted.Ruby.html +/// A partial reimplementation of /// in Rust /// /// - Create table diff --git a/aws/sdk/examples/eks/src/bin/create-cluster.rs b/aws/sdk/examples/eks/src/bin/create-cluster.rs index a53ba33f010..ab2d8382d38 100644 --- a/aws/sdk/examples/eks/src/bin/create-cluster.rs +++ b/aws/sdk/examples/eks/src/bin/create-cluster.rs @@ -17,7 +17,7 @@ struct Opt { /// To create a role-arn: /// /// 1. Follow instructions to create an IAM role: - /// https://docs.aws.amazon.com/eks/latest/userguide/service_IAM_role.html + /// /// /// 2. Copy role arn #[structopt(long)] diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/rustlang/RustWriter.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/rustlang/RustWriter.kt index f683e4ca0b2..ca012a91e98 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/rustlang/RustWriter.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/rustlang/RustWriter.kt @@ -204,6 +204,7 @@ fun T.docs(text: String, vararg args: Any, newlinePrefix: Strin // We need to filter out blank lines—an empty line causes the markdown parser to interpret the subsequent // docs as a code block because they are indented. .filter { it.isNotBlank() } + .map { fixBareUrls(it, expressionStart) } .joinToString("\n") { // Rustdoc warns on tabs in documentation it.trimStart().replace("\t", " ") @@ -213,8 +214,22 @@ fun T.docs(text: String, vararg args: Any, newlinePrefix: Strin return this } +private val BARE_URL_MATCHER = Regex("""(https?\:\/\/[^\s]+)""") +private fun fixBareUrls(line: String, expressionStart: Char): String = BARE_URL_MATCHER.replace(line) { match -> + // If the URL is prepended with quotes, it's likely inside of an anchor tag, so don't attempt to fix it + if (match.range.first > 0 && line[match.range.first - 1] != '"') { + val escaped = escape(match.value, expressionStart) + "<$escaped>" + } else { + match.value + } +} + /** Escape the [expressionStart] character to avoid problems during formatting */ -fun CodeWriter.escape(text: String): String = text.replace("$expressionStart", "$expressionStart$expressionStart") +fun CodeWriter.escape(text: String): String = escape(text, expressionStart) + +private fun escape(text: String, expressionStart: Char): String = + text.replace("$expressionStart", "$expressionStart$expressionStart") /** * Write _exactly_ the text as written into the code writer without newlines or formatting diff --git a/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/RustWriterTest.kt b/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/RustWriterTest.kt index bddc2b239d6..c463314f612 100644 --- a/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/RustWriterTest.kt +++ b/codegen/src/test/kotlin/software/amazon/smithy/rust/lang/RustWriterTest.kt @@ -112,4 +112,24 @@ class RustWriterTest { sut.docs("A link! #D", symbol) sut.toString() shouldContain "/// A link! [`Foo`](crate::model::Foo)" } + + @Test + fun `generate doc fix bare links`() { + val sut = RustWriter.forModule("lib") + sut.docs( + """ + Simple link in this line: https://example.com + Multiple http://test.org?something=something#foo URLs https://www.test.co.uk/asdf%20asdf?foo=%3C&bar + Real http://tools.ietf.org/html/rfc6902#section-4 link + Don't touch html links. + """.trimMargin() + ) + sut.toString() shouldContain + """ + /// Simple link in this line: + /// Multiple URLs + /// Real link + /// Don't touch html links. + """.trimIndent() + } } diff --git a/rust-runtime/smithy-types/src/lib.rs b/rust-runtime/smithy-types/src/lib.rs index 733d1fc2fef..48ff067c8c2 100644 --- a/rust-runtime/smithy-types/src/lib.rs +++ b/rust-runtime/smithy-types/src/lib.rs @@ -52,7 +52,7 @@ pub enum Document { } /// A number type that implements Javascript / JSON semantics, modeled on serde_json: -/// https://docs.serde.rs/src/serde_json/number.rs.html#20-22 +/// #[derive(Debug, Clone, Copy, PartialEq)] pub enum Number { PosInt(u64),