use crate::Version;
use serde_json::Value;
use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Type {
String,
Bool,
Enum,
List,
Uint,
Int,
Float,
Path,
}
impl Type {
pub const fn as_str(self) -> &'static str {
match self {
Type::String => "string",
Type::Bool => "bool",
Type::Enum => "enum",
Type::List => "list",
Type::Uint => "uint",
Type::Int => "int",
Type::Float => "float",
Type::Path => "path",
}
}
pub const ALL: &'static [&'static str] = &[
"string", "bool", "enum", "list", "uint", "int", "float", "path",
];
}
impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Stability {
Unknown,
Stable,
Experimental,
Debug,
Deprecated,
}
impl Stability {
pub const fn as_str(self) -> &'static str {
match self {
Stability::Unknown => "unknown",
Stability::Stable => "stable",
Stability::Experimental => "experimental",
Stability::Debug => "debug",
Stability::Deprecated => "deprecated",
}
}
pub const ALL: &'static [&'static str] =
&["unknown", "stable", "experimental", "debug", "deprecated"];
}
impl fmt::Display for Stability {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Since {
Unknown,
This,
At(Version),
}
impl Since {
pub fn resolve(self) -> Option<Version> {
match self {
Since::Unknown => None,
Since::This => Some(crate::THIS_VERSION),
Since::At(v) => Some(v),
}
}
pub(crate) fn to_json(self) -> Option<String> {
self.resolve().map(|v| v.to_string())
}
}
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct ConfigKeyRef(pub &'static str);
impl ConfigKeyRef {
pub const fn new(s: &'static str) -> ConfigKeyRef {
let b = s.as_bytes();
let mut i = 0usize;
let mut dot = false;
while i < b.len() {
if b[i] == b'.' {
dot = true;
}
i += 1;
}
if !dot {
panic!("config key must be namespaced, e.g. \"section.key\"");
}
ConfigKeyRef(s)
}
}
impl fmt::Display for ConfigKeyRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.0)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Deprecation {
pub since: Version,
pub replaced_by: Option<fn() -> &'static str>,
pub migration: &'static str,
}
impl Deprecation {
pub(crate) fn to_json(self) -> Value {
serde_json::json!({
"since": self.since.to_string(),
"replaced_by": self.replaced_by.map(|f| f()),
"migration": if self.migration.is_empty() { None } else { Some(self.migration) },
})
}
}