forked from amphitheatre-app/common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.rs
100 lines (94 loc) · 3.8 KB
/
character.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) The Amphitheatre Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::{collections::HashMap, fs::read_to_string, path::Path};
use super::{Build, Deploy, Partner};
use anyhow::anyhow;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use utoipa::ToSchema;
use validator::Validate;
#[derive(Clone, Debug, Default, Deserialize, Eq, JsonSchema, Serialize, PartialEq, Validate)]
pub struct Character {
/// Contains all the information about a character.
#[serde(flatten)]
pub meta: Metadata,
/// Describes how images are built.
#[serde(skip_serializing_if = "Option::is_none")]
pub build: Option<Build>,
/// Describes how images are deploy.
#[serde(skip_serializing_if = "Option::is_none")]
pub deploy: Option<Deploy>,
/// Depend on other partners from other repositories,
/// or subdirectories on your local file system.
#[serde(skip_serializing_if = "Option::is_none")]
pub partners: Option<HashMap<String, Partner>>,
}
impl Character {
pub fn new(name: &str) -> Self {
Self {
meta: Metadata {
name: name.to_string(),
..Default::default()
},
..Default::default()
}
}
/// Load the character from the specified file.
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, anyhow::Error> {
let content = read_to_string(path).map_err(|e| anyhow!(e.to_string()))?;
toml::from_str(&content).map_err(|e| anyhow!(e.to_string()))
}
}
/// Contains all the information about a character.
#[derive(Clone, Debug, Default, Deserialize, Eq, JsonSchema, Serialize, PartialEq, Validate, ToSchema)]
pub struct Metadata {
/// The name of the character.
pub name: String,
/// The version of the character.
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
/// The authors of the character.
#[serde(skip_serializing_if = "Option::is_none")]
pub authors: Option<Vec<String>>,
/// A description of the character.
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
/// URL of the character documentation.
#[serde(skip_serializing_if = "Option::is_none")]
pub documentation: Option<String>,
/// Path to the character's README file.
#[serde(skip_serializing_if = "Option::is_none")]
pub readme: Option<String>,
/// URL of the character homepage.
#[serde(skip_serializing_if = "Option::is_none")]
pub homepage: Option<String>,
/// URL of the character source repository.
/// e.g. https://github.com/amphitheatre-app/amphitheatre.git.
pub repository: String,
/// The character license.
#[serde(skip_serializing_if = "Option::is_none")]
pub license: Option<String>,
/// Path to the text of the license.
#[serde(skip_serializing_if = "Option::is_none")]
pub license_file: Option<String>,
/// Keywords for the character.
#[serde(skip_serializing_if = "Option::is_none")]
pub keywords: Option<Vec<String>>,
/// Categories of the character.
#[serde(skip_serializing_if = "Option::is_none")]
pub categories: Option<Vec<String>>,
/// Can be used to prevent publishing the character.
#[serde(skip_serializing_if = "Option::is_none")]
pub publish: Option<Vec<String>>,
}