Skip to content

Commit 2af58f6

Browse files
committed
chore: Parse SemVer when creating DocUrlReplacer
1 parent 41808c1 commit 2af58f6

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

crates/stackable-shared/src/yaml.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Utility functions for processing data in the YAML file format
2-
use std::{io::Write, path::Path, str::FromStr};
2+
use std::{io::Write, path::Path};
33

44
use semver::Version;
55
use snafu::{ResultExt, Snafu};
@@ -34,26 +34,36 @@ pub enum Error {
3434
ParseUtf8Bytes { source: std::string::FromUtf8Error },
3535
}
3636

37-
pub(crate) struct DocUrlReplacer<'a>(&'a str);
37+
pub(crate) struct DocUrlReplacer(Version);
3838

39-
impl<'a> DocUrlReplacer<'a> {
40-
pub(crate) fn new(operator_version: &'a str) -> Self {
41-
Self(operator_version)
39+
impl DocUrlReplacer {
40+
pub(crate) fn new(operator_version: &str) -> Result<Self> {
41+
let version = operator_version
42+
.parse()
43+
.context(ParseSemanticVersionSnafu {
44+
input: operator_version,
45+
})?;
46+
47+
Ok(Self(version))
4248
}
4349

44-
fn replace(&self, input: &str) -> Result<String> {
45-
let docs_version = match self.0 {
46-
"0.0.0-dev" => "nightly".to_owned(),
47-
ver => {
48-
let v = Version::from_str(ver).context(ParseSemanticVersionSnafu { input })?;
49-
format!("{major}.{minor}", major = v.major, minor = v.minor)
50+
fn replace(&self, input: &str) -> String {
51+
let docs_version = match (
52+
self.0.major,
53+
self.0.minor,
54+
self.0.patch,
55+
self.0.pre.as_str(),
56+
) {
57+
(0, 0, 0, "dev") => "nightly".to_owned(),
58+
(major, minor, ..) => {
59+
format!("{major}.{minor}")
5060
}
5161
};
5262

53-
Ok(input.replace(
63+
input.replace(
5464
STACKABLE_DOCS_HOME_URL_PLACEHOLDER,
5565
&format!("{STACKABLE_DOCS_HOME_BASE_URL}/{docs_version}"),
56-
))
66+
)
5767
}
5868
}
5969

@@ -96,8 +106,8 @@ pub trait YamlSchema: Sized + serde::Serialize {
96106

97107
let yaml_string = String::from_utf8(buffer).context(ParseUtf8BytesSnafu)?;
98108

99-
let replacer = DocUrlReplacer::new(operator_version);
100-
let yaml_string = replacer.replace(&yaml_string)?;
109+
let replacer = DocUrlReplacer::new(operator_version)?;
110+
let yaml_string = replacer.replace(&yaml_string);
101111

102112
Ok(yaml_string)
103113
}

0 commit comments

Comments
 (0)