/*
* Copyright (c) 2021, 2022 StorPool <support@storpool.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
//! Partially-generated data for handling build variants.
//!
//! The data is autogenerated from the supported StorPool build variants.
use std::collections::HashMap;
use std::str::FromStr;
use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize};
use crate::{
Builder, DebRepo, Detect, Repo, Variant, VariantDefTop, VariantError, VariantFormat,
VariantFormatVersion, YumRepo,
};
/// The supported StorPool build variants (OS distribution, version, etc).
#[derive(Debug, Clone, PartialEq, Hash, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum VariantKind {
{%- for name, var in variants|dictvsort -%}
/// {{ var.descr }}
{{ name }},
{%- endfor -%}
}
impl VariantKind {
{%- for name in variants|vsort -%}
const {{ name }}_NAME: &'static str = "{{ name }}";
{%- endfor -%}
}
impl AsRef<str> for VariantKind {
#[inline]
fn as_ref(&self) -> &str {
match *self {
{%- for name in variants|vsort -%}
Self::{{ name }} => Self::{{ name }}_NAME,
{%- endfor -%}
}
}
}
impl FromStr for VariantKind {
type Err = VariantError;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
{%- for name in variants|vsort -%}
Self::{{ name }}_NAME => Ok(Self::{{ name }}),
{%- endfor -%}
other => Err(VariantError::BadVariant(other.to_owned())),
}
}
}
/// Return the definition of the StorPool variants.
#[allow(clippy::manual_string_new)]
#[allow(clippy::panic)]
#[allow(clippy::too_many_lines)]
#[must_use]
pub fn get_variants() -> &'static VariantDefTop {
static DEF_TOP: Lazy<VariantDefTop> = Lazy::new(|| {
VariantDefTop {
format: VariantFormat {
version: VariantFormatVersion {
major: {{ format_version[0] }},
minor: {{ format_version[1] }},
},
},
order: vec![
{%- for name in order %}
VariantKind::{{name}},
{%- endfor %}
],
variants: HashMap::from(
[
{%- for name, var in variants|dictvsort %}
(
VariantKind::{{ name }},
Variant {
kind: VariantKind::{{ name }},
descr: "{{ var.descr }}".to_owned(),
family: "{{ var.family }}".to_owned(),
parent: "{{ var.parent }}".to_owned(),
detect: Detect {
filename: "{{ var.detect.filename }}".to_owned(),
regex: r"{{ var.detect.regex.pattern }}".to_owned(),
os_id: "{{ var.detect.os_id }}".to_owned(),
os_version_regex: r"{{ var.detect.os_version_regex.pattern }}".to_owned(),
},
commands: HashMap::from(
[
{%- for cat_name in var.commands._fields|sort %}
(
"{{ cat_name }}".to_owned(),
HashMap::from(
[
{%- for cmd_name in var.commands|attr(cat_name)|attr("_fields")|sort %}
(
"{{ cmd_name }}".to_owned(),
vec![
{%- for word in var.commands|attr(cat_name)|attr(cmd_name) %}
"{{ word|replace("\\", "\\\\")|replace('"', '\\"') }}".to_owned(),
{%- endfor %}
],
),
{%- endfor %}
]
),
),
{%- endfor %}
]
),
min_sys_python: "{{ var.min_sys_python }}".to_owned(),
repo: {%- if var.family == "redhat" %}
Repo::Yum(YumRepo {
yumdef: "{{ var.repo.yumdef }}".to_owned(),
keyring: "{{ var.repo.keyring }}".to_owned(),
}),
{% elif var.family == "debian" %}
Repo::Deb(DebRepo {
codename: "{{ var.repo.codename }}".to_owned(),
vendor: "{{ var.repo.vendor }}".to_owned(),
sources: "{{ var.repo.sources }}".to_owned(),
keyring: "{{ var.repo.keyring }}".to_owned(),
req_packages: vec![
{%- for pkg in var.repo.req_packages %}
"{{ pkg }}".to_owned(),
{%- endfor %}
],
}),
{% else %}
INVALID!,
{% endif -%}
package: HashMap::from(
[
{%- for pkg_name, pkg in var.package|dictsort %}
("{{ pkg_name }}".to_owned(), "{{ pkg }}".to_owned()),
{%- endfor %}
]
),
systemd_lib: "{{ var.systemd_lib }}".to_owned(),
file_ext: "{{ var.file_ext }}".to_owned(),
initramfs_flavor: "{{ var.initramfs_flavor }}".to_owned(),
builder: Builder {
alias: "{{ var.builder.alias }}".to_owned(),
base_image: "{{ var.builder.base_image }}".to_owned(),
branch: "{{ var.builder.branch }}".to_owned(),
kernel_package: "{{ var.builder.kernel_package }}".to_owned(),
utf8_locale: "{{ var.builder.utf8_locale }}".to_owned(),
},
},
),
{%- endfor %}
]
),
version: "{{ version }}".to_owned(),
}
});
assert!(
DEF_TOP.format.version.major == 1,
"Internal error: JSON variant definition: version {:?}",
DEF_TOP.format.version
);
&DEF_TOP
}