From 06a3411991255097f01d33685f3cfdfb0ce1af46 Mon Sep 17 00:00:00 2001
From: milesj The format for files and URLs are derived from the trailing extension. Example: The second pattern is with a list of strings, allowing multiple files or URLs to be extended. Each
@@ -211,8 +211,8 @@ Example: And lastly, supporting both a string or a list, using our built-in enum.Fallback va
diff --git a/config/enum/index.html b/config/enum/index.html
index 692f732c..792ec2d6 100644
--- a/config/enum/index.html
+++ b/config/enum/index.html
@@ -217,7 +217,7 @@ #![allow(unused)]
fn main() {
-let qux = Value::from_str("qux")?; // Value::Other("qux")
+let qux = Value::from_str("qux")?; // Value::Other("qux")
}
Attribute f
@@ -257,9 +257,9 @@
diff --git a/config/index.html b/config/index.html
index 9058984a..00087dae 100644
--- a/config/index.html
+++ b/config/index.html
@@ -220,13 +220,13 @@ #![allow(unused)]
fn main() {
#[derive(ConfigEnum)]
-#[config(before_parse = "UPPERCASE")]
+#[config(before_parse = "UPPERCASE")]
enum ExampleEnum {
// ...
}
@@ -260,7 +260,7 @@
#![allow(unused)]
fn main() {
#[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
-#[serde(rename_all = "kebab-case")]
+#[serde(rename_all = "kebab-case")]
}
Usage
#[derive(Config)]
struct AppConfig {
- #[setting(default = 3000, env = "PORT")]
+ #[setting(default = 3000, env = "PORT")]
pub port: usize,
#[setting(default = true)]
pub secure: bool,
- #[setting(default = vec!["localhost".into()])]
+ #[setting(default = vec!["localhost".into()])]
pub allowed_hosts: Vec<String>,
}
}Loading sourc
fn main() {
use schematic::Format;
-loader.code("secure: false", Format::Yaml)?;
-loader.file("path/to/config.yml")?;
-loader.url("https://ordomain.com/to/config.yaml")?;
+loader.code("secure: false", Format::Yaml)?;
+loader.file("path/to/config.yml")?;
+loader.url("https://ordomain.com/to/config.yaml")?;
}
Partials
diff --git a/config/settings.html b/config/settings.html
index ec2f5f8e..772d7aef 100644
--- a/config/settings.html
+++ b/config/settings.html
@@ -209,7 +209,7 @@ #![allow(unused)]
fn main() {
#[derive(Clone, Debug, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize)]
-#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
+#[serde(default, deny_unknown_fields, rename_all = "camelCase")]
pub struct PartialExampleConfig {
- #[serde(skip_serializing_if = "Option::is_none")]
+ #[serde(skip_serializing_if = "Option::is_none")]
pub number: Option<usize>,
- #[serde(skip_serializing_if = "Option::is_none")]
+ #[serde(skip_serializing_if = "Option::is_none")]
pub string: Option<String>,
- #[serde(skip_serializing_if = "Option::is_none")]
+ #[serde(skip_serializing_if = "Option::is_none")]
pub boolean: Option<bool>,
- #[serde(skip_serializing_if = "Option::is_none")]
+ #[serde(skip_serializing_if = "Option::is_none")]
pub array: Option<Vec<String>>,
- #[serde(skip_serializing_if = "Option::is_none")]
+ #[serde(skip_serializing_if = "Option::is_none")]
pub optional: Option<String>,
}
}
Serde support
fn main() {
#[derive(Config)]
struct Example {
- #[setting(rename = "type")]
+ #[setting(rename = "type")]
pub type_of: SomeEnum,
}
}
diff --git a/config/struct/default.html b/config/struct/default.html
index 11ec6630..e48f34ac 100644
--- a/config/struct/default.html
+++ b/config/struct/default.html
@@ -191,7 +191,7 @@
Default values<
fn main() {
#[derive(Config)]
struct AppConfig {
- #[setting(default = "/")]
+ #[setting(default = "/")]
pub base: String,
#[setting(default = 3000)]
@@ -200,7 +200,7 @@
Default values<
#[setting(default = true)]
pub secure: bool,
- #[setting(default = vec!["localhost".into()])]
+ #[setting(default = vec!["localhost".into()])]
pub allowed_hosts: Vec<String>,
}
}
diff --git a/config/struct/env.html b/config/struct/env.html
index 3e6112b8..afb21a26 100644
--- a/config/struct/env.html
+++ b/config/struct/env.html
@@ -186,7 +186,7 @@
E
fn main() {
#[derive(Config)]
struct AppConfig {
- #[setting(default = 3000, env = "PORT")]
+ #[setting(default = 3000, env = "PORT")]
pub port: usize,
}
}
@@ -199,7 +199,7 @@
Contain
#![allow(unused)]
fn main() {
#[derive(Config)]
-#[config(env_prefix = "APP_")]
+#[config(env_prefix = "APP_")]
struct AppConfig {
#[setting(default = 3000)]
pub port: usize,
@@ -212,13 +212,13 @@
Nested prefix
diff --git a/config/struct/extend.html b/config/struct/extend.html
index 15d717e3..147113f9 100644
--- a/config/struct/extend.html
+++ b/config/struct/extend.html
@@ -196,7 +196,7 @@ #![allow(unused)]
fn main() {
#[derive(Config)]
-#[config(env_prefix = "APP_SERVER_")]
+#[config(env_prefix = "APP_SERVER_")]
struct AppServerConfig {
// ...
}
#[derive(Config)]
-#[config(env_prefix = "APP_")]
+#[config(env_prefix = "APP_")]
struct AppConfig {
#[setting(nested)]
pub server: AppServerConfig,
@@ -233,7 +233,7 @@
Parsing values<
fn main() {
#[derive(Config)]
struct AppConfig {
- #[setting(env = "ALLOWED_HOSTS", parse_env = schematic::env::split_comma)]
+ #[setting(env = "ALLOWED_HOSTS", parse_env = schematic::env::split_comma)]
pub allowed_hosts: Vec<String>,
}
}
Single source
}
}
extends: "./another/file.yml"
+
extends: "./another/file.yml"
Multiple sources
Multiple so
}
extends:
- - "./another/file.yml"
- - "https://domain.com/some/other/file.yml"
+ - "./another/file.yml"
+ - "https://domain.com/some/other/file.yml"
Either pattern
Attribute f
#![allow(unused)]
fn main() {
#[derive(Config)]
-#[config(allow_unknown_fields, env_prefix = "EXAMPLE_")]
+#[config(allow_unknown_fields, env_prefix = "EXAMPLE_")]
struct ExampleConfig {
// ...
}
@@ -234,14 +234,14 @@
Serde support
layer merging.
#![allow(unused)] fn main() { -#[serde(default, deny_unknown_fields, rename_all = "camelCase")] +#[serde(default, deny_unknown_fields, rename_all = "camelCase")] }
However, the deny_unknown_fields
and rename_all
fields can be customized, and we also support
the rename
field, both via the top-level #[config]
attribute.
@@ -226,7 +226,7 @@#![allow(unused)] fn main() { #[derive(Config)] -#[config(allow_unknown_fields, rename = "ExampleConfig", rename_all = "snake_case")] +#[config(allow_unknown_fields, rename = "ExampleConfig", rename_all = "snake_case")] struct Example { // ... } diff --git a/config/struct/validate.html b/config/struct/validate.html index 15946ea9..c499053c 100644 --- a/config/struct/validate.html +++ b/config/struct/validate.html @@ -193,7 +193,7 @@
Validation #[setting(validate = schematic::validate::alphanumeric)] pub secret_key: String, - #[setting(validate = schematic::validate::regex("^\.env"))] + #[setting(validate = schematic::validate::regex("^\.env"))] pub env_file: String, } }
The format for files and URLs are derived from the trailing extension.
@@ -363,7 +363,7 @@Serde support fn main() { #[derive(Config)] struct Example { - #[setting(rename = "type")] + #[setting(rename = "type")] pub type_of: SomeEnum, } } @@ -381,21 +381,21 @@
Serde support
@@ -554,7 +554,7 @@#![allow(unused)] fn main() { #[derive(Clone, Debug, Default, Eq, PartialEq, serde::Deserialize, serde::Serialize)] -#[serde(default, deny_unknown_fields, rename_all = "camelCase")] +#[serde(default, deny_unknown_fields, rename_all = "camelCase")] pub struct PartialExampleConfig { - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub number: Option<usize>, - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub string: Option<String>, - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub boolean: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub array: Option<Vec<String>>, - #[serde(skip_serializing_if = "Option::is_none")] + #[serde(skip_serializing_if = "Option::is_none")] pub optional: Option<String>, } }
Attribu
@@ -677,7 +677,7 @@#![allow(unused)] fn main() { #[derive(Config)] -#[config(allow_unknown_fields, env_prefix = "EXAMPLE_")] +#[config(allow_unknown_fields, env_prefix = "EXAMPLE_")] struct ExampleConfig { // ... } @@ -570,14 +570,14 @@
Serde support layer merging.
#![allow(unused)] fn main() { -#[serde(default, deny_unknown_fields, rename_all = "camelCase")] +#[serde(default, deny_unknown_fields, rename_all = "camelCase")] }
However, the
deny_unknown_fields
andrename_all
fields can be customized, and we also support therename
field, both via the top-level#[config]
attribute.@@ -664,7 +664,7 @@#![allow(unused)] fn main() { #[derive(Config)] -#[config(allow_unknown_fields, rename = "ExampleConfig", rename_all = "snake_case")] +#[config(allow_unknown_fields, rename = "ExampleConfig", rename_all = "snake_case")] struct Example { // ... } @@ -601,7 +601,7 @@
Serde support fn main() { #[derive(Config)] struct AppConfig { - #[setting(default = "/")] + #[setting(default = "/")] pub base: String, #[setting(default = 3000)] @@ -610,7 +610,7 @@
Serde support #[setting(default = true)] pub secure: bool, - #[setting(default = vec!["localhost".into()])] + #[setting(default = vec!["localhost".into()])] pub allowed_hosts: Vec<String>, } }
Context han fn main() { #[derive(Config)] struct AppConfig { - #[setting(default = 3000, env = "PORT")] + #[setting(default = 3000, env = "PORT")] pub port: usize, } }
Contain
#![allow(unused)] fn main() { #[derive(Config)] -#[config(env_prefix = "APP_")] +#[config(env_prefix = "APP_")] struct AppConfig { #[setting(default = 3000)] pub port: usize, @@ -690,13 +690,13 @@
Nested prefix
@@ -757,7 +757,7 @@#![allow(unused)] fn main() { #[derive(Config)] -#[config(env_prefix = "APP_SERVER_")] +#[config(env_prefix = "APP_SERVER_")] struct AppServerConfig { // ... } #[derive(Config)] -#[config(env_prefix = "APP_")] +#[config(env_prefix = "APP_")] struct AppConfig { #[setting(nested)] pub server: AppServerConfig, @@ -711,7 +711,7 @@
Parsing values< fn main() { #[derive(Config)] struct AppConfig { - #[setting(env = "ALLOWED_HOSTS", parse_env = schematic::env::split_comma)] + #[setting(env = "ALLOWED_HOSTS", parse_env = schematic::env::split_comma)] pub allowed_hosts: Vec<String>, } }
Single source } }
Example:
-extends: "./another/file.yml" +
extends: "./another/file.yml"
Multiple sources
The second pattern is with a list of strings, allowing multiple files or URLs to be extended. Each @@ -772,8 +772,8 @@
Multiple so }
Example:
extends: - - "./another/file.yml" - - "https://domain.com/some/other/file.yml" + - "./another/file.yml" + - "https://domain.com/some/other/file.yml"
Either pattern
And lastly, supporting both a string or a list, using our built-in enum.
@@ -859,7 +859,7 @@Context #[setting(validate = schematic::validate::alphanumeric)] pub secret_key: String, - #[setting(validate = schematic::validate::regex("^\.env"))] + #[setting(validate = schematic::validate::regex("^\.env"))] pub env_file: String, } } @@ -892,7 +892,7 @@
Validate context: &Context ) -> Result<(), ValidateError> { if !do_check(value) { - return Err(ValidateError::new("Some failure message")); + return Err(ValidateError::new("Some failure message")); } Ok(()) @@ -916,7 +916,7 @@
Factories
Box::new(move |value, _, _| { if !pattern.is_match(value) { - return Err(ValidateError::new("Some failure message")); + return Err(ValidateError::new("Some failure message")); } Ok(()) @@ -931,7 +931,7 @@Path targeting< use schematic::PathSegment; ValidateError::with_segments( - "Some failure message", + "Some failure message", // [i].key [PathSegment::Index(i), PathSegment::Key(key.to_string())] ) @@ -993,7 +993,7 @@
Attribu
#![allow(unused)] fn main() { #[derive(ConfigEnum)] -#[config(before_parse = "UPPERCASE")] +#[config(before_parse = "UPPERCASE")] enum ExampleEnum { // ... } @@ -1036,7 +1036,7 @@
#![allow(unused)] fn main() { #[derive(Clone, Debug, Eq, PartialEq, serde::Deserialize, serde::Serialize)] -#[serde(rename_all = "kebab-case")] +#[serde(rename_all = "kebab-case")] }
Default variant
To define a default variant, use the
Default
trait and the optional#[default]
variant @@ -1077,7 +1077,7 @@
#![allow(unused)] fn main() { -let qux = Value::from_str("qux")?; // Value::Other("qux") +let qux = Value::from_str("qux")?; // Value::Other("qux") }
Schemas
@@ -1138,12 +1138,12 @@C impl Schematic for User { fn generate_schema() -> SchemaType { SchemaType::structure([ - SchemaField::new("name", SchemaType::String(StringType { + SchemaField::new("name", SchemaType::String(StringType { min_length: Some(1), ..StringType::default() })), - SchemaField::new("age", SchemaType::integer(IntegerKind::Usize)), - SchemaField::new("status", SchemaType::infer::<UserStatus>()), + SchemaField::new("age", SchemaType::integer(IntegerKind::Usize)), + SchemaField::new("status", SchemaType::infer::<UserStatus>()), ]) } } @@ -1201,7 +1201,7 @@
Names
#![allow(unused)] fn main() { let mut schema = SchemaType::string(); -schema.set_name("CustomName"); +schema.set_name("CustomName"); }
Arrays
The
ArrayType
paired with @@ -1302,9 +1302,9 @@Default value fn generate_schema() -> SchemaType { SchemaType::Enum(EnumType { values: vec![ - LiteralValue::String("debug".into()), - LiteralValue::String("error".into()), - LiteralValue::String("warning".into()), + LiteralValue::String("debug".into()), + LiteralValue::String("error".into()), + LiteralValue::String("warning".into()), ], ..EnumType::default() }) @@ -1317,9 +1317,9 @@
Default value
#![allow(unused)] fn main() { SchemaType::enumerable([ - LiteralValue::String("debug".into()), - LiteralValue::String("error".into()), - LiteralValue::String("warning".into()), + LiteralValue::String("debug".into()), + LiteralValue::String("error".into()), + LiteralValue::String("warning".into()), ]); }
Detailed variants
@@ -1332,27 +1332,27 @@Detailed SchemaType::Enum(EnumType { values: vec![ - LiteralValue::String("debug".into()), - LiteralValue::String("error".into()), - LiteralValue::String("warning".into()), + LiteralValue::String("debug".into()), + LiteralValue::String("error".into()), + LiteralValue::String("warning".into()), ], variants: Some(vec![ SchemaField { - name: "debug".into(), - description: Some("Shows debug messages and above".into()), - type_of: SchemaType::literal(LiteralValue::String("debug".into())), + name: "debug".into(), + description: Some("Shows debug messages and above".into()), + type_of: SchemaType::literal(LiteralValue::String("debug".into())), ..SchemaField::default() }, SchemaField { - name: "error".into(), - description: Some("Shows only error messages".into()), - type_of: SchemaType::literal(LiteralValue::String("error".into())), + name: "error".into(), + description: Some("Shows only error messages".into()), + type_of: SchemaType::literal(LiteralValue::String("error".into())), ..SchemaField::default() }, SchemaField { - name: "warning".into(), - description: Some("Shows warning and error messages".into()), - type_of: SchemaType::literal(LiteralValue::String("warning".into())), + name: "warning".into(), + description: Some("Shows warning and error messages".into()), + type_of: SchemaType::literal(LiteralValue::String("warning".into())), ..SchemaField::default() }, ]), @@ -1418,7 +1418,7 @@
Formats
fn main() { FloatType { // ... - format: Some("currency".into()), + format: Some("currency".into()), } }@@ -1510,7 +1510,7 @@Formats
fn main() { IntegerType { // ... - format: Some("age".into()), + format: Some("age".into()), } }@@ -1557,7 +1557,7 @@Multiple of impl Schematic for T { fn generate_schema() -> SchemaType { - SchemaType::literal(LiteralValue::String("enabled".into())) + SchemaType::literal(LiteralValue::String("enabled".into())) // Or, etc SchemaType::literal(LiteralValue::Uint(100)) } @@ -1646,7 +1646,7 @@
Required keys fn main() { ObjectType { // ... - required: Some(vec!["foo".into(), "bar".into()]), + required: Some(vec!["foo".into(), "bar".into()]), } }
@@ -1675,7 +1675,7 @@Default value value to the
StringType
constructor.#![allow(unused)] fn main() { -SchemaType::String(StringType::new("abc")); +SchemaType::String(StringType::new("abc")); }
Settings
The following fields can be passed to
StringType
, which are then fed into the @@ -1687,7 +1687,7 @@Enumerable
fn main() { StringType { // ... - enum_values: Some(vec!["a".into(), "b".into(), "c".into()]), + enum_values: Some(vec!["a".into(), "b".into(), "c".into()]), } }Formats
@@ -1697,7 +1697,7 @@Formats
fn main() { StringType { // ... - format: Some("url".into()), + format: Some("url".into()), } }@@ -1720,8 +1720,8 @@Patterns
fn main() { StringType { // ... - format: Some("version".into()), - pattern: Some("\d+\.\d+\.\d+".into()), + format: Some("version".into()), + pattern: Some("\d+\.\d+\.\d+".into()), } }@@ -1743,20 +1743,20 @@Patterns
SchemaType::Struct(StructType { fields: vec![ SchemaField { - name: "name".into(), - description: Some("Name of the user".into()), + name: "name".into(), + description: Some("Name of the user".into()), type_of: SchemaType::string(), ..SchemaField::default() }, SchemaField { - name: "age".into(), - description: Some("Age of the user".into()), + name: "age".into(), + description: Some("Age of the user".into()), type_of: SchemaType::integer(IntegerKind::U16), ..SchemaField::default() }, SchemaField { - name: "active".into(), - description: Some("Is the user active".into()), + name: "active".into(), + description: Some("Is the user active".into()), type_of: SchemaType::boolean(), ..SchemaField::default() }, @@ -1784,7 +1784,7 @@Required fiel fn main() { StructType { // ... - required: Some(vec!["name".into()]), + required: Some(vec!["name".into()]), } }
@@ -2012,8 +2012,8 @@Generatin generator.add::<FirstConfig>(); generator.add::<SecondConfig>(); generator.add::<ThirdConfig>(); -generator.generate(PathBuf::from("output/file"), CustomRenderer::default())?; -generator.generate(PathBuf::from("output/another/file"), AnotherRenderer::default())?; +generator.generate(PathBuf::from("output/file"), CustomRenderer::default())?; +generator.generate(PathBuf::from("output/another/file"), AnotherRenderer::default())?; }
Renderers
The following built-in renderers are available, but custom renderers can be created as well by @@ -2040,7 +2040,7 @@
Renderers
let mut generator = SchemaGenerator::default(); generator.add::<CustomType>(); -generator.generate(output_dir.join("config.json"), renderer)?; +generator.generate(output_dir.join("config.json"), renderer)?; }Support formats
JSON
@@ -2101,7 +2101,7 @@Root document // This is the root document generator.add::<LastType>(); -generator.generate(output_dir.join("config.json"), renderer)?; +generator.generate(output_dir.join("config.json"), renderer)?; }
Caveats
By default arrays and objects do not support default values, and will @@ -2118,11 +2118,11 @@
Example output< #[derive(Config)] struct ServerConfig { /// The base URL to serve from. - #[setting(default = "/")] + #[setting(default = "/")] pub base_url: String, /// The default port to listen on. - #[setting(default = 8080, env = "PORT")] + #[setting(default = 8080, env = "PORT")] pub port: usize, } } @@ -2136,17 +2136,17 @@
Example output<
{ // The base URL to serve from. - "base_url": "/", + "base_url": "/", // The default port to listen on. // @envvar PORT - "port": 8080 + "port": 8080 }
# The base URL to serve from. -base_url = "/" +base_url = "/" # The default port to listen on. # @envvar PORT @@ -2163,7 +2163,7 @@
Example output<
# The base URL to serve from. -base_url: "/" +base_url: "/" # The default port to listen on. # @envvar PORT @@ -2198,7 +2198,7 @@
Indentation
fn main() { TemplateOptions { // ... - indent_char: "\t".into(), + indent_char: "\t".into(), } }The spacing between fields can also be toggled with the
newline_between_fields
option. By default @@ -2227,8 +2227,8 @@Header an fn main() { TemplateOptions { // ... - header: "$schema: \"https://example.com/schema.json\"\n\n".into(), - footer: "\n\n# Learn more: https://example.com".into(), + header: "$schema: \"https://example.com/schema.json\"\n\n".into(), + footer: "\n\n# Learn more: https://example.com".into(), } }
Field display
@@ -2239,7 +2239,7 @@Field display fn main() { TemplateOptions { // ... - hide_fields: vec!["key".into(), "nested.key".into()], + hide_fields: vec!["key".into(), "nested.key".into()], } }
Additionally, if you’d like to render a field but have it commented out by default, use the @@ -2248,7 +2248,7 @@
Field display fn main() { TemplateOptions { // ... - comment_fields: vec!["key".into(), "nested.key".into()], + comment_fields: vec!["key".into(), "nested.key".into()], } }
@@ -2262,7 +2262,7 @@Field expansi fn main() { TemplateOptions { // ... - expand_fields: vec!["key".into(), "nested.key".into()], + expand_fields: vec!["key".into(), "nested.key".into()], } }
Here’s an example of how this works:
@@ -2274,17 +2274,17 @@Field expansi
{ - "proxies": [] + "proxies": [] }
{ - "proxies": [ + "proxies": [ // An example proxy configuration. { - "host": "", - "port": 8080 + "host": "", + "port": 8080 } ] } @@ -2309,7 +2309,7 @@
Field expansi let mut generator = SchemaGenerator::default(); generator.add::<CustomType>(); -generator.generate(output_dir.join("schema.json"), JsonSchemaRenderer::default())?; +generator.generate(output_dir.join("schema.json"), JsonSchemaRenderer::default())?; }
For a reference implementation, check out @@ -2328,17 +2328,17 @@
Root document // This is the root document generator.add::<LastType>(); -generator.generate(output_dir.join("schema.json"), JsonSchemaRenderer::default())?; +generator.generate(output_dir.join("schema.json"), JsonSchemaRenderer::default())?; }
When rendered, will look something like the following:
{ - "$schema": "http://json-schema.org/draft-07/schema#", - "title": "LastType", - "type": "object", - "properties": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "LastType", + "type": "object", + "properties": { // Fields in LastType... }, - "definitions": { + "definitions": { // Other types... } } @@ -2375,7 +2375,7 @@
Options
let mut generator = SchemaGenerator::default(); generator.add::<CustomType>(); -generator.generate(output_dir.join("types.ts"), TypeScriptRenderer::default())?; +generator.generate(output_dir.join("types.ts"), TypeScriptRenderer::default())?; }For a reference implementation, check out @@ -2400,7 +2400,7 @@
Indentation fn main() { TypeScriptOptions { // ... - indent_char: " ".into(), + indent_char: " ".into(), } }
Enum types
@@ -2417,7 +2417,7 @@Enum types
} }// Default -export type LogLevel = "debug" | "info" | "error"; +export type LogLevel = "debug" | "info" | "error"; // As enum export enum LogLevel { @@ -2467,7 +2467,7 @@
Type referenc
In the context of this renderer, a type reference is simply a reference to another type by its name, and is used by other types of another name. For example, the fields of a struct type may reference another type by name.
--export type UserStatus = "active" | "inactive"; +
export type UserStatus = "active" | "inactive"; export interface User { status: UserStatus; @@ -2483,10 +2483,10 @@
Type referenc disable_references: true, } }
export type UserStatus = "active" | "inactive"; +
export type UserStatus = "active" | "inactive"; export interface User { - status: "active" | "inactive"; + status: "active" | "inactive"; }
Additionally, the
exclude_references
option can be used to exclude a type reference by name @@ -2495,11 +2495,11 @@Type referenc fn main() { TypeScriptOptions { // ... - exclude_references: vec!["UserStatus".into()], + exclude_references: vec!["UserStatus".into()], } }
export interface User { - status: "active" | "inactive"; + status: "active" | "inactive"; }
Importing external types
@@ -2515,11 +2515,11 @@} -
import type { UserStatus } from "./states"; +
import type { UserStatus } from "./states"; export interface User { status: UserStatus; diff --git a/schema/enum.html b/schema/enum.html index 1cdcb5cb..21c3d805 100644 --- a/schema/enum.html +++ b/schema/enum.html @@ -188,9 +188,9 @@
Enums
fn generate_schema() -> SchemaType { SchemaType::Enum(EnumType { values: vec![ - LiteralValue::String("debug".into()), - LiteralValue::String("error".into()), - LiteralValue::String("warning".into()), + LiteralValue::String("debug".into()), + LiteralValue::String("error".into()), + LiteralValue::String("warning".into()), ], ..EnumType::default() }) @@ -203,9 +203,9 @@Enums
#![allow(unused)] fn main() { SchemaType::enumerable([ - LiteralValue::String("debug".into()), - LiteralValue::String("error".into()), - LiteralValue::String("warning".into()), + LiteralValue::String("debug".into()), + LiteralValue::String("error".into()), + LiteralValue::String("warning".into()), ]); }
Detailed variants
@@ -218,27 +218,27 @@Detailed SchemaType::Enum(EnumType { values: vec![ - LiteralValue::String("debug".into()), - LiteralValue::String("error".into()), - LiteralValue::String("warning".into()), + LiteralValue::String("debug".into()), + LiteralValue::String("error".into()), + LiteralValue::String("warning".into()), ], variants: Some(vec![ SchemaField { - name: "debug".into(), - description: Some("Shows debug messages and above".into()), - type_of: SchemaType::literal(LiteralValue::String("debug".into())), + name: "debug".into(), + description: Some("Shows debug messages and above".into()), + type_of: SchemaType::literal(LiteralValue::String("debug".into())), ..SchemaField::default() }, SchemaField { - name: "error".into(), - description: Some("Shows only error messages".into()), - type_of: SchemaType::literal(LiteralValue::String("error".into())), + name: "error".into(), + description: Some("Shows only error messages".into()), + type_of: SchemaType::literal(LiteralValue::String("error".into())), ..SchemaField::default() }, SchemaField { - name: "warning".into(), - description: Some("Shows warning and error messages".into()), - type_of: SchemaType::literal(LiteralValue::String("warning".into())), + name: "warning".into(), + description: Some("Shows warning and error messages".into()), + type_of: SchemaType::literal(LiteralValue::String("warning".into())), ..SchemaField::default() }, ]), diff --git a/schema/float.html b/schema/float.html index bb8612a6..4458c369 100644 --- a/schema/float.html +++ b/schema/float.html @@ -232,7 +232,7 @@
Formats
fn main() { FloatType { // ... - format: Some("currency".into()), + format: Some("currency".into()), } }diff --git a/schema/generator/index.html b/schema/generator/index.html index 229563fb..1d8d8abc 100644 --- a/schema/generator/index.html +++ b/schema/generator/index.html @@ -228,8 +228,8 @@Generatin generator.add::<FirstConfig>(); generator.add::<SecondConfig>(); generator.add::<ThirdConfig>(); -generator.generate(PathBuf::from("output/file"), CustomRenderer::default())?; -generator.generate(PathBuf::from("output/another/file"), AnotherRenderer::default())?; +generator.generate(PathBuf::from("output/file"), CustomRenderer::default())?; +generator.generate(PathBuf::from("output/another/file"), AnotherRenderer::default())?; }
Renderers
The following built-in renderers are available, but custom renderers can be created as well by diff --git a/schema/generator/json-schema.html b/schema/generator/json-schema.html index 12c6638c..bec8b7ab 100644 --- a/schema/generator/json-schema.html +++ b/schema/generator/json-schema.html @@ -193,7 +193,7 @@
JSON schemas}
For a reference implementation, check out @@ -212,17 +212,17 @@
Root document // This is the root document generator.add::<LastType>(); -generator.generate(output_dir.join("schema.json"), JsonSchemaRenderer::default())?; +generator.generate(output_dir.join("schema.json"), JsonSchemaRenderer::default())?; }
When rendered, will look something like the following:
{ - "$schema": "http://json-schema.org/draft-07/schema#", - "title": "LastType", - "type": "object", - "properties": { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "LastType", + "type": "object", + "properties": { // Fields in LastType... }, - "definitions": { + "definitions": { // Other types... } } diff --git a/schema/generator/template.html b/schema/generator/template.html index 80e49df7..dc7a5e1b 100644 --- a/schema/generator/template.html +++ b/schema/generator/template.html @@ -191,7 +191,7 @@
}
Support formats
JSON
@@ -252,7 +252,7 @@Root document // This is the root document generator.add::<LastType>(); -generator.generate(output_dir.join("config.json"), renderer)?; +generator.generate(output_dir.join("config.json"), renderer)?; }
Caveats
By default arrays and objects do not support default values, and will @@ -269,11 +269,11 @@
Example output< #[derive(Config)] struct ServerConfig { /// The base URL to serve from. - #[setting(default = "/")] + #[setting(default = "/")] pub base_url: String, /// The default port to listen on. - #[setting(default = 8080, env = "PORT")] + #[setting(default = 8080, env = "PORT")] pub port: usize, } } @@ -287,17 +287,17 @@
Example output<
{ // The base URL to serve from. - "base_url": "/", + "base_url": "/", // The default port to listen on. // @envvar PORT - "port": 8080 + "port": 8080 }
# The base URL to serve from. -base_url = "/" +base_url = "/" # The default port to listen on. # @envvar PORT @@ -314,7 +314,7 @@
Example output<
# The base URL to serve from. -base_url: "/" +base_url: "/" # The default port to listen on. # @envvar PORT @@ -349,7 +349,7 @@
Indentation
fn main() { TemplateOptions { // ... - indent_char: "\t".into(), + indent_char: "\t".into(), } }The spacing between fields can also be toggled with the
newline_between_fields
option. By default @@ -378,8 +378,8 @@Header an fn main() { TemplateOptions { // ... - header: "$schema: \"https://example.com/schema.json\"\n\n".into(), - footer: "\n\n# Learn more: https://example.com".into(), + header: "$schema: \"https://example.com/schema.json\"\n\n".into(), + footer: "\n\n# Learn more: https://example.com".into(), } }
Field display
@@ -390,7 +390,7 @@Field display fn main() { TemplateOptions { // ... - hide_fields: vec!["key".into(), "nested.key".into()], + hide_fields: vec!["key".into(), "nested.key".into()], } }
Additionally, if you’d like to render a field but have it commented out by default, use the @@ -399,7 +399,7 @@
Field display fn main() { TemplateOptions { // ... - comment_fields: vec!["key".into(), "nested.key".into()], + comment_fields: vec!["key".into(), "nested.key".into()], } }
@@ -413,7 +413,7 @@Field expansi fn main() { TemplateOptions { // ... - expand_fields: vec!["key".into(), "nested.key".into()], + expand_fields: vec!["key".into(), "nested.key".into()], } }
Here’s an example of how this works:
@@ -425,17 +425,17 @@Field expansi
{ - "proxies": [] + "proxies": [] }
{ - "proxies": [ + "proxies": [ // An example proxy configuration. { - "host": "", - "port": 8080 + "host": "", + "port": 8080 } ] } diff --git a/schema/generator/typescript.html b/schema/generator/typescript.html index 79d5d242..389f18d8 100644 --- a/schema/generator/typescript.html +++ b/schema/generator/typescript.html @@ -191,7 +191,7 @@
TypeScript let mut generator = SchemaGenerator::default(); generator.add::<CustomType>(); -generator.generate(output_dir.join("types.ts"), TypeScriptRenderer::default())?; +generator.generate(output_dir.join("types.ts"), TypeScriptRenderer::default())?; }
For a reference implementation, check out @@ -216,7 +216,7 @@
Indentation
fn main() { TypeScriptOptions { // ... - indent_char: " ".into(), + indent_char: " ".into(), } }Enum types
@@ -233,7 +233,7 @@Enum types
} }// Default -export type LogLevel = "debug" | "info" | "error"; +export type LogLevel = "debug" | "info" | "error"; // As enum export enum LogLevel { @@ -283,7 +283,7 @@
Type referenc
In the context of this renderer, a type reference is simply a reference to another type by its name, and is used by other types of another name. For example, the fields of a struct type may reference another type by name.
--export type UserStatus = "active" | "inactive"; +
export type UserStatus = "active" | "inactive"; export interface User { status: UserStatus; @@ -299,10 +299,10 @@
Type referenc disable_references: true, } }
export type UserStatus = "active" | "inactive"; +
export type UserStatus = "active" | "inactive"; export interface User { - status: "active" | "inactive"; + status: "active" | "inactive"; }
Additionally, the
exclude_references
option can be used to exclude a type reference by name @@ -311,11 +311,11 @@Type referenc fn main() { TypeScriptOptions { // ... - exclude_references: vec!["UserStatus".into()], + exclude_references: vec!["UserStatus".into()], } }
export interface User { - status: "active" | "inactive"; + status: "active" | "inactive"; }
Importing external types
@@ -331,11 +331,11 @@} -
import type { UserStatus } from "./states"; +
import type { UserStatus } from "./states"; export interface User { status: UserStatus; diff --git a/schema/index.html b/schema/index.html index 016f0838..28fa3aa2 100644 --- a/schema/index.html +++ b/schema/index.html @@ -235,12 +235,12 @@
C impl Schematic for User { fn generate_schema() -> SchemaType { SchemaType::structure([ - SchemaField::new("name", SchemaType::String(StringType { + SchemaField::new("name", SchemaType::String(StringType { min_length: Some(1), ..StringType::default() })), - SchemaField::new("age", SchemaType::integer(IntegerKind::Usize)), - SchemaField::new("status", SchemaType::infer::<UserStatus>()), + SchemaField::new("age", SchemaType::integer(IntegerKind::Usize)), + SchemaField::new("status", SchemaType::infer::<UserStatus>()), ]) } } diff --git a/schema/integer.html b/schema/integer.html index 3ac6fb00..ca937f2f 100644 --- a/schema/integer.html +++ b/schema/integer.html @@ -232,7 +232,7 @@
Formats
fn main() { IntegerType { // ... - format: Some("age".into()), + format: Some("age".into()), } }diff --git a/schema/literal.html b/schema/literal.html index e5dfa5be..e6c8386e 100644 --- a/schema/literal.html +++ b/schema/literal.html @@ -187,7 +187,7 @@Literals
impl Schematic for T { fn generate_schema() -> SchemaType { - SchemaType::literal(LiteralValue::String("enabled".into())) + SchemaType::literal(LiteralValue::String("enabled".into())) // Or, etc SchemaType::literal(LiteralValue::Uint(100)) } diff --git a/schema/object.html b/schema/object.html index f3361b3c..84d5d160 100644 --- a/schema/object.html +++ b/schema/object.html @@ -226,7 +226,7 @@Required keys fn main() { ObjectType { // ... - required: Some(vec!["foo".into(), "bar".into()]), + required: Some(vec!["foo".into(), "bar".into()]), } }
diff --git a/schema/string.html b/schema/string.html index 20438fc4..96a42d95 100644 --- a/schema/string.html +++ b/schema/string.html @@ -199,7 +199,7 @@Default value value to the
StringType
constructor.#![allow(unused)] fn main() { -SchemaType::String(StringType::new("abc")); +SchemaType::String(StringType::new("abc")); }
Settings
The following fields can be passed to
StringType
, which are then fed into the @@ -211,7 +211,7 @@Enumerable
fn main() { StringType { // ... - enum_values: Some(vec!["a".into(), "b".into(), "c".into()]), + enum_values: Some(vec!["a".into(), "b".into(), "c".into()]), } }Formats
@@ -221,7 +221,7 @@Formats
fn main() { StringType { // ... - format: Some("url".into()), + format: Some("url".into()), } }@@ -244,8 +244,8 @@Patterns
fn main() { StringType { // ... - format: Some("version".into()), - pattern: Some("\d+\.\d+\.\d+".into()), + format: Some("version".into()), + pattern: Some("\d+\.\d+\.\d+".into()), } }diff --git a/schema/struct.html b/schema/struct.html index f5024f00..d9c15ab3 100644 --- a/schema/struct.html +++ b/schema/struct.html @@ -192,20 +192,20 @@Structs
SchemaType::Struct(StructType { fields: vec![ SchemaField { - name: "name".into(), - description: Some("Name of the user".into()), + name: "name".into(), + description: Some("Name of the user".into()), type_of: SchemaType::string(), ..SchemaField::default() }, SchemaField { - name: "age".into(), - description: Some("Age of the user".into()), + name: "age".into(), + description: Some("Age of the user".into()), type_of: SchemaType::integer(IntegerKind::U16), ..SchemaField::default() }, SchemaField { - name: "active".into(), - description: Some("Is the user active".into()), + name: "active".into(), + description: Some("Is the user active".into()), type_of: SchemaType::boolean(), ..SchemaField::default() }, @@ -233,7 +233,7 @@Required fiel fn main() { StructType { // ... - required: Some(vec!["name".into()]), + required: Some(vec!["name".into()]), } }
diff --git a/schema/types.html b/schema/types.html index 729d27a8..27082951 100644 --- a/schema/types.html +++ b/schema/types.html @@ -204,7 +204,7 @@Names
diff --git a/searcher.js b/searcher.js index d2b0aeed..dc03e0a0 100644 --- a/searcher.js +++ b/searcher.js @@ -316,7 +316,7 @@ window.search = window.search || {}; // Eventhandler for keyevents on `document` function globalKeyHandler(e) { - if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.target.type === 'textarea' || e.target.type === 'text') { return; } + if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey || e.target.type === 'textarea' || e.target.type === 'text' || !hasFocus() && /^(?:input|select|textarea)$/i.test(e.target.nodeName)) { return; } if (e.keyCode === ESCAPE_KEYCODE) { e.preventDefault();#![allow(unused)] fn main() { let mut schema = SchemaType::string(); -schema.set_name("CustomName"); +schema.set_name("CustomName"); }