|
1 | 1 | //! 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}; |
3 | 3 |
|
4 | 4 | use semver::Version;
|
5 | 5 | use snafu::{ResultExt, Snafu};
|
@@ -34,26 +34,36 @@ pub enum Error {
|
34 | 34 | ParseUtf8Bytes { source: std::string::FromUtf8Error },
|
35 | 35 | }
|
36 | 36 |
|
37 |
| -pub(crate) struct DocUrlReplacer<'a>(&'a str); |
| 37 | +pub(crate) struct DocUrlReplacer(Version); |
38 | 38 |
|
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)) |
42 | 48 | }
|
43 | 49 |
|
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}") |
50 | 60 | }
|
51 | 61 | };
|
52 | 62 |
|
53 |
| - Ok(input.replace( |
| 63 | + input.replace( |
54 | 64 | STACKABLE_DOCS_HOME_URL_PLACEHOLDER,
|
55 | 65 | &format!("{STACKABLE_DOCS_HOME_BASE_URL}/{docs_version}"),
|
56 |
| - )) |
| 66 | + ) |
57 | 67 | }
|
58 | 68 | }
|
59 | 69 |
|
@@ -96,8 +106,8 @@ pub trait YamlSchema: Sized + serde::Serialize {
|
96 | 106 |
|
97 | 107 | let yaml_string = String::from_utf8(buffer).context(ParseUtf8BytesSnafu)?;
|
98 | 108 |
|
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); |
101 | 111 |
|
102 | 112 | Ok(yaml_string)
|
103 | 113 | }
|
|
0 commit comments