serde_yml-0.0.12/Cargo.lock 0000644 00000007133 00000000001 0011056 0 ustar # This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anyhow"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "indexmap"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93ead53efc7ea8ed3cfb0c79fc8023fbb782a5432b52830b6518941cebe6505c"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "indoc"
version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5"
[[package]]
name = "itoa"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b"
[[package]]
name = "libyml"
version = "0.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3302702afa434ffa30847a83305f0a69d6abd74293b6554c18ec85c7ef30c980"
dependencies = [
"anyhow",
"version_check",
]
[[package]]
name = "memchr"
version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "proc-macro2"
version = "1.0.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "serde"
version = "1.0.209"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.209"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_yml"
version = "0.0.12"
dependencies = [
"indexmap",
"indoc",
"itoa",
"libyml",
"memchr",
"ryu",
"serde",
"serde_derive",
"version_check",
]
[[package]]
name = "syn"
version = "2.0.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
serde_yml-0.0.12/Cargo.toml 0000644 00000011720 00000000001 0011076 0 ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
rust-version = "1.56.0"
name = "serde_yml"
version = "0.0.12"
authors = ["Serde YML Contributors"]
build = "build.rs"
exclude = [
"/.git/*",
"/.github/*",
"/.gitignore",
"/.vscode/*",
]
include = [
"/CONTRIBUTING.md",
"/LICENSE-APACHE",
"/LICENSE-MIT",
"/benches/**",
"/build.rs",
"/Cargo.toml",
"/examples/**",
"/README.md",
"/src/**",
"/tests/**",
]
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = """
A robust Rust library that simplifies the serialization and deserialization of Rust data structures to and from YAML format using the widely-used Serde framework.
"""
homepage = "https://serdeyml.com"
documentation = "https://docs.rs/serde_yml/"
readme = "README.md"
keywords = [
"yaml",
"serde",
"serialization",
]
categories = [
"encoding",
"parser-implementations",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/sebastienrousseau/serde_yml"
[package.metadata.clippy]
warn-lints = [
"clippy::all",
"clippy::pedantic",
"clippy::cargo",
"clippy::nursery",
]
[package.metadata.docs.rs]
all-features = true
rustdoc-args = [
"--generate-link-to-definition",
"--cfg",
"docsrs",
"--document-private-items",
"--display-warnings",
]
targets = ["x86_64-unknown-linux-gnu"]
[profile.dev]
opt-level = 0
lto = false
codegen-units = 256
debug = 2
debug-assertions = true
rpath = false
panic = "unwind"
overflow-checks = true
incremental = true
strip = false
[profile.release]
opt-level = "s"
lto = true
codegen-units = 1
debug = 0
debug-assertions = false
rpath = false
panic = "abort"
overflow-checks = false
incremental = false
strip = "symbols"
[profile.test]
opt-level = 0
lto = false
codegen-units = 256
debug = 2
debug-assertions = true
rpath = false
overflow-checks = true
incremental = true
strip = false
[lib]
name = "serde_yml"
crate-type = ["lib"]
path = "src/lib.rs"
doc-scrape-examples = false
required-features = []
[[example]]
name = "example"
path = "examples/example.rs"
[[test]]
name = "mod"
path = "tests/mod.rs"
[[test]]
name = "test_anchors_and_aliases"
path = "tests/test_anchors_and_aliases.rs"
[[test]]
name = "test_de"
path = "tests/test_de.rs"
[[test]]
name = "test_error"
path = "tests/test_error.rs"
[[test]]
name = "test_lib"
path = "tests/test_lib.rs"
[[test]]
name = "test_loader"
path = "tests/test_loader.rs"
[[test]]
name = "test_mapping"
path = "tests/test_mapping.rs"
[[test]]
name = "test_number"
path = "tests/test_number.rs"
[[test]]
name = "test_ser"
path = "tests/test_ser.rs"
[[test]]
name = "test_serde"
path = "tests/test_serde.rs"
[[test]]
name = "test_tagged"
path = "tests/test_tagged.rs"
[[test]]
name = "test_value"
path = "tests/test_value.rs"
[[test]]
name = "test_with"
path = "tests/test_with.rs"
[dependencies.indexmap]
version = "2.2.4"
[dependencies.itoa]
version = "1.0"
[dependencies.libyml]
version = "0.0.5"
[dependencies.memchr]
version = "2"
default-features = false
[dependencies.ryu]
version = "1.0"
[dependencies.serde]
version = "1.0.204"
[dev-dependencies.indoc]
version = "2.0.5"
[dev-dependencies.serde]
version = "1.0.204"
features = ["derive"]
[dev-dependencies.serde_derive]
version = "1.0.204"
[build-dependencies.version_check]
version = "0.9.4"
[features]
default = []
[lints.rust]
bare_trait_objects = "allow"
dead_code = "deny"
deprecated_in_future = "deny"
elided_lifetimes_in_paths = "allow"
ellipsis_inclusive_range_patterns = "deny"
explicit_outlives_requirements = "deny"
macro_use_extern_crate = "deny"
meta_variable_misuse = "deny"
missing_copy_implementations = "warn"
missing_debug_implementations = "forbid"
missing_docs = "warn"
missing_fragment_specifier = "deny"
non_ascii_idents = "forbid"
non_camel_case_types = "allow"
non_upper_case_globals = "allow"
noop_method_call = "deny"
single_use_lifetimes = "deny"
trivial_bounds = "allow"
trivial_casts = "deny"
trivial_numeric_casts = "deny"
unreachable_pub = "forbid"
unsafe_code = "allow"
unstable_features = "warn"
unused_extern_crates = "warn"
unused_features = "deny"
unused_import_braces = "deny"
unused_labels = "deny"
unused_lifetimes = "deny"
unused_macro_rules = "deny"
unused_qualifications = "deny"
variant_size_differences = "deny"
[lints.rust.future_incompatible]
level = "deny"
priority = -1
[lints.rust.keyword_idents]
level = "deny"
priority = -1
[lints.rust.rust_2018_idioms]
level = "deny"
priority = -1
[lints.rust.rust_2021_compatibility]
level = "deny"
priority = -1
[lints.rust.unused]
level = "deny"
priority = -1
serde_yml-0.0.12/Cargo.toml.orig 0000644 0000000 0000000 00000007706 10461020230 0014570 0 ustar 0000000 0000000 [package]
authors = ["Serde YML Contributors"]
categories = ["encoding", "parser-implementations"]
description = """
A robust Rust library that simplifies the serialization and deserialization of Rust data structures to and from YAML format using the widely-used Serde framework.
"""
documentation = "https://docs.rs/serde_yml/"
edition = "2021"
exclude = ["/.git/*", "/.github/*", "/.gitignore", "/.vscode/*"]
homepage = "https://serdeyml.com"
keywords = ["yaml", "serde", "serialization"]
license = "MIT OR Apache-2.0"
name = "serde_yml"
readme = "README.md"
repository = "https://github.com/sebastienrousseau/serde_yml"
rust-version = "1.56.0"
version = "0.0.12"
include = [
"/CONTRIBUTING.md",
"/LICENSE-APACHE",
"/LICENSE-MIT",
"/benches/**",
"/build.rs",
"/Cargo.toml",
"/examples/**",
"/README.md",
"/src/**",
"/tests/**",
]
# [[bench]]
# name = "benchmark"
# harness = false
# path = "benches/criterion.rs"
# [profile.bench]
# debug = true
[dependencies]
indexmap = "2.2.4"
itoa = "1.0"
libyml = "0.0.5"
memchr = { version = "2", default-features = false }
ryu = "1.0"
serde = { version = "1.0.204" }
[build-dependencies]
# Dependencies for build scripts.
version_check = "0.9.4" # Check the Rust version used to compile the package.
[dev-dependencies]
indoc = "2.0.5"
serde = { version = "1.0.204", features = ["derive"] }
serde_derive = "1.0.204"
[features]
default = []
[lib]
crate-type = ["lib"]
doc-scrape-examples = false
name = "serde_yml"
path = "src/lib.rs"
required-features = []
[package.metadata.docs.rs]
# Specify arguments for rustdoc to enhance documentation quality.
rustdoc-args = [
"--generate-link-to-definition",
"--cfg", "docsrs",
"--document-private-items",
"--display-warnings"
]
# Build docs with all crate features enabled to cover the entire API.
all-features = true
# Target platform for the docs, ensuring compatibility with common Linux servers.
targets = ["x86_64-unknown-linux-gnu"]
# Linting config
[lints.rust]
## Warn
# box_pointers = "warn"
missing_copy_implementations = "warn"
missing_docs = "warn"
unstable_features = "warn"
# unused_crate_dependencies = "warn"
unused_extern_crates = "warn"
# unused_results = "warn"
## Allow
bare_trait_objects = "allow"
elided_lifetimes_in_paths = "allow"
non_camel_case_types = "allow"
non_upper_case_globals = "allow"
trivial_bounds = "allow"
unsafe_code = "allow"
## Forbid
# missing_docs = "warn"
missing_debug_implementations = "forbid"
non_ascii_idents = "forbid"
unreachable_pub = "forbid"
## Deny
dead_code = "deny"
deprecated_in_future = "deny"
ellipsis_inclusive_range_patterns = "deny"
explicit_outlives_requirements = "deny"
future_incompatible = { level = "deny", priority = -1 }
keyword_idents = { level = "deny", priority = -1 }
macro_use_extern_crate = "deny"
meta_variable_misuse = "deny"
missing_fragment_specifier = "deny"
noop_method_call = "deny"
rust_2018_idioms = { level = "deny", priority = -1 }
rust_2021_compatibility = { level = "deny", priority = -1 }
single_use_lifetimes = "deny"
trivial_casts = "deny"
trivial_numeric_casts = "deny"
unused = { level = "deny", priority = -1 }
unused_features = "deny"
unused_import_braces = "deny"
unused_labels = "deny"
unused_lifetimes = "deny"
unused_macro_rules = "deny"
unused_qualifications = "deny"
variant_size_differences = "deny"
[package.metadata.clippy]
warn-lints = [
"clippy::all",
"clippy::pedantic",
"clippy::cargo",
"clippy::nursery",
]
[profile.dev]
codegen-units = 256
debug = true
debug-assertions = true
incremental = true
lto = false
opt-level = 0
overflow-checks = true
panic = 'unwind'
rpath = false
strip = false
[profile.release]
codegen-units = 1
debug = false
debug-assertions = false
incremental = false
lto = true
opt-level = "s"
overflow-checks = false
panic = "abort"
rpath = false
strip = "symbols"
[profile.test]
codegen-units = 256
debug = true
debug-assertions = true
incremental = true
lto = false
opt-level = 0
overflow-checks = true
rpath = false
strip = false
serde_yml-0.0.12/LICENSE-APACHE 0000644 0000000 0000000 00000022773 10461020230 0013626 0 ustar 0000000 0000000 Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
serde_yml-0.0.12/LICENSE-MIT 0000644 0000000 0000000 00000001777 10461020230 0013337 0 ustar 0000000 0000000 Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
serde_yml-0.0.12/README.md 0000644 0000000 0000000 00000044603 10461020230 0013155 0 ustar 0000000 0000000
# Serde YML (a fork of Serde YAML)
[![Made With Love][made-with-rust]][11] [![Crates.io][crates-badge]][07] [![lib.rs][libs-badge]][12] [![Docs.rs][docs-badge]][08] [![Codecov][codecov-badge]][09] [![Build Status][build-badge]][10] [![GitHub][github-badge]][06]
[Serde YML][00] is a Rust library for using the [Serde][01] serialization framework with data in [YAML][05] file format.
## Features
- Serialization and deserialization of Rust data structures to/from YAML format
- Support for custom structs and enums using Serde's derive macros
- Handling of YAML's `!tag` syntax for representing enum variants
- Direct access to YAML values through the `Value` type and related types like `Mapping` and `Sequence`
- Comprehensive error handling with `Error`, `Location`, and `Result` types
- Serialization to YAML using `to_string` and `to_writer` functions
- Deserialization from YAML using `from_str`, `from_slice`, and `from_reader` functions
- Customizable serialization and deserialization behavior using Serde's `#[serde(with = ...)]` attribute
- Support for serializing/deserializing enums using a YAML map with a single key-value pair through the `singleton_map` module
- Recursive application of `singleton_map` serialization/deserialization to all enums within a data structure using the `singleton_map_recursive` module
- Serialization and deserialization of optional enum fields using the `singleton_map_optional` module
- Handling of nested enum structures with optional inner enums using the `singleton_map_recursive` module
- Customization of serialization and deserialization logic for enums using the `singleton_map_with` module and custom helper functions
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
serde = "1.0"
serde_yml = "0.0.12"
```
## Usage
Here's a quick example on how to use Serde YML to serialize and deserialize a struct to and from YAML:
```rust
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
struct Point {
x: f64,
y: f64,
}
fn main() -> Result<(), serde_yml::Error> {
let point = Point { x: 1.0, y: 2.0 };
// Serialize to YAML
let yaml = serde_yml::to_string(&point)?;
assert_eq!(yaml, "x: 1.0\ny: 2.0\n");
// Deserialize from YAML
let deserialized_point: Point = serde_yml::from_str(&yaml)?;
assert_eq!(point, deserialized_point);
Ok(())
}
```
## Documentation
For full API documentation, please visit [https://doc.libyml.com/serde-yaml/][04] or [https://docs.rs/serde-yaml][08].
## Rust Version Compatibility
Compiler support: requires rustc 1.56.0+
## Examples
Serde YML provides a set of comprehensive examples. You can find them in the
`examples` directory of the project. To run the examples, clone the repository
and execute the following command in your terminal from the project:
```shell
cargo run --example example
```
The examples cover various scenarios, including serializing and deserializing
structs, enums, optional fields, custom structs, and more.
Here are a few notable examples:
### Serializing and Deserializing Structs
```rust
use serde::{Serialize, Deserialize};
use serde_yml;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Point {
x: f64,
y: f64,
}
fn main() -> Result<(), serde_yml::Error> {
let point = Point { x: 1.0, y: 2.0 };
// Serialize to YAML
let yaml = serde_yml::to_string(&point)?;
assert_eq!(yaml, "x: 1.0\ny: 2.0\n");
// Deserialize from YAML
let deserialized_point: Point = serde_yml::from_str(&yaml)?;
assert_eq!(point, deserialized_point);
Ok(())
}
```
This example demonstrates how to serialize and deserialize a simple struct
`Point` to and from YAML using the `serde_yml` crate.
### Serializing and Deserializing Enums
```rust
use serde::{Serialize, Deserialize};
use serde_yml;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum Shape {
Rectangle { width: u32, height: u32 },
Circle { radius: f64 },
Triangle { base: u32, height: u32 },
}
fn main() -> Result<(), serde_yml::Error> {
let shapes = vec![
Shape::Rectangle { width: 10, height: 20 },
Shape::Circle { radius: 5.0 },
Shape::Triangle { base: 8, height: 12 },
];
// Serialize to YAML
let yaml = serde_yml::to_string(&shapes)?;
println!("Serialized YAML:\n{}", yaml);
// Deserialize from YAML
let deserialized_shapes: Vec = serde_yml::from_str(&yaml)?;
assert_eq!(shapes, deserialized_shapes);
Ok(())
}
```
This example demonstrates how to serialize and deserialize an enum `Shape`
(with struct variants) to and from YAML using the `serde_yml` crate.
### Serializing and Deserializing Optional Fields
```rust
use serde::{Serialize, Deserialize};
use serde_yml;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct User {
name: String,
age: Option,
#[serde(default)]
is_active: bool,
}
fn main() -> Result<(), serde_yml::Error> {
let user = User {
name: "John".to_string(),
age: Some(30),
is_active: true,
};
// Serialize to YAML
let yaml = serde_yml::to_string(&user)?;
println!("Serialized YAML:\n{}", yaml);
// Deserialize from YAML
let deserialized_user: User = serde_yml::from_str(&yaml)?;
assert_eq!(user, deserialized_user);
Ok(())
}
```
This example demonstrates how to serialize and deserialize a struct `User` with
an optional field `age` to and from YAML using the `serde_yml` crate.
### Serializing and Deserializing a HashMap
```rust
use std::collections::HashMap;
use serde_yml;
fn main() -> Result<(), serde_yml::Error> {
let mut map = HashMap::new();
map.insert("name".to_string(), "John".to_string());
map.insert("age".to_string(), "30".to_string());
let yaml = serde_yml::to_string(&map)?;
println!("Serialized YAML: {}", yaml);
let deserialized_map: HashMap = serde_yml::from_str(&yaml)?;
println!("Deserialized map: {:?}", deserialized_map);
Ok(())
}
```
This example demonstrates how to serialize and deserialize a `HashMap` to and
from YAML using the `serde_yml` crate.
### Serializing and Deserializing Custom Structs
```rust
use serde::{Serialize, Deserialize};
use serde_yml;
#[derive(Serialize, Deserialize, Debug)]
struct Person {
name: String,
age: u32,
city: String,
}
fn main() -> Result<(), serde_yml::Error> {
let person = Person {
name: "Alice".to_string(),
age: 25,
city: "New York".to_string(),
};
let yaml = serde_yml::to_string(&person)?;
println!("Serialized YAML: {}", yaml);
let deserialized_person: Person = serde_yml::from_str(&yaml)?;
println!("Deserialized person: {:?}", deserialized_person);
Ok(())
}
```
This example demonstrates how to serialize and deserialize a custom struct
`Person` to and from YAML using the `serde_yml` crate.
### Using Serde derive
It can also be used with Serde's derive macros to handle structs and enums
defined in your program.
Structs serialize in the obvious way:
```rust
use serde_derive::{Serialize, Deserialize};
use serde_yml;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Point {
x: f64,
y: f64,
}
fn main() -> Result<(), serde_yml::Error> {
let point = Point { x: 1.0, y: 2.0 };
let yaml = serde_yml::to_string(&point)?;
assert_eq!(yaml, "x: 1.0\n'y': 2.0\n");
let deserialized_point: Point = serde_yml::from_str(&yaml)?;
assert_eq!(point, deserialized_point);
Ok(())
}
```
Enums serialize using YAML's `!tag` syntax to identify the variant name.
```rust
use serde_derive::{Serialize, Deserialize};
use serde_yml;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum Enum {
Unit,
Newtype(usize),
Tuple(usize, usize, usize),
Struct { x: f64, y: f64 },
}
fn main() -> Result<(), serde_yml::Error> {
let yaml = "
- !Newtype 1
- !Tuple [0, 0, 0]
- !Struct {x: 1.0, y: 2.0}
";
let values: Vec = serde_yml::from_str(yaml).unwrap();
assert_eq!(values[0], Enum::Newtype(1));
assert_eq!(values[1], Enum::Tuple(0, 0, 0));
assert_eq!(values[2], Enum::Struct { x: 1.0, y: 2.0 });
// The last two in YAML's block style instead:
let yaml = "
- !Tuple
- 0
- 0
- 0
- !Struct
x: 1.0
'y': 2.0
";
let values: Vec = serde_yml::from_str(yaml).unwrap();
assert_eq!(values[0], Enum::Tuple(0, 0, 0));
assert_eq!(values[1], Enum::Struct { x: 1.0, y: 2.0 });
// Variants with no data can be written using !Tag or just the string name.
let yaml = "
- Unit # serialization produces this one
- !Unit
";
let values: Vec = serde_yml::from_str(yaml).unwrap();
assert_eq!(values[0], Enum::Unit);
assert_eq!(values[1], Enum::Unit);
Ok(())
}
```
This example demonstrates how to use Serde's derive macros to automatically
implement the `Serialize` and `Deserialize` traits for a struct `Point`, and
then serialize and deserialize it to and from YAML using the `serde_yml` crate.
### Serializing and Deserializing Enums with Custom Serialization and Deserialization
```rust
use serde::{Deserialize, Serialize};
use serde::de::{self, Deserializer, MapAccess, Visitor};
use serde::ser::{SerializeMap, Serializer};
use std::fmt;
use serde_yml;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(PartialEq, Debug)]
struct MyStruct {
field: MyEnum,
}
// Include custom Serialize and Deserialize implementations for MyStruct here
// ...
fn main() -> Result<(), serde_yml::Error> {
let input = MyStruct {
field: MyEnum::Variant2 { field: 42 },
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: MyStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
Ok(())
}
```
This example demonstrates how to use custom `Serialize` and `Deserialize`
implementations for a struct containing an enum field, and how to leverage
`serde_yml` to serialize and deserialize the struct to and from YAML.
### Serializing and Deserializing Optional Enums
```rust
use serde::{Deserialize, Serialize};
use serde_yml;
use serde_yml::with::singleton_map_optional;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum OptionalEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct OptionalStruct {
#[serde(with = "singleton_map_optional")]
field: Option,
}
fn main() -> Result<(), serde_yml::Error> {
let input = OptionalStruct {
field: Some(OptionalEnum::Variant2 { field: 42 }),
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: OptionalStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
Ok(())
}
```
This example demonstrates how to use the `singleton_map_optional`
attribute to serialize and deserialize an `Option` field as a single
YAML mapping entry with the key being the enum variant name.
### Serializing and Deserializing Nested Enums
```rust
use serde::{Deserialize, Serialize};
use serde_yml;
use serde_yml::with::singleton_map_recursive;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum NestedEnum {
Variant1(String),
Variant2(Option),
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum InnerEnum {
Inner1(i32),
Inner2(i32),
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct NestedStruct {
#[serde(with = "singleton_map_recursive")]
field: NestedEnum,
}
fn main() -> Result<(), serde_yml::Error> {
let input = NestedStruct {
field: NestedEnum::Variant2(Some(InnerEnum::Inner2(42))),
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: NestedStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
Ok(())
}
```
This example demonstrates how to use the `singleton_map_recursive` attribute to
serialize and deserialize a nested enum structure where one of the enum
variants contains an optional inner enum.
### Serializing and Deserializing Enums with `singleton_map_recursive`
```rust
use serde::{Deserialize, Serialize};
use serde_yml;
use serde_yml::with::singleton_map_recursive;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyStruct {
#[serde(with = "singleton_map_recursive")]
field: MyEnum,
}
fn main() -> Result<(), serde_yml::Error> {
let input = MyStruct {
field: MyEnum::Variant2 { field: 42 },
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: MyStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
Ok(())
}
```
This example demonstrates how to use the `singleton_map_recursive` attribute to
serialize and deserialize an enum field as a single YAML mapping entry with the
key being the enum variant name.
### Serializing and Deserializing Enums with `singleton_map_with` and Custom Serialization
```rust
use serde::{Deserialize, Serialize};
use serde_yml;
use serde_yml::with::singleton_map_with;
fn custom_serialize(
value: &T,
serializer: S,
) -> Result
where
T: Serialize,
S: serde::Serializer,
{
// Custom serialization logic
singleton_map_with::serialize(value, serializer)
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyStruct {
#[serde(
serialize_with = "custom_serialize",
deserialize_with = "singleton_map_with::deserialize"
)]
field: MyEnum,
}
fn main() -> Result<(), serde_yml::Error> {
let input = MyStruct {
field: MyEnum::Variant2 { field: 42 },
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: MyStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
Ok(())
}
```
This example demonstrates how to use the `singleton_map_with` attribute in
combination with a custom serialization function (`custom_serialize`) to
serialize and deserialize an enum field (`MyEnum`) within a struct
(`MyStruct`).
The `custom_serialize` function is used for serialization, while the
`singleton_map_with::deserialize` function is used for deserialization. This
allows for additional customization of the serialization process while still
leveraging the singleton_map_with attribute for deserialization.
### Serializing and Deserializing Enums with `singleton_map_with`
```rust
use serde::{Deserialize, Serialize};
use serde_yml;
use serde_yml::with::singleton_map_with;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyStruct {
#[serde(with = "singleton_map_with")]
field: MyEnum,
}
fn main() -> Result<(), serde_yml::Error> {
let input = MyStruct {
field: MyEnum::Variant2 { field: 42 },
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: MyStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
Ok(())
}
```
This example demonstrates how to use the `singleton_map_with` attribute to
serialize and deserialize an enum field (`MyEnum`) within a struct
(`MyStruct`). The `singleton_map_with` attribute allows for additional
customization of the serialization and deserialization process through the use
of helper functions.
## Contributing
Contributions are welcome! Please submit a Pull Request on [GitHub][06].
## Credits and Acknowledgements
Serde YML is a continuation of the excellent work done by [David Tolnay][03] and the maintainers of the [serde-yaml][02] library. While Serde YML has evolved into a separate library, we express our sincere gratitude to them for their contributions to the Rust community.
## License
Licensed under either of the [Apache License](LICENSE-APACHE) or the
[MIT license](LICENSE-MIT) at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.
[00]: https://serdeyml.com
[01]: https://github.com/serde-rs/serde
[02]: https://github.com/dtolnay/serde-yaml
[03]: https://github.com/dtolnay
[04]: https://doc.libyml.com/serde-yaml/
[05]: https://yaml.org/
[06]: https://github.com/sebastienrousseau/serde_yml
[07]: https://crates.io/crates/serde_yml
[08]: https://docs.rs/serde_yml
[09]: https://codecov.io/gh/sebastienrousseau/serde_yml
[10]: https://github.com/sebastienrousseau/serde-yml/actions?query=branch%3Amaster
[11]: https://www.rust-lang.org/
[12]: https://lib.rs/crates/serde_yml
[build-badge]: https://img.shields.io/github/actions/workflow/status/sebastienrousseau/serde_yml/release.yml?branch=master&style=for-the-badge&logo=github "Build Status"
[codecov-badge]: https://img.shields.io/codecov/c/github/sebastienrousseau/serde_yml?style=for-the-badge&token=Q9KJ6XXL67&logo=codecov "Codecov"
[crates-badge]: https://img.shields.io/crates/v/serde_yml.svg?style=for-the-badge&color=fc8d62&logo=rust "Crates.io"
[libs-badge]: https://img.shields.io/badge/lib.rs-v0.0.12-orange.svg?style=for-the-badge "View on lib.rs"
[docs-badge]: https://img.shields.io/badge/docs.rs-serde__yml-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs "Docs.rs"
[github-badge]: https://img.shields.io/badge/github-sebastienrousseau/serde--yml-8da0cb?style=for-the-badge&labelColor=555555&logo=github "GitHub"
[made-with-rust]: https://img.shields.io/badge/rust-f04041?style=for-the-badge&labelColor=c0282d&logo=rust 'Made With Rust'
serde_yml-0.0.12/build.rs 0000644 0000000 0000000 00000003263 10461020230 0013340 0 ustar 0000000 0000000 //! This build script checks if the current Rustc version is at least the
//! minimum required version.
//! If the current Rustc version is less than the minimum required version,
//! the build script will exit the build process with a non-zero exit code.
//!
//! The minimum required version is specified in the `min_version` variable.
use std::process;
/// Checks if the current Rustc version is at least the minimum required version
///
/// # Arguments
///
/// * `min_version` - The minimum required Rustc version as a string.
///
/// # Returns
///
/// * `Some(true)` - If the current Rustc version is at least the minimum
/// required version.
/// * `Some(false)` - If the current Rustc version is less than the minimum
/// required version.
/// * `None` - If the current Rustc version cannot be determined.
///
/// # Errors
///
/// This function will exit the build process with a non-zero exit code if the
/// current Rustc version is less than the minimum required version.
///
/// # Examples
///
/// ```rust
/// let min_version = "1.56";
///
/// match version_check::is_min_version(min_version) {
/// Some(true) => println!("Rustc version is at least {}", min_version),
/// Some(false) => {
/// eprintln!("Rustc version is less than {}", min_version);
/// process::exit(1);
/// }
/// None => {
/// eprintln!("Unable to determine Rustc version");
/// process::exit(1);
/// }
/// }
/// ```
fn main() {
let min_version = "1.56";
match version_check::is_min_version(min_version) {
Some(true) => {}
_ => {
eprintln!("'fd' requires Rustc version >= {}", min_version);
process::exit(1);
}
}
}
serde_yml-0.0.12/examples/example.rs 0000644 0000000 0000000 00000002426 10461020230 0015512 0 ustar 0000000 0000000 //! # Serde YML Examples
//!
//! This crate contains examples that demonstrate the usage of the Serde YML library.
//!
//! The examples are organized into the following modules:
//!
//! - `loader` - Contains the example modules for the `loader` module.
//! - `with` - Contains the example modules for the `with` module.
//!
/// Contains the example modules for the `loader` module.
mod loader;
/// Contains the example modules for the `modules` module.
mod modules;
/// Contains the example modules for the `serializer` module.
mod serializer;
/// Contains the example modules for the `value` module.
mod value;
/// Examples for the `with` module.
mod with;
/// Examples for the `tag` module.
mod libyml;
/// The main function that runs all the example modules.
///
/// This function is responsible for running all the example modules.
/// It does this by calling the `main` function of each example module.
///
fn main() {
// Run the example module `loader`.
loader::main();
// Run the example module `modules`.
modules::main();
// Run the example module `serializer`.
serializer::main();
// Run the example module `value`.
value::main();
// Run the example module `with`.
with::main();
// Run the example module `libyml`.
libyml::main();
}
serde_yml-0.0.12/examples/libyml/emitter_examples.rs 0000644 0000000 0000000 00000027411 10461020230 0020717 0 ustar 0000000 0000000 //! Examples for the `Emitter` struct and its methods in the `emitter` module.
//!
//! This file demonstrates the creation, usage, and various functionalities of the `Emitter` for emitting YAML events, including scalars, sequences, mappings, and document/stream events.
use serde_yml::libyml::emitter::{
Emitter, Event, Mapping, Scalar, ScalarStyle, Sequence,
};
use std::io::Cursor;
pub(crate) fn main() {
// Print a message to indicate the file being executed
println!("\n❯ Executing examples/libyml/emitter_examples.rs");
// Example: Emitting a stream start and end event
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted stream start and end: {}", output);
// Example: Emitting a document start and end event with a scalar
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "hello",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted document with scalar: {}", output);
// Example: Emitting a sequence
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::SequenceStart(Sequence { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "item1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "item2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted sequence: {}", output);
// Example: Emitting a mapping
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::MappingStart(Mapping { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "value1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "value2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted mapping: {}", output);
// Example: Flushing the emitter
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "hello",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.flush().unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted and flushed: {}", output);
// Example: Emitting scalar with tag
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: Some("!mytag".to_string()),
value: "hello",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted scalar with tag: {}", output);
// Example: Emitting sequence with tag
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::SequenceStart(Sequence {
tag: Some("!mytag".to_string()),
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "item1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "item2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted sequence with tag: {}", output);
// Example: Emitting mapping with tag
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::MappingStart(Mapping {
tag: Some("!mytag".to_string()),
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "value1",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "value2",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted mapping with tag: {}", output);
// Example: Emitting an empty sequence
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::SequenceStart(Sequence { tag: None }))
.unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted empty sequence: {}", output);
// Example: Emitting an empty mapping
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::MappingStart(Mapping { tag: None }))
.unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted empty mapping: {}", output);
// Example: Emitting a nested sequence
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::SequenceStart(Sequence { tag: None }))
.unwrap();
emitter
.emit(Event::SequenceStart(Sequence { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "nested",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::SequenceEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted nested sequence: {}", output);
// Example: Emitting a nested mapping
let mut buffer = Cursor::new(Vec::new());
{
let mut emitter = Emitter::new(Box::new(&mut buffer));
emitter.emit(Event::StreamStart).unwrap();
emitter.emit(Event::DocumentStart).unwrap();
emitter
.emit(Event::MappingStart(Mapping { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "key",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::MappingStart(Mapping { tag: None }))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "nested_key",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter
.emit(Event::Scalar(Scalar {
tag: None,
value: "nested_value",
style: ScalarStyle::Plain,
}))
.unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::MappingEnd).unwrap();
emitter.emit(Event::DocumentEnd).unwrap();
emitter.emit(Event::StreamEnd).unwrap();
}
let output =
String::from_utf8_lossy(&buffer.into_inner()).to_string();
println!("\n✅ Emitted nested mapping: {}", output);
}
serde_yml-0.0.12/examples/libyml/mod.rs 0000644 0000000 0000000 00000001473 10461020230 0016127 0 ustar 0000000 0000000 /// This module contains the `tag` example.
pub(crate) mod tag_examples;
/// This module contains the `emitter` example.
pub(crate) mod emitter_examples;
/// This module contains the `parser` example.
pub(crate) mod parser_examples;
/// This module contains the `safe_cstr` example.
pub(crate) mod safe_cstr_examples;
/// This module contains the `util` example.
pub(crate) mod util_examples;
/// The main function that runs all the example modules.
pub(crate) fn main() {
// Run the example module `emitter`.
emitter_examples::main();
// Run the example module `parser`.
parser_examples::main();
// Run the example module `safe_cstr`.
safe_cstr_examples::main();
// Run the example module `tag`.
tag_examples::main();
// Run the example module `util`.
util_examples::main();
}
serde_yml-0.0.12/examples/libyml/parser_examples.rs 0000644 0000000 0000000 00000023170 10461020230 0020540 0 ustar 0000000 0000000 //! Examples for the `Parser` struct and its methods in the `parser` module.
//!
//! This file demonstrates the creation, usage, and event parsing of `Parser` instances,
//! as well as handling different types of YAML events and input scenarios.
use serde_yml::libyml::parser::{Event, Parser};
use std::borrow::Cow;
#[allow(clippy::single_match)]
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/libyml/parser_examples.rs");
// Example 1: Creating a parser and parsing a stream start event
{
let input = Cow::Borrowed(b"foo: bar\n");
let mut parser = Parser::new(Cow::Borrowed(input.as_ref()));
match parser.parse_next_event() {
Ok((event, _)) => {
match event {
Event::StreamStart => {
println!("\n✅ Stream start event parsed successfully.")
}
_ => println!("\n❌ Unexpected event."),
}
}
Err(err) => println!("Error parsing event: {:?}", err),
}
}
// Example 2: Parsing a stream end event
{
let input = Cow::Borrowed(b"foo: bar\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
if matches!(event, Event::StreamEnd) {
println!("\n✅ Stream end event parsed successfully.");
break;
}
}
}
// Example 3: Parsing a document start event
{
let input = Cow::Borrowed(b"---\nfoo: bar\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
if matches!(event, Event::DocumentStart) {
println!(
"\n✅ Document start event parsed successfully."
);
break;
}
}
}
// Example 4: Parsing a document end event
{
let input =
Cow::Borrowed(b"foo: bar\n---\nbaz: qux\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
if matches!(event, Event::DocumentEnd) {
println!(
"\n✅ Document end event parsed successfully."
);
break;
}
}
}
// Example 5: Parsing a scalar event
{
let input = Cow::Borrowed(b"bar\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
if let Event::Scalar(scalar) = event {
println!(
"\n✅ Scalar event parsed successfully with value: {:?}",
scalar.value
);
break;
}
}
}
// Example 6: Parsing a sequence start event
{
let input = Cow::Borrowed(b"- item1\n- item2\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
if matches!(event, Event::SequenceStart(_)) {
println!(
"\n✅ Sequence start event parsed successfully."
);
break;
}
}
}
// Example 7: Parsing a sequence end event
{
let input = Cow::Borrowed(b"- item1\n- item2\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
if matches!(event, Event::SequenceEnd) {
println!(
"\n✅ Sequence end event parsed successfully."
);
break;
}
}
}
// Example 8: Parsing a mapping start event
{
let input = Cow::Borrowed(b"key: value\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
if matches!(event, Event::MappingStart(_)) {
println!(
"\n✅ Mapping start event parsed successfully."
);
break;
}
}
}
// Example 9: Parsing a mapping end event
{
let input = Cow::Borrowed(b"key: value\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
if matches!(event, Event::MappingEnd) {
println!("\n✅ Mapping end event parsed successfully.");
break;
}
}
}
// Example 10: Handling unexpected input
{
let input = Cow::Borrowed(b"unexpected: [value").as_ref(); // Malformed YAML
let mut parser = Parser::new(Cow::Borrowed(input));
match parser.parse_next_event() {
Ok(_) => println!(
"\n❌ Unexpectedly parsed malformed input without error."
),
Err(err) => {
println!("\n❌ Error parsing malformed input: {:?}", err)
}
}
}
// Example 11: Handling empty input
{
let input = Cow::Borrowed(b"").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
match parser.parse_next_event() {
Ok((event, _)) => match event {
Event::StreamEnd => println!("Stream end event parsed successfully for empty input."),
_ => println!("Unexpected event for empty input."),
},
Err(err) => println!("Error parsing empty input: {:?}", err),
}
}
// Example 12: Parsing nested sequences
{
let input =
Cow::Borrowed(b"- item1\n- - nested1\n - nested2\n")
.as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
let mut found_nested_start = false;
while let Ok((event, _)) = parser.parse_next_event() {
if matches!(event, Event::SequenceStart(_)) {
if found_nested_start {
println!("\n✅ Nested sequence start event parsed successfully.");
break;
} else {
found_nested_start = true;
}
}
}
if !found_nested_start {
println!("\n❌ Nested sequence start event was not found.");
}
}
// Example 13: Parsing mixed content (sequence and mapping)
{
let input =
Cow::Borrowed(b"- item1\nkey: value\n- item2\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
let mut found_sequence = false;
let mut found_mapping = false;
while let Ok((event, _)) = parser.parse_next_event() {
if matches!(event, Event::SequenceStart(_)) {
found_sequence = true;
}
if matches!(event, Event::MappingStart(_)) {
found_mapping = true;
}
if found_sequence && found_mapping {
println!("\n✅ Mixed content parsed successfully (sequence and mapping).");
break;
}
}
if !found_sequence {
println!("\n❌ Sequence start event was not found.");
}
if !found_mapping {
println!("\n❌ Mapping start event was not found.");
}
}
// Example 14: Error handling with invalid input
{
let input = Cow::Borrowed(b"invalid: [yaml").as_ref(); // Invalid YAML
let mut parser = Parser::new(Cow::Borrowed(input));
match parser.parse_next_event() {
Ok(_) => println!(
"\n❌ Unexpectedly parsed invalid input without error."
),
Err(err) => println!(
"\n✅ Correctly handled error for invalid input: {:?}",
err
),
}
}
// Example 15: Parser initialization check
{
let input = Cow::Borrowed(b"foo: bar\n").as_ref();
let parser = Parser::new(Cow::Borrowed(input));
if parser.is_ok() {
println!("\n✅ Parser initialized successfully.");
} else {
println!("\n❌ Parser failed to initialize.");
}
}
// Example 16: Parsing complex nested structures
{
let input = Cow::Borrowed(
b"- item1\n- item2:\n - nested1\n - nested2\n- item3\n",
)
.as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
match event {
Event::SequenceStart(_) => {
println!("\n✅ Sequence start event found.")
}
Event::MappingStart(_) => {
println!("\n✅ Mapping start event found.")
}
Event::Scalar(scalar) => {
println!("\n✅ Scalar value: {:?}", scalar.value)
}
_ => {}
}
}
}
// Example 17: Handling comments in YAML (if supported)
{
let input =
Cow::Borrowed(b"# This is a comment\nfoo: bar\n").as_ref();
let mut parser = Parser::new(Cow::Borrowed(input));
while let Ok((event, _)) = parser.parse_next_event() {
match event {
Event::Scalar(scalar) => {
println!("\n✅ Scalar value: {:?}", scalar.value)
}
// Event::Comment(comment) => println!("\n✅ Comment: {:?}", comment), // Uncomment if comments are supported
_ => {}
}
}
}
}
serde_yml-0.0.12/examples/libyml/safe_cstr_examples.rs 0000644 0000000 0000000 00000013335 10461020230 0021217 0 ustar 0000000 0000000 //! Examples for the `CStr` struct and its methods in the `safe_cstr` module.
//!
//! This file demonstrates the creation, usage, and comparison of `CStr` instances,
//! as well as the usage of its various methods.
use serde_yml::libyml::safe_cstr::{CStr, CStrError};
use std::ffi::CString;
use std::ptr::NonNull;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/libyml/safe_cstr_examples.rs");
// Example: Creating a new CStr instance from a byte slice with a null terminator
let bytes: &'static [u8] = b"hello\0";
match CStr::from_bytes_with_nul(bytes) {
Ok(cstr) => {
println!("\n✅ Created a new CStr instance: {:?}", cstr)
}
Err(_) => println!("\n❌ Failed to create CStr instance"),
}
// Example: Creating a new CStr instance from a byte slice without a null terminator
let bytes: &'static [u8] = b"hello";
match CStr::from_bytes_with_nul(bytes) {
Ok(_) => println!("\n❌ This should not happen"),
Err(CStrError) => println!("\n✅ Correctly failed to create CStr instance without null terminator"),
}
// Example: Creating a new CStr instance from an empty byte slice
let bytes: &'static [u8] = b"";
match CStr::from_bytes_with_nul(bytes) {
Ok(_) => println!("\n❌ This should not happen"),
Err(CStrError) => println!("\n✅ Correctly failed to create CStr instance from empty byte slice"),
}
// Example: Creating a new CStr instance from a byte slice with only a null terminator
let bytes: &'static [u8] = b"\0";
match CStr::from_bytes_with_nul(bytes) {
Ok(cstr) => {
println!("\n✅ Created an empty CStr instance: {:?}", cstr)
}
Err(_) => println!("\n❌ Failed to create CStr instance"),
}
// Example: Creating a new CStr instance from a byte slice with one character and a null terminator
let bytes: &'static [u8] = b"a\0";
match CStr::from_bytes_with_nul(bytes) {
Ok(cstr) => println!(
"\n✅ Created a CStr instance with one character: {:?}",
cstr
),
Err(_) => println!("\n❌ Failed to create CStr instance"),
}
// Example: Creating a new CStr instance from a non-null pointer
let c_string = CString::new("hello").unwrap();
let ptr = NonNull::new(c_string.into_raw()).unwrap();
let cstr = CStr::from_ptr(ptr);
println!(
"\n✅ Created a new CStr instance from a pointer: {:?}",
cstr
);
// Example: Calculating the length of the CStr instance
let bytes: &'static [u8] = b"hello\0";
let cstr = CStr::from_bytes_with_nul(bytes).unwrap();
println!("\n✅ Length of the CStr instance: {}", cstr.len());
// Example: Checking if the CStr instance is empty
let bytes: &'static [u8] = b"\0";
let cstr = CStr::from_bytes_with_nul(bytes).unwrap();
println!("\n✅ Is the CStr instance empty? {}", cstr.is_empty());
// Example: Retrieving the underlying byte slice of the CStr instance
let bytes: &'static [u8] = b"hello\0";
let cstr = CStr::from_bytes_with_nul(bytes).unwrap();
println!(
"\n✅ The underlying byte slice of the CStr instance: {:?}",
cstr.to_bytes()
);
// Example: Using the Display implementation for CStr
let bytes: &'static [u8] = b"hello\0";
let cstr = CStr::from_bytes_with_nul(bytes).unwrap();
println!(
"\n✅ Display representation of the CStr instance: {}",
cstr
);
// Example: Using the Debug implementation for CStr
let bytes: &'static [u8] = b"hello\0";
let cstr = CStr::from_bytes_with_nul(bytes).unwrap();
println!(
"\n✅ Debug representation of the CStr instance: {:?}",
cstr
);
// Example: Using the Display implementation for CStr with invalid UTF-8
let bytes: &'static [u8] = b"hello\xFFworld\0";
let cstr = CStr::from_bytes_with_nul(bytes).unwrap();
println!("\n✅ Display representation of the CStr instance with invalid UTF-8: {}", cstr);
// Example: Using the Debug implementation for CStr with invalid UTF-8
let bytes: &'static [u8] = b"hello\xFFworld\0";
let cstr = CStr::from_bytes_with_nul(bytes).unwrap();
println!("\n✅ Debug representation of the CStr instance with invalid UTF-8: {:?}", cstr);
// Example: Handling the custom CStrError error type
let error = CStrError;
println!("\n✅ Custom CStrError message: {}", error);
// Example: Creating a CStr instance with a very long string
const LONG_STRING_SIZE: usize = 10_000;
let mut long_string = Vec::with_capacity(LONG_STRING_SIZE + 1);
long_string.extend(std::iter::repeat(b'a').take(LONG_STRING_SIZE));
long_string.push(b'\0');
let bytes = Box::leak(long_string.into_boxed_slice());
match CStr::from_bytes_with_nul(bytes) {
Ok(cstr) => println!("\n✅ Created a CStr instance with a long string: Length = {}", cstr.len()),
Err(_) => println!("\n❌ Failed to create CStr instance with long string"),
}
// Example: Creating a CStr instance with Unicode characters
let bytes: &'static [u8] = "hello🌍\0".as_bytes();
match CStr::from_bytes_with_nul(bytes) {
Ok(cstr) => println!("\n✅ Created a CStr instance with Unicode characters: {:?}", cstr),
Err(_) => println!("\n❌ Failed to create CStr instance with Unicode characters"),
}
// Example: Creating a CStr instance with multiple null terminators
let bytes: &'static [u8] = b"hello\0world\0";
match CStr::from_bytes_with_nul(bytes) {
Ok(cstr) => println!("\n✅ Created a CStr instance with multiple null terminators: {:?}", cstr),
Err(_) => println!("\n❌ Failed to create CStr instance with multiple null terminators"),
}
}
serde_yml-0.0.12/examples/libyml/tag_examples.rs 0000644 0000000 0000000 00000012675 10461020230 0020027 0 ustar 0000000 0000000 //! Examples for the `Tag` struct and its methods in the `tag` module.
//!
//! This file demonstrates the creation, usage, and comparison of `Tag` instances,
//! as well as the usage of its various methods.
use serde_yml::libyml::tag::{Tag, TagFormatError};
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/libyml/tag_examples.rs");
// Example: Creating a new Tag instance
let tag_null = Tag::new(Tag::NULL);
println!(
"\n✅ Created a new Tag instance for NULL: {:?}",
tag_null
);
// Example: Creating a Tag instance for a custom tag
let custom_tag = Tag::new("tag:example.org,2024:custom");
println!(
"\n✅ Created a new Tag instance for custom tag: {:?}",
custom_tag
);
// Example: Checking if a Tag starts with a prefix
match custom_tag.starts_with("tag:example.org") {
Ok(true) => {
println!("\n✅ The tag starts with the given prefix.")
}
Ok(false) => println!(
"\n✅ The tag does not start with the given prefix."
),
Err(TagFormatError) => {
println!("\n✅ Error: The prefix is longer than the tag.")
}
}
// Example: Comparing a Tag with a &str
let comparison_str = "tag:example.org,2024:custom";
if custom_tag == comparison_str {
println!("\n✅ The tag is equal to the given string slice.");
} else {
println!(
"\n✅ The tag is not equal to the given string slice."
);
}
// Example: Using Deref to access the underlying byte slice
let tag_bytes: &[u8] = &custom_tag;
println!(
"\n✅ The underlying byte slice of the tag: {:?}",
tag_bytes
);
// Example: Using the Debug implementation
println!(
"\n✅ Debug representation of the custom tag: {:?}",
custom_tag
);
// Example: Using Tag constants
let tag_bool = Tag::new(Tag::BOOL);
println!(
"\n✅ Created a new Tag instance for BOOL: {:?}",
tag_bool
);
let tag_int = Tag::new(Tag::INT);
println!("\n✅ Created a new Tag instance for INT: {:?}", tag_int);
let tag_float = Tag::new(Tag::FLOAT);
println!(
"\n✅ Created a new Tag instance for FLOAT: {:?}",
tag_float
);
// Example: Handling TagFormatError when the prefix is longer than the tag
match custom_tag.starts_with("tag:example.org,2024:custom:extra") {
Ok(_) => println!("\n✅ The tag starts with the given prefix."),
Err(TagFormatError) => {
println!("\n✅ Error: The prefix is longer than the tag.")
}
}
// Example: Validating a list of YAML tags
let tags = vec![
Tag::new("tag:example.org,2024:custom1"),
Tag::new("tag:example.org,2024:custom2"),
Tag::new("tag:example.com,2024:other"),
];
for tag in &tags {
if tag.starts_with("tag:example.org").unwrap_or(false) {
println!("\n✅ The tag {:?} starts with the prefix 'tag:example.org'", tag);
} else {
println!("\n✅ The tag {:?} does not start with the prefix 'tag:example.org'", tag);
}
}
// Example: Comparing tags with different formats
let another_custom_tag = Tag::new("tag:example.org,2024:custom");
if custom_tag == another_custom_tag {
println!("\n✅ The custom_tag is equal to another_custom_tag.");
} else {
println!(
"\n✅ The custom_tag is not equal to another_custom_tag."
);
}
// Example: Filtering tags based on a prefix
let filtered_tags: Vec<&Tag> = tags
.iter()
.filter(|tag| {
tag.starts_with("tag:example.org").unwrap_or(false)
})
.collect();
println!(
"\n✅ Filtered tags that start with 'tag:example.org': {:?}",
filtered_tags
);
// Example: Creating a custom function to process tags
fn print_tag_info(tag: &Tag) {
println!("\n📌 Tag info: {:?}", tag);
if tag.starts_with("tag:example.org").unwrap_or(false) {
println!("✅ This tag starts with 'tag:example.org'");
} else {
println!(
"❌ This tag does not start with 'tag:example.org'"
);
}
}
let custom_tag = Tag::new("tag:example.org,2024:custom");
print_tag_info(&custom_tag);
// Example: Error handling with invalid tags
let invalid_tag = "invalid:tag";
match Tag::new(invalid_tag).starts_with("tag:example.org") {
Ok(_) => println!(
"\n✅ The invalid_tag starts with the given prefix."
),
Err(TagFormatError) => {
println!("\n❌ Error: The prefix is longer than the invalid_tag.")
}
}
// Example: Real-world scenario - parsing and validating tags from a YAML document
let yaml_tags = vec![
"tag:example.org,2024:custom1",
"tag:example.org,2024:custom2",
"tag:example.com,2024:other",
"invalid:tag",
];
for yaml_tag in yaml_tags {
let tag = Tag::new(yaml_tag);
match tag.starts_with("tag:example.org") {
Ok(true) => println!("\n✅ The tag {:?} is valid and starts with 'tag:example.org'", tag),
Ok(false) => println!("\n✅ The tag {:?} is valid but does not start with 'tag:example.org'", tag),
Err(TagFormatError) => println!("\n❌ The tag {:?} is invalid or the prefix is too long", tag),
}
}
}
serde_yml-0.0.12/examples/libyml/util_examples.rs 0000644 0000000 0000000 00000004377 10461020230 0020231 0 ustar 0000000 0000000 //! Examples for the `Owned` and `InitPtr` structs and their methods in the `util` module.
//!
//! This file demonstrates the creation, usage, and safety considerations of `Owned` and `InitPtr` instances,
//! as well as the usage of their various methods.
use serde_yml::libyml::util::{InitPtr, Owned};
use std::mem::MaybeUninit;
use std::ops::Deref;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/libyml/util_examples.rs");
// Example: Creating a new uninitialized Owned instance
let uninit_owned: Owned, i32> =
Owned::new_uninit();
println!(
"\n✅ Created a new uninitialized Owned instance: {:?}",
uninit_owned
);
// Example: Converting an uninitialized Owned instance to an initialized one
let init_owned: Owned =
unsafe { Owned::assume_init(uninit_owned) };
println!(
"\n✅ Converted to an initialized Owned instance: {:?}",
init_owned
);
// Example: Dereferencing an Owned instance
let init_ptr = init_owned.deref().ptr;
println!(
"\n✅ Dereferenced the Owned instance to get the InitPtr: {:?}",
init_ptr
);
// Example: Creating an InitPtr instance
let mut value: i32 = 42;
let init_ptr = InitPtr { ptr: &mut value };
println!(
"\n✅ Created an InitPtr instance: {:?} with value: {}",
init_ptr,
unsafe { *init_ptr.ptr }
);
// Example: Using the Drop implementation
{
let drop_owned: Owned, i32> =
Owned::new_uninit();
println!(
"\n✅ Created a new Owned instance to be dropped: {:?}",
drop_owned
);
} // drop_owned goes out of scope here, and memory is deallocated.
// Example: Creating Owned instances with different types
let uninit_owned_f64: Owned, f64> =
Owned::new_uninit();
println!(
"\n✅ Created a new uninitialized Owned instance: {:?}",
uninit_owned_f64
);
let init_owned_f64: Owned =
unsafe { Owned::assume_init(uninit_owned_f64) };
println!(
"\n✅ Converted to an initialized Owned instance: {:?}",
init_owned_f64
);
}
serde_yml-0.0.12/examples/loader/anchors_and_aliases.rs 0000644 0000000 0000000 00000003000 10461020230 0021272 0 ustar 0000000 0000000 use serde_yml::{
de::{Event, Progress},
loader::Loader,
};
use std::str;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!(
"\n❯ Executing examples/loader/anchors_and_anchor_event_map.rs"
);
let input = "---\nkey: &anchor value\nalias: *anchor\n...";
let progress = Progress::Str(input);
let mut loader = Loader::new(progress).unwrap();
let document = loader.next_document().unwrap();
// Print a success message and present the results to the user.
println!(
"\n✅ Successfully loaded document with {} events:",
document.events.len()
);
for (event, mark) in &document.events {
println!("\tEvent: {:?}, Mark: {:?}", event, mark);
}
// Perform assertions to verify that the loader is working as expected.
assert!(document.error.is_none());
assert_eq!(document.anchor_event_map.len(), 1);
let (event, _) = &document.events[1];
if let Event::Scalar(scalar) = event {
assert_eq!(str::from_utf8(&scalar.value).unwrap(), "key");
assert_eq!(scalar.anchor, None);
} else {
panic!("Expected Event::Scalar");
}
let (event, _) = &document.events[3];
if let Event::Scalar(scalar) = event {
assert_eq!(str::from_utf8(&scalar.value).unwrap(), "alias");
assert_eq!(scalar.anchor, None);
} else {
panic!("Expected Event::Scalar");
}
let (event, _) = &document.events[4];
assert!(matches!(event, Event::Alias(0)));
}
serde_yml-0.0.12/examples/loader/io_errors.rs 0000644 0000000 0000000 00000000755 10461020230 0017333 0 ustar 0000000 0000000 use serde_yml::{de::Progress, loader::Loader};
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/loader/io_errors.rs");
let faulty_reader = std::io::Cursor::new(b"---\n- key: value\n");
let progress = Progress::Read(Box::new(faulty_reader));
match Loader::new(progress) {
Ok(_) => println!("\n✅ Loader created successfully"),
Err(e) => println!("Failed to create loader: {}", e),
}
}
serde_yml-0.0.12/examples/loader/mod.rs 0000644 0000000 0000000 00000001674 10461020230 0016110 0 ustar 0000000 0000000 /// This module contains the `single_document` example.
pub(crate) mod single_document;
/// This module contains the `multiple_documents` example.
pub(crate) mod multiple_documents;
/// This module contains the `unknown anchor` example.
pub(crate) mod unknown_anchor;
/// This module contains the `anchors_and_aliases` example.
pub(crate) mod anchors_and_aliases;
/// this module contains the `io_errors` example.
pub(crate) mod io_errors;
/// The main function that runs all the example modules.
pub(crate) fn main() {
// Run the example module `loader_anchors_and_aliases`.
anchors_and_aliases::main();
// Run the example module `loader_io_errors`.
io_errors::main();
// Run the example module `loader_multiple_documents`.
multiple_documents::main();
// Run the example module `loader_single_document`.
single_document::main();
// Run the example module `loader_unknown_anchor`.
unknown_anchor::main();
}
serde_yml-0.0.12/examples/loader/multiple_documents.rs 0000644 0000000 0000000 00000006365 10461020230 0021247 0 ustar 0000000 0000000 // Import necessary modules from the serde_yml crate.
use serde_yml::{de::Progress, loader::Loader};
// Define the main function.
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/loader/multiple_documents.rs");
// Define the YAML input string containing multiple documents.
let input = "---\nkey1: value1\n...\n---\nkey2: value2\n...";
// Create a progress indicator for the loader using the input string.
let progress = Progress::Str(input);
// Attempt to create a new loader for deserializing YAML data.
match Loader::new(progress) {
Ok(mut loader) => {
// If the loader creation is successful, print a success message.
println!("\n✅ Loader created successfully");
// Attempt to load the first document from the loader.
if let Some(document1) = loader.next_document() {
// If document loading is successful, print a success message and present the results to the user.
println!("\n✅ Document 1 successfully loaded:");
for (event, mark) in &document1.events {
println!("\tEvent: {:?}, Mark: {:?}", event, mark);
}
println!(); // Add a newline for better formatting
// Perform assertions to verify that the loader is working as expected.
assert_eq!(document1.events.len(), 4);
assert!(document1.error.is_none());
assert_eq!(document1.anchor_event_map.len(), 0);
} else {
// If document loading fails, print an error message.
println!("Failed to load document 1");
}
// Attempt to load the second document from the loader.
if let Some(document2) = loader.next_document() {
// If document loading is successful, print a success message and present the results to the user.
println!("\n✅ Document 2 successfully loaded:");
for (event, mark) in &document2.events {
println!("\tEvent: {:?}, Mark: {:?}", event, mark);
}
println!(); // Add a newline for better formatting
// Perform assertions to verify that the loader is working as expected.
assert_eq!(document2.events.len(), 4);
assert!(document2.error.is_none());
assert_eq!(document2.anchor_event_map.len(), 0);
} else {
// If document loading fails, print an error message.
println!("Failed to load document 2");
}
// Check if there are more documents in the loader.
if loader.next_document().is_none() {
// If no more documents are present, print a success message.
println!("\n✅ All documents loaded successfully");
} else {
// If more documents are present, print an error message.
println!("Failed to load all documents");
}
}
Err(e) => {
// If loader creation fails, print an error message.
println!("Failed to create loader: {}", e);
}
}
}
serde_yml-0.0.12/examples/loader/single_document.rs 0000644 0000000 0000000 00000003530 10461020230 0020501 0 ustar 0000000 0000000 // Import necessary modules from the serde_yml crate.
use serde_yml::{de::Progress, loader::Loader};
/// Example demonstrating the usage of Serde YML's `Loader` for YAML deserialization.
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/loader/single_document.rs");
// Sample YAML input string
let input = "key: value";
// Create a progress indicator for the loader using the input string
let progress = Progress::Str(input);
// Attempt to create a new loader for deserializing YAML data
match Loader::new(progress) {
Ok(mut loader) => {
// Attempt to retrieve the next YAML document from the loader
if let Some(document) = loader.next_document() {
// Assert that the number of events in the document is as expected
assert_eq!(document.events.len(), 4);
// Assert that there are no errors in the document
assert!(document.error.is_none());
// Assert that there are no anchor_event_map in the document
assert_eq!(document.anchor_event_map.len(), 0);
// Print a success message and present the results to the user
println!("\n✅ Document successfully loaded:");
for (event, mark) in &document.events {
println!("\tEvent: {:?}, Mark: {:?}", event, mark);
}
} else {
// If no document was returned, print an error message
println!(
"Failed to load document: ❌ No document found"
);
}
}
Err(e) => {
// If there was an error creating the loader, print the error message
println!("Failed to create loader: ❌ {}", e);
}
}
}
serde_yml-0.0.12/examples/loader/unknown_anchor.rs 0000644 0000000 0000000 00000003666 10461020230 0020365 0 ustar 0000000 0000000 // Import necessary modules from the serde_yml crate.
use serde_yml::{
de::Progress, loader::Loader, modules::error::ErrorImpl,
};
/// Example demonstrating the usage of Serde YML's `Loader` for YAML deserialization.
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/loader/unknown_anchor.rs");
// YAML input string with an unknown anchor
let input = "*unknown";
// Create a progress indicator for the loader using the input string
let progress = Progress::Str(input);
// Create a new loader for deserializing YAML data, unwrapping the result
let mut loader = match Loader::new(progress) {
Ok(loader) => loader,
Err(e) => {
// If there was an error creating the loader, print the error message and exit
println!("Failed to create loader: ❌ {}", e);
return;
}
};
// Attempt to retrieve the next YAML document from the loader
let document = match loader.next_document() {
Some(document) => document,
None => {
// If no document was returned, print an error message and exit
println!("Failed to load document: ❌ No document found");
return;
}
};
// Assert that the number of events in the document is as expected
assert_eq!(document.events.len(), 0);
// Assert that there is an error in the document
assert!(document.error.is_some());
// Assert that there are no anchor_event_map in the document
assert_eq!(document.anchor_event_map.len(), 0);
// Retrieve the error from the document
let error = document.error.unwrap();
// Assert that the error matches the expected UnknownAnchor variant
assert!(matches!(*error, ErrorImpl::UnknownAnchor(_)));
// Print a success message and present the error to the user
println!("\n✅ Document loaded with expected error: \n\t{}", error);
}
serde_yml-0.0.12/examples/modules/mod.rs 0000644 0000000 0000000 00000000337 10461020230 0016305 0 ustar 0000000 0000000 /// This module contains the `path` example.
pub(crate) mod path_examples;
/// The main function that runs all the example modules.
pub(crate) fn main() {
// Run the example module `path`.
path_examples::main();
}
serde_yml-0.0.12/examples/modules/path_examples.rs 0000644 0000000 0000000 00000015446 10461020230 0020367 0 ustar 0000000 0000000 //! Examples for the `Path` enum and its usage in the `path` module.
//!
//! This file demonstrates the creation, usage, and formatting of `Path` instances,
//! as well as handling various path scenarios.
use serde_yml::modules::path::Path;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/libyml/path_examples.rs");
// Example: Creating a Path::Root instance
let path_root = Path::Root;
println!("\n✅ Created a Path::Root instance: {}", path_root); // Output: .
// Example: Creating a Path::Seq instance
let path_seq = Path::Seq {
parent: &path_root,
index: 42,
};
println!("\n✅ Created a Path::Seq instance: {}", path_seq); // Output: [42]
// Example: Creating a Path::Map instance
let path_map = Path::Map {
parent: &path_root,
key: "key",
};
println!("\n✅ Created a Path::Map instance: {}", path_map); // Output: key
// Example: Creating a Path::Alias instance
let path_alias = Path::Alias { parent: &path_root };
println!("\n✅ Created a Path::Alias instance: {}", path_alias); // Output: (empty string)
// Example: Creating a Path::Unknown instance
let path_unknown = Path::Unknown { parent: &path_root };
println!("\n✅ Created a Path::Unknown instance: {}", path_unknown); // Output: ?
// Example: Nested paths
let path_nested = Path::Unknown {
parent: &Path::Alias {
parent: &Path::Map {
parent: &Path::Seq {
parent: &path_root,
index: 0,
},
key: "key",
},
},
};
println!("\n✅ Created a nested Path instance: {}", path_nested); // Output: [0].key..?
// Example: Deeply nested paths
let path_deeply_nested = Path::Unknown {
parent: &Path::Alias {
parent: &Path::Map {
parent: &Path::Seq {
parent: &Path::Map {
parent: &Path::Seq {
parent: &path_root,
index: 1,
},
key: "first",
},
index: 2,
},
key: "second",
},
},
};
println!(
"\n✅ Created a deeply nested Path instance: {}",
path_deeply_nested
); // Output: [1].first[2].second..?
// Example: Path with an empty key in Path::Map
let path_map_empty_key = Path::Map {
parent: &path_root,
key: "",
};
println!(
"\n✅ Created a Path::Map instance with an empty key: {}",
path_map_empty_key
); // Output: (empty string)
// Example: Path with maximum index in Path::Seq
let path_seq_max_index = Path::Seq {
parent: &path_root,
index: usize::MAX,
};
println!(
"\n✅ Created a Path::Seq instance with max index: {}",
path_seq_max_index
); // Output: [18446744073709551615]
// Example: Complex nested paths
let path_complex_nested = Path::Unknown {
parent: &Path::Alias {
parent: &Path::Map {
parent: &Path::Seq {
parent: &Path::Map {
parent: &Path::Seq {
parent: &Path::Map {
parent: &path_root,
key: "third",
},
index: 3,
},
key: "second",
},
index: 2,
},
key: "first",
},
},
};
println!(
"\n✅ Created a complex nested Path instance: {}",
path_complex_nested
); // Output: [2].first[3].second.third..?
// Example: Path with multiple unknowns
let path_multiple_unknowns = Path::Unknown {
parent: &Path::Unknown {
parent: &Path::Unknown { parent: &path_root },
},
};
println!(
"\n✅ Created a Path instance with multiple unknowns: {}",
path_multiple_unknowns
); // Output: .?.?.?
// Example: Path with multiple aliases
let path_multiple_aliases = Path::Alias {
parent: &Path::Alias {
parent: &Path::Alias { parent: &path_root },
},
};
println!(
"\n✅ Created a Path instance with multiple aliases: {}",
path_multiple_aliases
); // Output: ..
// Example: Path with multiple sequences
let path_multiple_sequences = Path::Seq {
parent: &Path::Seq {
parent: &Path::Seq {
parent: &path_root,
index: 1,
},
index: 2,
},
index: 3,
};
println!(
"\n✅ Created a Path instance with multiple sequences: {}",
path_multiple_sequences
); // Output: \[1\].\[2\].\[3\]
// Example: Path with multiple maps
let path_multiple_maps = Path::Map {
parent: &Path::Map {
parent: &Path::Map {
parent: &path_root,
key: "first",
},
key: "second",
},
key: "third",
};
println!(
"\n✅ Created a Path instance with multiple maps: {}",
path_multiple_maps
); // Output: first.second.third
// Example: Path with multiple aliases, sequences, and maps
let path_multiple_nested = Path::Alias {
parent: &Path::Seq {
parent: &Path::Map {
parent: &Path::Alias { parent: &path_root },
key: "first",
},
index: 2,
},
};
println!(
"\n✅ Created a Path instance with multiple nested paths: {}",
path_multiple_nested
); // Output: .first.\[2\].
// Example: Path with multiple unknowns, aliases, sequences, and maps
let path_multiple_complex = Path::Unknown {
parent: &Path::Alias {
parent: &Path::Seq {
parent: &Path::Map {
parent: &Path::Unknown { parent: &path_root },
key: "first",
},
index: 2,
},
},
};
println!(
"\n✅ Created a Path instance with multiple complex paths: {}",
path_multiple_complex
); // Output: ?.first.\[2\]..?
// Example: Comparing Path instances
let another_path_seq = Path::Seq {
parent: &path_root,
index: 42,
};
if path_seq == another_path_seq {
println!("\n✅ The path_seq is equal to another_path_seq.");
} else {
println!("\n❌ The path_seq is not equal to another_path_seq.");
}
// Example: Debug representation of Path instances
println!("\n✅ Debug representation of path_seq: {:?}", path_seq);
}
serde_yml-0.0.12/examples/serializer/basic.rs 0000644 0000000 0000000 00000001416 10461020230 0017307 0 ustar 0000000 0000000 //!
//! Example for basic usage of the YAML serializer.
//!
//! This example demonstrates how to serialize a simple struct into YAML format
//! using the `Serializer` provided by the `serde_yml` crate.
//!
use serde::Serialize;
use serde_yml::Serializer;
#[derive(Serialize)]
struct Person {
name: String,
age: u32,
city: String,
}
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/serializer/basic.rs");
let person = Person {
name: "John Doe".to_string(),
age: 30,
city: "New York".to_string(),
};
let mut serializer = Serializer::new(std::io::stdout());
person.serialize(&mut serializer).unwrap();
println!("\n✅ Person serialized to YAML.");
}
serde_yml-0.0.12/examples/serializer/collections.rs 0000644 0000000 0000000 00000001536 10461020230 0020547 0 ustar 0000000 0000000 //!
//! Example for serializing collections with the YAML serializer.
//!
//! This example demonstrates how to serialize various collection types (Vec,
//! HashMap) into YAML format using the `Serializer` provided by the `serde_yml`
//! crate.
//!
use serde_yml::{to_string, Result};
use std::collections::HashMap;
pub(crate) fn main() -> Result<()> {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/serializer/collections.rs");
let numbers = vec![1, 2, 3, 4, 5];
let yaml = to_string(&numbers)?;
println!("\n✅ Vec serialized to YAML:\n{}", yaml);
let mut map = HashMap::new();
map.insert("key1", "value1");
map.insert("key2", "value2");
map.insert("key3", "value3");
let yaml = to_string(&map)?;
println!("\n✅ HashMap serialized to YAML:\n{}", yaml);
Ok(())
}
serde_yml-0.0.12/examples/serializer/complex_nested.rs 0000644 0000000 0000000 00000003321 10461020230 0021234 0 ustar 0000000 0000000 //!
//! Example for serializing complex nested data structures with the YAML serializer.
//!
//! This example demonstrates how the serializer handles complex nested data structures
//! when serializing a struct into YAML format using the `Serializer` provided by the `serde_yml` crate.
//!
use serde::Serialize;
use serde_yml::{to_string, Result};
use std::collections::HashMap;
#[derive(Serialize)]
enum Shape {
Circle { radius: f64 },
Rectangle { width: f64, height: f64 },
}
#[derive(Serialize)]
struct Node {
name: String,
children: Vec,
properties: HashMap,
shape: Shape,
}
pub(crate) fn main() -> Result<()> {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/serializer/complex_nested.rs");
let root = Node {
name: "root".to_string(),
children: vec![
Node {
name: "child1".to_string(),
children: vec![],
properties: HashMap::new(),
shape: Shape::Circle { radius: 5.0 },
},
Node {
name: "child2".to_string(),
children: vec![],
properties: [("color".to_string(), "blue".to_string())]
.iter()
.cloned()
.collect(),
shape: Shape::Rectangle {
width: 10.0,
height: 20.0,
},
},
],
properties: HashMap::new(),
shape: Shape::Circle { radius: 10.0 },
};
let yaml = to_string(&root)?;
println!(
"\n✅ Complex nested data structure serialized to YAML:\n{}",
yaml
);
Ok(())
}
serde_yml-0.0.12/examples/serializer/custom_serialization.rs 0000644 0000000 0000000 00000001711 10461020230 0022473 0 ustar 0000000 0000000 //!
//! Example for custom serialization implementations with the YAML serializer.
//!
//! This example demonstrates how to use custom serialization implementations
//! with the YAML serializer provided by the `serde_yml` crate.
//!
use serde::Serialize;
use serde_yml::{to_string, Result};
struct Point {
x: i32,
y: i32,
}
impl Serialize for Point {
fn serialize(
&self,
serializer: S,
) -> std::result::Result
where
S: serde::Serializer,
{
serializer.serialize_str(&format!("({}, {})", self.x, self.y))
}
}
pub(crate) fn main() -> Result<()> {
// Print a message to indicate the file being executed.
println!(
"\n❯ Executing examples/serializer/custom_serialization.rs"
);
let point = Point { x: 3, y: 7 };
let yaml = to_string(&point)?;
println!(
"\n✅ Point serialized with custom implementation:\n{}",
yaml
);
Ok(())
}
serde_yml-0.0.12/examples/serializer/enums.rs 0000644 0000000 0000000 00000001655 10461020230 0017362 0 ustar 0000000 0000000 //!
//! Example for serializing enums with the YAML serializer.
//!
//! This example demonstrates how to serialize enums with associated data into
//! YAML format using the `Serializer` provided by the `serde_yml` crate.
//!
use serde::Serialize;
use serde_yml::{to_string, Result};
#[derive(Serialize)]
enum Shape {
Rectangle { width: u32, height: u32 },
Circle { radius: f64 },
Triangle { base: u32, height: u32 },
}
pub(crate) fn main() -> Result<()> {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/serializer/enums.rs");
let shapes = vec![
Shape::Rectangle {
width: 10,
height: 20,
},
Shape::Circle { radius: 5.0 },
Shape::Triangle {
base: 8,
height: 12,
},
];
let yaml = to_string(&shapes)?;
println!("\n✅ Shapes serialized to YAML:\n{}", yaml);
Ok(())
}
serde_yml-0.0.12/examples/serializer/error_handling.rs 0000644 0000000 0000000 00000002133 10461020230 0021220 0 ustar 0000000 0000000 //!
//! Example for error handling and serialization of custom error types with the YAML serializer.
//!
//! This example demonstrates how to handle and serialize custom error types
//! with the YAML serializer provided by the `serde_yml` crate.
//!
use serde::Serialize;
use serde_yml::{to_string, Result};
use std::fmt;
#[derive(Serialize, Debug)]
struct CustomError {
message: String,
}
impl fmt::Display for CustomError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "CustomError: {}", self.message)
}
}
impl std::error::Error for CustomError {}
#[derive(Serialize)]
struct ErrorWrapper {
error: Option,
}
pub(crate) fn main() -> Result<()> {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/serializer/error_handling.rs");
let error = CustomError {
message: "Something went wrong".to_string(),
};
let wrapper = ErrorWrapper { error: Some(error) };
let yaml = to_string(&wrapper)?;
println!("\n✅ Custom error type serialized to YAML:\n{}", yaml);
Ok(())
}
serde_yml-0.0.12/examples/serializer/mod.rs 0000644 0000000 0000000 00000003320 10461020230 0017001 0 ustar 0000000 0000000 //! Example modules for demonstrating the usage of the YAML serializer.
//!
//! This module contains example modules that demonstrate the usage of the YAML
//! serializer provided by the `serde_yml` crate. Each example module demonstrates
//! a different aspect of the serializer, such as serializing basic types, structs,
//! enums, and collections.
//!
/// This module contains the `basic` example.
pub(crate) mod basic;
/// This module contains the `collections` example.
pub(crate) mod collections;
/// This module contains the `complex_nested` example.
pub(crate) mod complex_nested;
/// This module contains the `custom_serialization.rs` example.
pub(crate) mod custom_serialization;
/// This module contains the `enums` example.
pub(crate) mod enums;
/// This module contains the `error_handling` example.
pub(crate) mod error_handling;
/// This module contains the `optional_and_default` example.
pub(crate) mod optional_and_default;
/// This module contains the `structs` example.
pub(crate) mod structs;
/// The main function that runs all the example modules.
pub(crate) fn main() {
// Run the example module `basic`.
basic::main();
// Run the example module `collections`.
let _ = collections::main();
// Run the example module `complex_nested`.
let _ = complex_nested::main();
// Run the example module `custom_serialization`.
let _ = custom_serialization::main();
// Run the example module `enums`.
let _ = enums::main();
// Run the example module `error_handling`.
let _ = error_handling::main();
// Run the example module `optional_and_default`.
let _ = optional_and_default::main();
// Run the example module `structs`.
let _ = structs::main();
}
serde_yml-0.0.12/examples/serializer/optional_and_default.rs 0000644 0000000 0000000 00000002231 10461020230 0022375 0 ustar 0000000 0000000 //!
//! Example for serializing optional fields and default values with the YAML serializer.
//!
//! This example demonstrates how the serializer handles optional fields and default values
//! when serializing a struct into YAML format using the `Serializer` provided by the `serde_yml` crate.
//!
use serde::Serialize;
use serde_yml::{to_string, Result};
#[derive(Serialize)]
struct User {
name: String,
age: Option,
#[serde(default)]
is_active: bool,
}
pub(crate) fn main() -> Result<()> {
// Print a message to indicate the file being executed.
println!(
"\n❯ Executing examples/serializer/optional_and_default.rs"
);
let user1 = User {
name: "John".to_string(),
age: Some(30),
is_active: true,
};
let yaml = to_string(&user1)?;
println!(
"\n✅ User with optional fields serialized to YAML:\n{}",
yaml
);
let user2 = User {
name: "Jane".to_string(),
age: None,
is_active: false,
};
let yaml = to_string(&user2)?;
println!(
"\n✅ User with default values serialized to YAML:\n{}",
yaml
);
Ok(())
}
serde_yml-0.0.12/examples/serializer/structs.rs 0000644 0000000 0000000 00000001727 10461020230 0017742 0 ustar 0000000 0000000 //!
//! Example for serializing structs with the YAML serializer.
//!
//! This example demonstrates how to serialize nested structs into YAML format
//! using the `Serializer` provided by the `serde_yml` crate.
//!
use serde::Serialize;
use serde_yml::{to_string, Result};
#[derive(Serialize)]
struct Address {
street: String,
city: String,
country: String,
}
#[derive(Serialize)]
struct User {
name: String,
age: u32,
address: Address,
}
pub(crate) fn main() -> Result<()> {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/serializer/structs.rs");
let user = User {
name: "Alice".to_string(),
age: 25,
address: Address {
street: "123 Main St".to_string(),
city: "Anytown".to_string(),
country: "USA".to_string(),
},
};
let yaml = to_string(&user)?;
println!("\n✅ User serialized to YAML:\n{}", yaml);
Ok(())
}
serde_yml-0.0.12/examples/value/de_examples.rs 0000644 0000000 0000000 00000007407 10461020230 0017465 0 ustar 0000000 0000000 //! This file demonstrates the deserialization of various data structures such as empty tuples,
//! empty tuple structs, newtype variants, sequences, maps, option types, and enums with multiple variants using `serde_yml`.
use serde::Deserialize;
use serde_yml::Value;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/de_examples.rs");
// Example: Deserializing an empty tuple struct.
fn example_deserialize_empty_tuple_struct() {
let yaml_str = "---";
let value: Value = serde_yml::from_str(yaml_str).unwrap();
#[derive(Deserialize, PartialEq, Debug)]
struct Empty;
let result: Empty = serde_yml::from_value(value).unwrap();
println!("\n✅ Deserialized Empty tuple struct: {:?}", result);
}
// Example: Deserializing an empty tuple.
fn example_deserialize_empty_tuple() {
let yaml_str = "---";
let value: Value = serde_yml::from_str(yaml_str).unwrap();
let result: () = serde_yml::from_value(value).unwrap();
println!("\n✅ Deserialized Empty tuple: {:?}", result);
}
// Example: Deserializing a newtype variant.
fn example_deserialize_newtype_variant() {
let yaml_str = "!Variant 0";
let value: Value = serde_yml::from_str(yaml_str).unwrap();
#[derive(Deserialize, PartialEq, Debug)]
enum E {
Variant(i32),
}
let result: E = serde_yml::from_value(value).unwrap();
println!("\n✅ Deserialized newtype variant: {:?}", result);
}
// Example: Deserializing a struct with multiple fields.
fn example_deserialize_struct_with_fields() {
let yaml_str = "
name: \"John Doe\"
age: 30
";
let value: Value = serde_yml::from_str(yaml_str).unwrap();
#[derive(Deserialize, PartialEq, Debug)]
struct Person {
name: String,
age: i32,
}
let result: Person = serde_yml::from_value(value).unwrap();
println!("\n✅ Deserialized struct with fields: {:?}", result);
}
// Example: Deserializing a sequence (Vec).
fn example_deserialize_sequence() {
let yaml_str = "
- 1
- 2
- 3
";
let value: Value = serde_yml::from_str(yaml_str).unwrap();
let result: Vec = serde_yml::from_value(value).unwrap();
println!("\n✅ Deserialized sequence: {:?}", result);
}
// Example: Deserializing a map (HashMap).
fn example_deserialize_map() {
use std::collections::HashMap;
let yaml_str = "
key1: value1
key2: value2
";
let value: Value = serde_yml::from_str(yaml_str).unwrap();
let result: HashMap =
serde_yml::from_value(value).unwrap();
println!("\n✅ Deserialized map: {:?}", result);
}
// Example: Deserializing an option type.
fn example_deserialize_option() {
let yaml_str_some = "some_value";
let value_some: Value =
serde_yml::from_str(yaml_str_some).unwrap();
let result_some: Option =
serde_yml::from_value(value_some).unwrap();
println!("\n✅ Deserialized option (Some): {:?}", result_some);
let yaml_str_none = "---";
let value_none: Value =
serde_yml::from_str(yaml_str_none).unwrap();
let result_none: Option =
serde_yml::from_value(value_none).unwrap();
println!("\n✅ Deserialized option (None): {:?}", result_none);
}
// Execute the examples
example_deserialize_empty_tuple_struct();
example_deserialize_empty_tuple();
example_deserialize_newtype_variant();
example_deserialize_struct_with_fields();
example_deserialize_sequence();
example_deserialize_map();
example_deserialize_option();
}
serde_yml-0.0.12/examples/value/index_examples.rs 0000644 0000000 0000000 00000017305 10461020230 0020202 0 ustar 0000000 0000000 //! Examples for the `Index` trait and its implementations in the `index` module.
//!
//! This file demonstrates the usage of the `Index` trait with various implementations,
//! including indexing into sequences and mappings, and handling out-of-bounds and invalid indices.
use serde_yml::value::Index;
use serde_yml::Value;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/libyml/index_examples.rs");
// Example: Indexing into a sequence using usize
let sequence = Value::Sequence(vec![
Value::Number(1.into()),
Value::Number(2.into()),
]);
let index = 1;
match index.index_into(&sequence) {
Some(value) => {
println!("\n✅ Indexed into sequence: {:?}", value)
}
None => println!("\n❌ Index out of bounds"),
}
// Example: Indexing into a mapping using usize
let mut mapping = serde_yml::Mapping::new();
mapping
.insert(Value::Number(1.into()), Value::String("one".into()));
let value = Value::Mapping(mapping);
match index.index_into(&value) {
Some(value) => {
println!("\n✅ Indexed into mapping: {:?}", value)
}
None => println!("\n❌ Key not found"),
}
// Example: Indexing into a sequence with out-of-bounds index using usize
let index = 3;
match index.index_into(&sequence) {
Some(value) => {
println!("\n✅ Indexed into sequence: {:?}", value)
}
None => println!("\n❌ Index out of bounds"),
}
// Example: Indexing into a mapping with a non-numeric key using usize
let mut mapping = serde_yml::Mapping::new();
mapping.insert(
Value::String("key".into()),
Value::String("value".into()),
);
let value = Value::Mapping(mapping);
match index.index_into(&value) {
Some(value) => {
println!("\n✅ Indexed into mapping: {:?}", value)
}
None => println!("\n❌ Key not found"),
}
// Example: Mutably indexing into a sequence using usize
let mut sequence = Value::Sequence(vec![
Value::Number(1.into()),
Value::Number(2.into()),
]);
let index = 1;
if let Some(value) = index.index_into_mut(&mut sequence) {
*value = Value::Number(3.into());
println!("\n✅ Mutably indexed into sequence: {:?}", sequence);
}
// Example: Mutably indexing into a mapping using usize
let mut mapping = serde_yml::Mapping::new();
mapping
.insert(Value::Number(1.into()), Value::String("one".into()));
let mut value = Value::Mapping(mapping);
if let Some(value) = index.index_into_mut(&mut value) {
*value = Value::String("two".into());
println!("\n✅ Mutably indexed into mapping: {:?}", value);
}
// Example: Mutably indexing into a sequence with out-of-bounds index using usize
let index = 3;
if let Some(value) = index.index_into_mut(&mut sequence) {
*value = Value::Number(4.into());
} else {
println!("\n❌ Index out of bounds");
}
// Example: Using index_or_insert with a sequence using usize
let mut sequence = Value::Sequence(vec![Value::Number(1.into())]);
let index = 1;
if index >= sequence.as_sequence().unwrap().len() {
for _ in sequence.as_sequence().unwrap().len()..=index {
sequence.as_sequence_mut().unwrap().push(Value::Null);
}
}
index
.index_or_insert(&mut sequence)
.clone_from(&Value::Number(2.into()));
println!("\n✅ Used index_or_insert with sequence: {:?}", sequence);
// Example: Using index_or_insert with a mapping using usize
let mapping = serde_yml::Mapping::new();
let mut value = Value::Mapping(mapping);
index
.index_or_insert(&mut value)
.clone_from(&Value::String("one".into()));
let mut expected_mapping = serde_yml::Mapping::new();
expected_mapping
.insert(Value::Number(1.into()), Value::String("one".into()));
println!("\n✅ Used index_or_insert with mapping: {:?}", value);
// Example: Indexing into a non-indexable value
let value = Value::String("hello".into());
match index.index_into(&value) {
Some(value) => println!("\n✅ Indexed into value: {:?}", value),
None => println!("\n❌ Cannot index into non-indexable value"),
}
// Example: Mutably indexing into a non-indexable value
let mut value = Value::String("hello".into());
if let Some(value) = index.index_into_mut(&mut value) {
*value = Value::String("world".into());
println!("\n✅ Mutably indexed into value: {:?}", value);
} else {
println!("\n❌ Cannot index into non-indexable value");
}
// Example: Using index_or_insert with a non-indexable value
let value = Value::String("hello".into());
let result = std::panic::catch_unwind(move || {
let mut value_owned = value.clone();
index.index_or_insert(&mut value_owned);
});
match result {
Ok(_) => println!("\n❌ Should have panicked"),
Err(_) => {
println!("\n✅ Correctly panicked for non-indexable value")
}
}
// Example: Using index_or_insert with a null value
let value = Value::Null;
let result = std::panic::catch_unwind(move || {
let mut value_owned = value.clone();
index.index_or_insert(&mut value_owned);
});
match result {
Ok(_) => println!("\n❌ Should have panicked"),
Err(_) => println!("\n✅ Correctly panicked for null value"),
}
// Example: Indexing into a mapping using &str
let mut mapping = serde_yml::Mapping::new();
mapping.insert(
Value::String("key".into()),
Value::String("value".into()),
);
let value = Value::Mapping(mapping);
let index = "key";
match index.index_into(&value) {
Some(value) => {
println!("\n✅ Indexed into mapping with &str: {:?}", value)
}
None => println!("\n❌ Key not found"),
}
// Example: Mutably indexing into a mapping using &str
let mut mapping = serde_yml::Mapping::new();
mapping.insert(
Value::String("key".into()),
Value::String("value".into()),
);
let mut value = Value::Mapping(mapping);
let index = "key";
if let Some(value) = index.index_into_mut(&mut value) {
*value = Value::String("new_value".into());
println!(
"\n✅ Mutably indexed into mapping with &str: {:?}",
value
);
}
// Example: Using index_or_insert with a mapping using &str
let mut mapping = serde_yml::Mapping::new();
mapping.insert(
Value::String("key".into()),
Value::String("value".into()),
);
let mut value = Value::Mapping(mapping);
let index = "new_key";
index
.index_or_insert(&mut value)
.clone_from(&Value::String("new_value".into()));
println!(
"\n✅ Used index_or_insert with mapping and &str: {:?}",
value
);
// Example: Indexing into a nested mapping
let mut nested_mapping = serde_yml::Mapping::new();
nested_mapping.insert(
Value::String("inner_key".into()),
Value::String("inner_value".into()),
);
let mut outer_mapping = serde_yml::Mapping::new();
outer_mapping.insert(
Value::String("outer_key".into()),
Value::Mapping(nested_mapping),
);
let value = Value::Mapping(outer_mapping);
let index = Value::String("outer_key".into());
if let Some(inner_value) = index
.index_into(&value)
.and_then(|v| "inner_key".index_into(v))
{
println!("\n✅ Indexed into nested mapping: {:?}", inner_value);
} else {
println!("\n❌ Key not found in nested mapping");
}
}
serde_yml-0.0.12/examples/value/mod.rs 0000644 0000000 0000000 00000000575 10461020230 0015755 0 ustar 0000000 0000000 /// This module contains the `de` examples.
pub(crate) mod de_examples;
/// This module contains the `index` examples.
pub(crate) mod index_examples;
/// The main function that runs all the example modules.
pub(crate) fn main() {
// Run the example module `de_examples`.
de_examples::main();
// Run the example module `index_examples`.
index_examples::main();
}
serde_yml-0.0.12/examples/with/mod.rs 0000644 0000000 0000000 00000005271 10461020230 0015612 0 ustar 0000000 0000000 /// This module contains the `singleton_map` example.
pub(crate) mod singleton_map;
/// This module contains the `singleton_map_recursive`
pub(crate) mod singleton_map_recursive;
/// This module contains the `singleton_map_enum_variants` example.
pub(crate) mod singleton_map_enum_variants;
/// This module contains the `singleton_map_recursive_deep_nesting` example.
pub(crate) mod singleton_map_recursive_deep_nesting;
/// This module contains the `singleton_map_recursive_serialize_deserialize` example.
pub(crate) mod singleton_map_recursive_serialize_deserialize;
/// This module contains the `singleton_map_optional` example.
pub(crate) mod singleton_map_optional;
/// This module contains the `singleton_map_with` example.
pub(crate) mod singleton_map_with;
/// This module contains the `singleton_map_recursive_optional` example.
pub(crate) mod singleton_map_recursive_optional;
/// This module contains the `singleton_map_recursive_with` example.
pub(crate) mod singleton_map_recursive_with;
/// This module contains the `singleton_map_with_custom_serialize` example.
pub(crate) mod singleton_map_with_custom_serialize;
/// This module contains the `singleton_map_custom_serialize_deserialize` example.
pub(crate) mod singleton_map_custom_serialize_deserialize;
/// This module contains the `nested_singleton_map` example.
pub(crate) mod nested_singleton_map;
/// The main function that runs all the example modules.
pub(crate) fn main() {
// Run the example module `loader_anchors_and_aliases`.
singleton_map::main();
// Run the example module `singleton_map_recursive`.
singleton_map_recursive::main();
// Run the example module `singleton_map_enum_variants`.
singleton_map_enum_variants::main();
// Run the example module `singleton_map_recursive_deep_nesting`.
singleton_map_recursive_deep_nesting::main();
// Run the example module `singleton_map_recursive_serialize_deserialize`.
singleton_map_recursive_serialize_deserialize::main();
// Run the example module `singleton_map_optional`.
singleton_map_optional::main();
// Run the example module `singleton_map_with`.
singleton_map_with::main();
// Run the example module `singleton_map_recursive_optional`.
singleton_map_recursive_optional::main();
// Run the example module `singleton_map_recursive_with`.
singleton_map_recursive_with::main();
// Run the example module `singleton_map_with_custom_serialize`.
singleton_map_with_custom_serialize::main();
// Run the example module `singleton_map_custom_serialize_deserialize`.
singleton_map_custom_serialize_deserialize::main();
// Run the example module `nested_singleton_map`.
nested_singleton_map::main();
}
serde_yml-0.0.12/examples/with/nested_singleton_map.rs 0000644 0000000 0000000 00000007632 10461020230 0021237 0 ustar 0000000 0000000 //! Example for the `nested_singleton_map` function in the `with` module.
//!
//! This function is used to recursively serialize and deserialize nested enums
//! into a YAML map with a single key-value pair for each enum variant, where the
//! key is the enum variant name and the value is the inner value of the enum
//! variant.
//!
use serde::{Deserialize, Serialize};
use serde_yml::with::nested_singleton_map;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/with/nested_singleton_map.rs");
// Define the inner enum with different variants
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum InnerEnum {
Variant1,
Variant2(String),
Variant3 { field1: i32, field2: bool },
}
// Define the outer enum that contains the inner enum as a field
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum OuterEnum {
Variant1(InnerEnum),
Variant2 { inner: InnerEnum },
}
// Define a struct that contains the outer enum as a field
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct NestedEnumStruct {
#[serde(with = "nested_singleton_map")]
field: OuterEnum,
}
// Example 1: OuterEnum::Variant1(InnerEnum::Variant1)
let input1 = NestedEnumStruct {
field: OuterEnum::Variant1(InnerEnum::Variant1),
};
let yaml1 = serde_yml::to_string(&input1).unwrap();
println!("\n✅ Serialized YAML for Example 1:\n{}", yaml1);
let output1: NestedEnumStruct =
serde_yml::from_str(&yaml1).unwrap();
println!("\n✅ Deserialized YAML for Example 1:\n{:#?}", output1);
assert_eq!(input1, output1);
// Example 2: OuterEnum::Variant1(InnerEnum::Variant2("value".to_string()))
let input2 = NestedEnumStruct {
field: OuterEnum::Variant1(InnerEnum::Variant2(
"value".to_string(),
)),
};
let yaml2 = serde_yml::to_string(&input2).unwrap();
println!("\n✅ Serialized YAML for Example 2:\n{}", yaml2);
let output2: NestedEnumStruct =
serde_yml::from_str(&yaml2).unwrap();
println!("\n✅ Deserialized YAML for Example 2:\n{:#?}", output2);
assert_eq!(input2, output2);
// Example 3: OuterEnum::Variant2 { inner: InnerEnum::Variant3 { field1: 42, field2: true } }
let input3 = NestedEnumStruct {
field: OuterEnum::Variant2 {
inner: InnerEnum::Variant3 {
field1: 42,
field2: true,
},
},
};
let yaml3 = serde_yml::to_string(&input3).unwrap();
println!("\n✅ Serialized YAML for Example 3:\n{}", yaml3);
let output3: NestedEnumStruct =
serde_yml::from_str(&yaml3).unwrap();
println!("\n✅ Deserialized YAML for Example 3:\n{:#?}", output3);
assert_eq!(input3, output3);
// Example 4: OuterEnum::Variant1(InnerEnum::Variant3 { field1: 99, field2: false })
let input4 = NestedEnumStruct {
field: OuterEnum::Variant1(InnerEnum::Variant3 {
field1: 99,
field2: false,
}),
};
let yaml4 = serde_yml::to_string(&input4).unwrap();
println!("\n✅ Serialized YAML for Example 4:\n{}", yaml4);
let output4: NestedEnumStruct =
serde_yml::from_str(&yaml4).unwrap();
println!("\n✅ Deserialized YAML for Example 4:\n{:#?}", output4);
assert_eq!(input4, output4);
// Example 5: OuterEnum::Variant2 { inner: InnerEnum::Variant2("another value".to_string()) }
let input5 = NestedEnumStruct {
field: OuterEnum::Variant2 {
inner: InnerEnum::Variant2("another value".to_string()),
},
};
let yaml5 = serde_yml::to_string(&input5).unwrap();
println!("\n✅ Serialized YAML for Example 5:\n{}", yaml5);
let output5: NestedEnumStruct =
serde_yml::from_str(&yaml5).unwrap();
println!("\n✅ Deserialized YAML for Example 5:\n{:#?}", output5);
assert_eq!(input5, output5);
}
serde_yml-0.0.12/examples/with/singleton_map.rs 0000644 0000000 0000000 00000002410 10461020230 0017662 0 ustar 0000000 0000000 //!
//! Example for the `singleton_map` function in the `with` module.
//!
//! This function is used to serialize a struct field that contains an enum with
//! only one variant into a YAML map with a single key-value pair, where the key
//! is the enum variant name and the value is the inner value of the enum
//! variant.
//!
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/with/singleton_map.rs");
// Example 1: Using singleton_map for a single enum field
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum SingleVariantEnum {
Variant(String),
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct SingleVariantStruct {
#[serde(with = "singleton_map")]
field: SingleVariantEnum,
}
let input = SingleVariantStruct {
field: SingleVariantEnum::Variant("value".to_string()),
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: SingleVariantStruct =
serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_custom_serialize_deserialize.rs 0000644 0000000 0000000 00000005060 10461020230 0025707 0 ustar 0000000 0000000 //!
//! Example for using `singleton_map` within a struct that has custom `Serialize` and `Deserialize` implementations.
//!
//! This example demonstrates the usage of `singleton_map` within a struct that has custom serialization
//! and deserialization logic to serialize and deserialize an enum field.
//!
use serde::{
de::{self, Deserializer, IgnoredAny, MapAccess, Visitor},
ser::{SerializeMap, Serializer},
Deserialize, Serialize,
};
use std::fmt;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(PartialEq, Debug)]
struct MyStruct {
field: MyEnum,
}
impl Serialize for MyStruct {
fn serialize(&self, serializer: S) -> Result
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(1))?;
map.serialize_entry("field", &self.field)?;
map.end()
}
}
impl<'de> Deserialize<'de> for MyStruct {
fn deserialize(deserializer: D) -> Result
where
D: Deserializer<'de>,
{
struct MyStructVisitor;
impl<'de> Visitor<'de> for MyStructVisitor {
type Value = MyStruct;
fn expecting(
&self,
formatter: &mut fmt::Formatter,
) -> fmt::Result {
formatter.write_str("a MyStruct")
}
fn visit_map(
self,
mut map: V,
) -> Result
where
V: MapAccess<'de>,
{
let mut field = None;
while let Some(key) = map.next_key::()? {
if key == "field" {
field = Some(map.next_value()?);
} else {
map.next_value::()?;
}
}
let field = field
.ok_or_else(|| de::Error::missing_field("field"))?;
Ok(MyStruct { field })
}
}
deserializer.deserialize_map(MyStructVisitor)
}
}
pub(crate) fn main() {
println!("\n❯ Executing examples/with/singleton_map_custom_serialize_deserialize.rs");
let input = MyStruct {
field: MyEnum::Variant2 { field: 42 },
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: MyStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_enum_variants.rs 0000644 0000000 0000000 00000003022 10461020230 0022615 0 ustar 0000000 0000000 //!
//! Example for the `singleton_map_enum_variants` function in the `with` module.
//!
//! This function is used to serialize a struct field that contains an enum with
//! multiple variants into a YAML map with a single key-value pair, where the key
//! is the enum variant name and the value is the inner value of the enum
//! variant.
//!
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!(
"\n❯ Executing examples/with/singleton_map_enum_variants.rs"
);
// Define an enum with multiple variants
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MultiVariantEnum {
Unit,
Newtype(String),
Tuple(i32, bool),
Struct { field: f64 },
}
// Define a struct containing fields of the enum type
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct EnumStruct {
#[serde(with = "singleton_map")]
field1: MultiVariantEnum,
#[serde(with = "singleton_map")]
field2: MultiVariantEnum,
}
let input = EnumStruct {
field1: MultiVariantEnum::Unit,
field2: MultiVariantEnum::Struct {
field: std::f64::consts::PI,
},
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: EnumStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_optional.rs 0000644 0000000 0000000 00000002252 10461020230 0021573 0 ustar 0000000 0000000 //!
//! Example for using `singleton_map_optional` to serialize and deserialize an optional enum field.
//!
//! This example demonstrates the usage of `singleton_map_optional` to seamlessly serialize and
//! deserialize an `Option` field as a single YAML mapping entry with the key being the enum
//! variant name.
//!
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map_optional;
pub(crate) fn main() {
println!("\n❯ Executing examples/with/singleton_map_optional.rs");
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum OptionalEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct OptionalStruct {
#[serde(with = "singleton_map_optional")]
field: Option,
}
let input = OptionalStruct {
field: Some(OptionalEnum::Variant2 { field: 42 }),
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: OptionalStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_recursive.rs 0000644 0000000 0000000 00000003704 10461020230 0021760 0 ustar 0000000 0000000 //!
//! Example for the `singleton_map_recursive` function in the `with` module.
//!
//! This function is used to serialize a struct field that contains an enum with
//! only one variant into a YAML map with a single key-value pair, where the key
//! is the enum variant name and the value is the inner value of the enum
//! variant.
//!
// Import necessary crates.
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map_recursive;
// Define the main function.
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/with/singleton_map_recursive.rs");
// Define an enum with a single variant.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum SingleVariantEnum {
Variant(String),
}
// Define a nested enum with two variants, one containing the single variant enum.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum NestedEnum {
Variant1(String),
Variant2(SingleVariantEnum),
}
// Define a struct containing the nested enum field serialized using the `singleton_map_recursive` function.
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct NestedStruct {
#[serde(with = "singleton_map_recursive")]
field: NestedEnum,
}
// Create an instance of the NestedStruct with a nested enum variant.
let input = NestedStruct {
field: NestedEnum::Variant2(SingleVariantEnum::Variant(
"nested".to_string(),
)),
};
// Serialize the input struct to YAML format and print it.
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
// Deserialize the YAML string back to a NestedStruct and print it.
let output: NestedStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
// Assert that the input and output are equal.
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_recursive_deep_nesting.rs 0000644 0000000 0000000 00000003522 10461020230 0024502 0 ustar 0000000 0000000 //!
//! Example for the `singleton_map_recursive_deep_nesting` function in the `with` module.
//!
//! This example demonstrates the use of the
//! `singleton_map_recursive_deep_nesting` function to serialize and deserialize
//! a struct field that contains an enum with deeply nested variants into a YAML
//! map with a single key-value pair, where the key is the enum variant name and
//! the value is the inner value of the enum variant.
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map_recursive;
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/with/singleton_map_recursive_deep_nesting.rs");
// Define an enum with deeply nested enums
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum NestedEnum {
Variant1(String),
Variant2(InnerEnum),
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum InnerEnum {
Inner1(i32),
Inner2(bool, DeepEnum),
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum DeepEnum {
Deep1(char),
Deep2 { field: String },
}
// Define a struct containing a field of the deeply nested enum type
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct NestedStruct {
#[serde(with = "singleton_map_recursive")]
field: NestedEnum,
}
let input = NestedStruct {
field: NestedEnum::Variant2(InnerEnum::Inner2(
true,
DeepEnum::Deep2 {
field: "nested".to_string(),
},
)),
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: NestedStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_recursive_optional.rs 0000644 0000000 0000000 00000002647 10461020230 0023672 0 ustar 0000000 0000000 //!
//! Example for using `singleton_map_recursive` to serialize and deserialize a nested enum structure.
//!
//! This example demonstrates the usage of `singleton_map_recursive` to seamlessly serialize and
//! deserialize a nested enum structure where one of the enum variants contains an optional inner
//! enum. The nested enums are serialized and deserialized as single YAML mapping entries with the
//! keys being the enum variant names.
//!
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map_recursive;
pub(crate) fn main() {
println!("\n❯ Executing examples/with/singleton_map_recursive_optional.rs");
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum NestedEnum {
Variant1(String),
Variant2(Option),
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum InnerEnum {
Inner1(i32),
Inner2(i32),
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct NestedStruct {
#[serde(with = "singleton_map_recursive")]
field: NestedEnum,
}
let input = NestedStruct {
field: NestedEnum::Variant2(Some(InnerEnum::Inner2(42))),
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: NestedStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_recursive_serialize_deserialize.rs 0000644 0000000 0000000 00000002507 10461020230 0026407 0 ustar 0000000 0000000 //!
//! Example for the `serialize` and `deserialize` functions in the `singleton_map_recursive` module.
//!
//! This example demonstrates the usage of the `serialize` and `deserialize` functions
//! from the `singleton_map_recursive` module to serialize and deserialize a
//! struct field that contains an enum with multiple variants into a YAML map
//! with a single key-value pair, where the key is the enum variant name and the
//! value is the inner value of the enum variant.
//!
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map_recursive;
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyStruct {
#[serde(with = "singleton_map_recursive")]
field: MyEnum,
}
pub(crate) fn main() {
// Print a message to indicate the file being executed.
println!("\n❯ Executing examples/with/singleton_map_recursive_serialize_deserialize.rs");
let input = MyStruct {
field: MyEnum::Variant2 { field: 42 },
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: MyStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_recursive_with.rs 0000644 0000000 0000000 00000002171 10461020230 0023010 0 ustar 0000000 0000000 //!
//! Example for using `singleton_map_recursive` to serialize and deserialize an enum field.
//!
//! This example demonstrates the usage of `singleton_map_recursive` to seamlessly serialize and
//! deserialize an enum field as a single YAML mapping entry with the key being the enum variant name.
//!
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map_recursive;
pub(crate) fn main() {
println!(
"\n❯ Executing examples/with/singleton_map_recursive_with.rs"
);
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyStruct {
#[serde(with = "singleton_map_recursive")]
field: MyEnum,
}
let input = MyStruct {
field: MyEnum::Variant2 { field: 42 },
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: MyStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_with.rs 0000644 0000000 0000000 00000002362 10461020230 0020723 0 ustar 0000000 0000000 //!
//! Example for using `singleton_map_with` to serialize and deserialize an enum field.
//!
//! This example demonstrates the usage of `singleton_map_with` to seamlessly serialize and
//! deserialize an enum field as a single YAML mapping entry with the key being the enum variant name.
//! The `singleton_map_with` attribute allows for additional customization of the serialization
//! and deserialization process through the use of helper functions.
//!
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map_with;
pub(crate) fn main() {
println!("\n❯ Executing examples/with/singleton_map_with.rs");
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyStruct {
#[serde(with = "singleton_map_with")]
field: MyEnum,
}
let input = MyStruct {
field: MyEnum::Variant2 { field: 42 },
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: MyStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/examples/with/singleton_map_with_custom_serialize.rs 0000644 0000000 0000000 00000002560 10461020230 0024364 0 ustar 0000000 0000000 //!
//! Example for using `singleton_map_with` in combination with a custom `serialize_with` attribute.
//!
//! This example demonstrates the usage of `singleton_map_with` in combination with a custom
//! serialization function to serialize and deserialize an enum field within a struct.
//!
use serde::{Deserialize, Serialize};
use serde_yml::with::singleton_map_with;
fn custom_serialize(
value: &T,
serializer: S,
) -> Result
where
T: Serialize,
S: serde::Serializer,
{
// Custom serialization logic
singleton_map_with::serialize(value, serializer)
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum MyEnum {
Variant1(String),
Variant2 { field: i32 },
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct MyStruct {
#[serde(
serialize_with = "custom_serialize",
deserialize_with = "singleton_map_with::deserialize"
)]
field: MyEnum,
}
pub(crate) fn main() {
println!("\n❯ Executing examples/with/singleton_map_with_custom_serialize.rs");
let input = MyStruct {
field: MyEnum::Variant2 { field: 42 },
};
let yaml = serde_yml::to_string(&input).unwrap();
println!("\n✅ Serialized YAML:\n{}", yaml);
let output: MyStruct = serde_yml::from_str(&yaml).unwrap();
println!("\n✅ Deserialized YAML:\n{:#?}", output);
assert_eq!(input, output);
}
serde_yml-0.0.12/src/de.rs 0000644 0000000 0000000 00000214137 10461020230 0013424 0 ustar 0000000 0000000 use crate::{
libyml::{
error::Mark,
parser::{MappingStart, Scalar, ScalarStyle, SequenceStart},
tag::Tag,
},
loader::{Document, Loader},
modules::error::{self, Error, ErrorImpl},
modules::path::Path,
};
use serde::de::{
self, value::StrDeserializer, Deserialize, DeserializeOwned,
DeserializeSeed, Expected, IgnoredAny, Unexpected, Visitor,
};
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::{fmt, io, mem, num::ParseIntError, str, sync::Arc};
type Result = std::result::Result;
/// A structure that deserializes YAML into Rust values.
///
/// # Examples
///
/// Deserializing a single document:
///
/// ```
/// use serde::Deserialize;
/// use serde_yml::Value;
///
/// fn main() -> serde_yml::Result<()> {
/// let input = "k: 107\n";
/// let de = serde_yml::Deserializer::from_str(input);
/// let value = Value::deserialize(de)?;
/// println!("{:?}", value);
/// Ok(())
/// }
/// ```
///
/// Deserializing multi-doc YAML:
///
/// ```
/// use serde::Deserialize;
/// use serde_yml::Value;
///
/// fn main() -> serde_yml::Result<()> {
/// let input = "---\nk: 107\n...\n---\nj: 106\n";
///
/// for document in serde_yml::Deserializer::from_str(input) {
/// let value = Value::deserialize(document)?;
/// println!("{:?}", value);
/// }
///
/// Ok(())
/// }
/// ```
#[derive(Debug)]
pub struct Deserializer<'de> {
/// Represents the progress of parsing a YAML document.
pub progress: Progress<'de>,
}
/// Represents the progress of parsing a YAML document.
pub enum Progress<'de> {
/// Indicates that the YAML input is a string slice.
///
/// The `&'de str` represents a borrowed string slice with a lifetime `'de`.
Str(&'de str),
/// Indicates that the YAML input is a byte slice.
///
/// The `&'de [u8]` represents a borrowed byte slice with a lifetime `'de`.
Slice(&'de [u8]),
/// Indicates that the YAML input is provided through a `Read` trait object.
///
/// The `Box` represents a boxed trait object that implements the `Read` trait
/// and has a lifetime `'de`. This allows for reading the YAML input from various sources,
/// such as files, network streams, or any other type that implements `Read`.
Read(Box),
/// Indicates that the YAML input is provided through an iterator of `Loader` instances.
///
/// The `Loader<'de>` represents a YAML loader that iterates over the YAML documents.
/// The `'de` lifetime indicates the lifetime of the borrowed data within the loader.
Iterable(Loader<'de>),
/// Indicates that the YAML input is a single `Document` instance.
///
/// The `Document<'de>` represents a parsed YAML document.
/// The `'de` lifetime indicates the lifetime of the borrowed data within the document.
Document(Document<'de>),
/// Indicates that an error occurred during parsing.
///
/// The `Arc` represents a reference-counted pointer to the error implementation.
/// It allows for sharing the error across multiple owners without duplication.
Fail(Arc),
}
impl Debug for Progress<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Progress::Str(s) => write!(f, "Progress::Str({:?})", s),
Progress::Slice(slice) => {
write!(f, "Progress::Slice({:?})", slice)
}
Progress::Read(_) => {
write!(f, "Progress::Read(Box)")
}
Progress::Iterable(loader) => {
write!(f, "Progress::Iterable({:?})", loader)
}
Progress::Document(doc) => {
write!(f, "Progress::Document({:?})", doc)
}
Progress::Fail(err) => {
write!(f, "Progress::Fail({:?})", err)
}
}
}
}
impl<'de> Deserializer<'de> {
/// Deserializes an instance of type `T` from a string of YAML text.
///
/// This function takes a string slice containing YAML data and attempts to parse and
/// deserialize it into an instance of the type `T`. The type must implement the `Deserialize`
/// trait from Serde. The function returns a result, which is either the deserialized
/// type `T` or an error if the deserialization process fails.
///
/// # Errors
///
/// This function returns an error if the YAML text does not correctly represent the
/// expected type `T`. Errors can arise from incorrect YAML syntax, type mismatches,
/// missing required fields, and other deserialization issues.
///
/// # Examples
///
/// ```
/// use serde_yml::from_str;
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize)]
/// struct Config {
/// name: String,
/// age: u32,
/// }
///
/// let yaml_data = r#"
/// name: John Doe
/// age: 30
/// "#;
/// let config: Config = from_str(yaml_data).unwrap();
/// println!("{:?}", config); // Config { name: "John Doe", age: 30 }
/// ```
#[allow(clippy::should_implement_trait)]
pub fn from_str(s: &'de str) -> Self {
let progress = Progress::Str(s);
Deserializer { progress }
}
/// Deserializes an instance of type `T` from bytes of YAML text.
///
/// Similar to `from_str`, but instead of a string slice, it operates on a byte slice. This
/// is useful when working with binary data or data read from non-text sources.
///
/// # Errors
///
/// Returns an error if the byte slice does not represent a valid YAML sequence or if it
/// cannot be deserialized into type `T` due to type mismatches, missing fields, etc.
///
/// # Examples
///
/// ```
/// use serde_yml::from_slice;
/// use serde::Deserialize;
///
/// #[derive(Debug, Deserialize)]
/// struct Item {
/// name: String,
/// quantity: u32,
/// }
///
/// let bytes = b"name: Widget\nquantity: 100";
/// let item: Item = from_slice(bytes).unwrap();
/// println!("{:?}", item); // Item { name: "Widget", quantity: 100 }
///
pub fn from_slice(v: &'de [u8]) -> Self {
let progress = Progress::Slice(v);
Deserializer { progress }
}
/// Deserializes an instance of type `T` from an IO stream of YAML.
///
/// This function is useful when you need to deserialize data directly from a stream, such as
/// reading from a file or over the network. It accepts any type that implements the `io::Read`
/// trait. As with `from_str`, the target type `T` must implement the `Deserialize` trait.
///
/// # Errors
///
/// Deserialization might fail due to IO errors (e.g., if the stream is not readable), YAML syntax
/// errors, or if the data does not fit the expected structure of type `T`. In such cases, the
/// function returns an error.
///
/// # Examples
///
/// ```
/// use serde_yml::from_reader;
/// use serde::Deserialize;
/// use std::io::Cursor;
///
/// #[derive(Debug, Deserialize)]
/// struct Config {
/// name: String,
/// age: u32,
/// }
///
/// // Simulate file reading with a cursor over a byte slice.
/// let data = b"name: Jane Doe\nage: 25";
/// let cursor = Cursor::new(data);
///
/// let config: Config = from_reader(cursor).unwrap();
/// println!("{:?}", config); // Config { name: "Jane Doe", age: 25 }
/// ```
///
pub fn from_reader(rdr: R) -> Self
where
R: io::Read + 'de,
{
let progress = Progress::Read(Box::new(rdr));
Deserializer { progress }
}
fn de(
self,
f: impl for<'document> FnOnce(
&mut DeserializerFromEvents<'de, 'document>,
) -> Result,
) -> Result {
let mut pos = 0;
let mut jumpcount = 0;
match self.progress {
Progress::Iterable(_) => {
return Err(error::new(ErrorImpl::MoreThanOneDocument))
}
Progress::Document(document) => {
let t = f(&mut DeserializerFromEvents {
document: &document,
pos: &mut pos,
jumpcount: &mut jumpcount,
path: Path::Root,
remaining_depth: 128,
current_enum: None,
})?;
if let Some(parse_error) = document.error {
return Err(error::shared(parse_error));
}
return Ok(t);
}
_ => {}
}
let mut loader = Loader::new(self.progress)?;
let document = match loader.next_document() {
Some(document) => document,
None => return Err(error::new(ErrorImpl::EndOfStream)),
};
let t = f(&mut DeserializerFromEvents {
document: &document,
pos: &mut pos,
jumpcount: &mut jumpcount,
path: Path::Root,
remaining_depth: 128,
current_enum: None,
})?;
if let Some(parse_error) = document.error {
return Err(error::shared(parse_error));
}
if loader.next_document().is_none() {
Ok(t)
} else {
Err(error::new(ErrorImpl::MoreThanOneDocument))
}
}
}
impl Iterator for Deserializer<'_> {
type Item = Self;
fn next(&mut self) -> Option {
match &mut self.progress {
Progress::Iterable(loader) => {
let document = loader.next_document()?;
return Some(Deserializer {
progress: Progress::Document(document),
});
}
Progress::Document(_) => return None,
Progress::Fail(err) => {
return Some(Deserializer {
progress: Progress::Fail(Arc::clone(err)),
});
}
_ => {}
}
let dummy = Progress::Str("");
let input = mem::replace(&mut self.progress, dummy);
match Loader::new(input) {
Ok(loader) => {
self.progress = Progress::Iterable(loader);
self.next()
}
Err(err) => {
let fail = err.shared();
self.progress = Progress::Fail(Arc::clone(&fail));
Some(Deserializer {
progress: Progress::Fail(fail),
})
}
}
}
}
impl<'de> de::Deserializer<'de> for Deserializer<'de> {
type Error = Error;
fn deserialize_any(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_any(visitor))
}
fn deserialize_bool(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_bool(visitor))
}
fn deserialize_i8(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_i8(visitor))
}
fn deserialize_i16(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_i16(visitor))
}
fn deserialize_i32(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_i32(visitor))
}
fn deserialize_i64(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_i64(visitor))
}
fn deserialize_i128(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_i128(visitor))
}
fn deserialize_u8(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_u8(visitor))
}
fn deserialize_u16(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_u16(visitor))
}
fn deserialize_u32(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_u32(visitor))
}
fn deserialize_u64(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_u64(visitor))
}
fn deserialize_u128(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_u128(visitor))
}
fn deserialize_f32(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_f32(visitor))
}
fn deserialize_f64(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_f64(visitor))
}
fn deserialize_char(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_char(visitor))
}
fn deserialize_str(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_str(visitor))
}
fn deserialize_string(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_string(visitor))
}
fn deserialize_bytes(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_bytes(visitor))
}
fn deserialize_byte_buf(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_byte_buf(visitor))
}
fn deserialize_option(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_option(visitor))
}
fn deserialize_unit(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_unit(visitor))
}
fn deserialize_unit_struct(
self,
name: &'static str,
visitor: V,
) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_unit_struct(name, visitor))
}
fn deserialize_newtype_struct(
self,
name: &'static str,
visitor: V,
) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_newtype_struct(name, visitor))
}
fn deserialize_seq(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_seq(visitor))
}
fn deserialize_tuple(
self,
len: usize,
visitor: V,
) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_tuple(len, visitor))
}
fn deserialize_tuple_struct(
self,
name: &'static str,
len: usize,
visitor: V,
) -> Result
where
V: Visitor<'de>,
{
self.de(|state| {
state.deserialize_tuple_struct(name, len, visitor)
})
}
fn deserialize_map(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_map(visitor))
}
fn deserialize_struct(
self,
name: &'static str,
fields: &'static [&'static str],
visitor: V,
) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_struct(name, fields, visitor))
}
fn deserialize_enum(
self,
name: &'static str,
variants: &'static [&'static str],
visitor: V,
) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_enum(name, variants, visitor))
}
fn deserialize_identifier(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_identifier(visitor))
}
fn deserialize_ignored_any(self, visitor: V) -> Result
where
V: Visitor<'de>,
{
self.de(|state| state.deserialize_ignored_any(visitor))
}
}
/// Represents the different events that can occur during YAML parsing.
#[derive(Debug)]
pub enum Event<'de> {
/// Represents an alias event, which refers to a previously defined anchor.
/// The `usize` value represents the index of the aliased event.
Alias(usize),
/// Represents a scalar event, which contains a scalar value.
/// The `Scalar` type holds the scalar value and its associated properties.
Scalar(Scalar<'de>),
/// Represents the start of a sequence event.
/// The `SequenceStart` type holds the properties of the sequence, such as its anchor and tag.
SequenceStart(SequenceStart),
/// Represents the end of a sequence event.
SequenceEnd,
/// Represents the start of a mapping event.
/// The `MappingStart` type holds the properties of the mapping, such as its anchor and tag.
MappingStart(MappingStart),
/// Represents the end of a mapping event.
MappingEnd,
/// Represents a void event, which is an empty event.
/// This event is used when there are no other events to be parsed.
Void,
}
struct DeserializerFromEvents<'de, 'document> {
document: &'document Document<'de>,
pos: &'document mut usize,
jumpcount: &'document mut usize,
path: Path<'document>,
remaining_depth: u8,
current_enum: Option>,
}
#[derive(Copy, Clone)]
/// Represents the current state of an enum during deserialization.
#[derive(Debug)]
pub struct CurrentEnum<'document> {
/// The name of the enum variant.
pub name: Option<&'static str>,
/// The tag of the enum variant.
pub tag: &'document str,
}
impl<'de, 'document> DeserializerFromEvents<'de, 'document> {
fn peek_event(&self) -> Result<&'document Event<'de>> {
self.peek_event_mark().map(|(event, _mark)| event)
}
fn peek_event_mark(&self) -> Result<(&'document Event<'de>, Mark)> {
match self.document.events.get(*self.pos) {
Some((event, mark)) => Ok((event, *mark)),
None => Err(match &self.document.error {
Some(parse_error) => {
error::shared(Arc::clone(parse_error))
}
None => error::new(ErrorImpl::EndOfStream),
}),
}
}
fn next_event(&mut self) -> Result<&'document Event<'de>> {
self.next_event_mark().map(|(event, _mark)| event)
}
fn next_event_mark(
&mut self,
) -> Result<(&'document Event<'de>, Mark)> {
self.peek_event_mark().map(|(event, mark)| {
*self.pos += 1;
self.current_enum = None;
(event, mark)
})
}
fn jump<'anchor>(
&'anchor mut self,
pos: &'anchor mut usize,
) -> Result> {
*self.jumpcount += 1;
if *self.jumpcount > self.document.events.len() * 100 {
return Err(error::new(ErrorImpl::RepetitionLimitExceeded));
}
match self.document.anchor_event_map.get(pos) {
Some(found) => {
*pos = *found;
Ok(DeserializerFromEvents {
document: self.document,
pos,
jumpcount: self.jumpcount,
path: Path::Alias { parent: &self.path },
remaining_depth: self.remaining_depth,
current_enum: None,
})
}
None => panic!("unresolved alias: {}", *pos),
}
}
fn ignore_any(&mut self) -> Result<()> {
enum Nest {
Sequence,
Mapping,
}
let mut stack = Vec::new();
#[allow(clippy::never_loop)]
loop {
match self.next_event()? {
Event::Alias(_) | Event::Scalar(_) | Event::Void => {}
Event::SequenceStart(_) => {
stack.push(Nest::Sequence);
}
Event::MappingStart(_) => {
stack.push(Nest::Mapping);
}
Event::SequenceEnd => match stack.pop() {
Some(Nest::Sequence) => {}
None | Some(Nest::Mapping) => {
panic!("unexpected end of sequence");
}
},
Event::MappingEnd => match stack.pop() {
Some(Nest::Mapping) => {}
None | Some(Nest::Sequence) => {
panic!("unexpected end of mapping");
}
},
}
if stack.is_empty() {
return Ok(());
}
}
}
fn visit_sequence(
&mut self,
visitor: V,
mark: Mark,
) -> Result
where
V: Visitor<'de>,
{
let (value, len) = self.recursion_check(mark, |de| {
let mut seq = SeqAccess {
empty: false,
de,
len: 0,
};
let value = visitor.visit_seq(&mut seq)?;
Ok((value, seq.len))
})?;
self.end_sequence(len)?;
Ok(value)
}
fn visit_mapping(
&mut self,
visitor: V,
mark: Mark,
) -> Result
where
V: Visitor<'de>,
{
let (value, len) = self.recursion_check(mark, |de| {
let mut map = MapAccess {
empty: false,
de,
len: 0,
key: None,
};
let value = visitor.visit_map(&mut map)?;
Ok((value, map.len))
})?;
self.end_mapping(len)?;
Ok(value)
}
fn end_sequence(&mut self, len: usize) -> Result<()> {
let total = {
let mut seq = SeqAccess {
empty: false,
de: self,
len,
};
while de::SeqAccess::next_element::(&mut seq)?
.is_some()
{}
seq.len
};
match self.next_event()? {
Event::SequenceEnd | Event::Void => {}
_ => panic!("expected a SequenceEnd event"),
}
if total == len {
Ok(())
} else {
struct ExpectedSeq(usize);
impl Expected for ExpectedSeq {
fn fmt(
&self,
formatter: &mut Formatter<'_>,
) -> fmt::Result {
if self.0 == 1 {
write!(formatter, "sequence of 1 element")
} else {
write!(
formatter,
"sequence of {} elements",
self.0
)
}
}
}
Err(de::Error::invalid_length(total, &ExpectedSeq(len)))
}
}
fn end_mapping(&mut self, len: usize) -> Result<()> {
let total = {
let mut map = MapAccess {
empty: false,
de: self,
len,
key: None,
};
while de::MapAccess::next_entry::(
&mut map,
)?
.is_some()
{}
map.len
};
match self.next_event()? {
Event::MappingEnd | Event::Void => {}
_ => panic!("expected a MappingEnd event"),
}
if total == len {
Ok(())
} else {
struct ExpectedMap(usize);
impl Expected for ExpectedMap {
fn fmt(
&self,
formatter: &mut Formatter<'_>,
) -> fmt::Result {
if self.0 == 1 {
write!(formatter, "map containing 1 entry")
} else {
write!(
formatter,
"map containing {} entries",
self.0
)
}
}
}
Err(de::Error::invalid_length(total, &ExpectedMap(len)))
}
}
fn recursion_check Result, T>(
&mut self,
mark: Mark,
f: F,
) -> Result {
let previous_depth = self.remaining_depth;
self.remaining_depth = match previous_depth.checked_sub(1) {
Some(depth) => depth,
None => {
return Err(error::new(
ErrorImpl::RecursionLimitExceeded(mark),
))
}
};
let result = f(self);
self.remaining_depth = previous_depth;
result
}
}
struct SeqAccess<'de, 'document, 'seq> {
empty: bool,
de: &'seq mut DeserializerFromEvents<'de, 'document>,
len: usize,
}
impl<'de> de::SeqAccess<'de> for SeqAccess<'de, '_, '_> {
type Error = Error;
fn next_element_seed(
&mut self,
seed: T,
) -> Result