Skip to content

Commit 0415584

Browse files
committed
types: Guard all KDL/miette stuff with kdl feature
This keeps the provisioning internals free from the other crates for use in blsforme, etc. Signed-off-by: Ikey Doherty <[email protected]>
1 parent f7de05d commit 0415584

11 files changed

+107
-62
lines changed

crates/provisioning/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ miette = { workspace = true, features = ["fancy"] }
99
[dependencies]
1010
disks = { path = "../disks" }
1111
partitioning = { path = "../partitioning" }
12-
types = { path = "../types" }
12+
types = { path = "../types", features = ["kdl"] }
1313
kdl = { workspace = true, features = ["span"] }
1414
miette = { workspace = true }
1515
itertools = { workspace = true }

crates/types/Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
kdl.workspace = true
7+
kdl = { workspace = true, optional = true }
88
thiserror.workspace = true
9-
miette.workspace = true
9+
miette = { workspace = true, optional = true }
1010
gpt.workspace = true
1111
uuid.workspace = true
12+
13+
[features]
14+
kdl = ["dep:kdl", "dep:miette"]

crates/types/src/constraints.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// SPDX-License-Identifier: MPL-2.0
44

5+
#[cfg(feature = "kdl")]
56
use crate::{get_kdl_entry, kdl_value_to_storage_size};
67

78
/// Constraints for partition size, 1:1 mapping to SizeRequirements in
@@ -22,6 +23,7 @@ pub enum Constraints {
2223
Invalid,
2324
}
2425

26+
#[cfg(feature = "kdl")]
2527
impl Constraints {
2628
pub fn from_kdl_node(node: &kdl::KdlNode) -> Result<Self, crate::Error> {
2729
let range = node

crates/types/src/errors.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,72 @@
22
//
33
// SPDX-License-Identifier: MPL-2.0
44

5-
use std::{io, sync::Arc};
5+
use std::io;
66

7+
#[cfg(feature = "kdl")]
78
use miette::{Diagnostic, NamedSource, SourceSpan};
9+
#[cfg(feature = "kdl")]
10+
use std::sync::Arc;
811
use thiserror::Error;
912

13+
#[cfg(feature = "kdl")]
1014
use crate::KdlType;
1115

1216
/// Error type for the provisioning crate
13-
#[derive(Diagnostic, Debug, Error)]
17+
#[cfg_attr(feature = "kdl", derive(Diagnostic))]
18+
#[derive(Debug, Error)]
1419
pub enum Error {
1520
#[error(transparent)]
1621
IO(#[from] io::Error),
1722

23+
#[cfg(feature = "kdl")]
1824
#[diagnostic(transparent)]
1925
#[error(transparent)]
2026
Kdl(#[from] kdl::KdlError),
2127

28+
#[cfg(feature = "kdl")]
2229
#[error("unknown type")]
2330
UnknownType,
2431

2532
#[error("unknown variant")]
2633
UnknownVariant,
2734

35+
#[cfg(feature = "kdl")]
2836
#[diagnostic(transparent)]
2937
#[error(transparent)]
3038
InvalidArguments(#[from] InvalidArguments),
3139

40+
#[cfg(feature = "kdl")]
3241
#[diagnostic(transparent)]
3342
#[error(transparent)]
3443
InvalidType(#[from] InvalidType),
3544

45+
#[cfg(feature = "kdl")]
3646
#[diagnostic(transparent)]
3747
#[error(transparent)]
3848
UnsupportedNode(#[from] UnsupportedNode),
3949

50+
#[cfg(feature = "kdl")]
4051
#[diagnostic(transparent)]
4152
#[error(transparent)]
4253
MissingEntry(#[from] MissingEntry),
4354

55+
#[cfg(feature = "kdl")]
4456
#[error("missing node: {0}")]
4557
MissingNode(&'static str),
4658

59+
#[cfg(feature = "kdl")]
4760
#[diagnostic(transparent)]
4861
#[error(transparent)]
4962
MissingProperty(#[from] MissingProperty),
5063

64+
#[cfg(feature = "kdl")]
5165
#[diagnostic(transparent)]
5266
#[error(transparent)]
5367
UnsupportedValue(#[from] UnsupportedValue),
5468
}
5569

70+
#[cfg(feature = "kdl")]
5671
/// Merged error for parsing failures
5772
/// Returns a list of diagnostics for the user
5873
#[derive(Debug, Diagnostic, Error)]
@@ -65,6 +80,7 @@ pub struct ParseError {
6580
pub diagnostics: Vec<Error>,
6681
}
6782

83+
#[cfg(feature = "kdl")]
6884
/// Error for invalid types
6985
#[derive(Debug, Diagnostic, Error)]
7086
#[error("invalid type, expected {expected_type}")]
@@ -77,6 +93,7 @@ pub struct InvalidType {
7793
pub expected_type: KdlType,
7894
}
7995

96+
#[cfg(feature = "kdl")]
8097
/// Error for missing mandatory properties
8198
#[derive(Debug, Diagnostic, Error)]
8299
#[error("missing property: {id}")]
@@ -91,6 +108,7 @@ pub struct MissingProperty {
91108
pub advice: Option<String>,
92109
}
93110

111+
#[cfg(feature = "kdl")]
94112
/// Error for missing mandatory properties
95113
#[derive(Debug, Diagnostic, Error)]
96114
#[error("missing entry: {id}")]
@@ -105,6 +123,7 @@ pub struct MissingEntry {
105123
pub advice: Option<String>,
106124
}
107125

126+
#[cfg(feature = "kdl")]
108127
/// Error for unsupported node types
109128
#[derive(Debug, Diagnostic, Error)]
110129
#[error("unsupported node: {name}")]
@@ -116,6 +135,7 @@ pub struct UnsupportedNode {
116135
pub name: String,
117136
}
118137

138+
#[cfg(feature = "kdl")]
119139
/// Error for unsupported values
120140
#[derive(Debug, Diagnostic, Error)]
121141
#[error("unsupported value")]
@@ -128,6 +148,7 @@ pub struct UnsupportedValue {
128148
pub advice: Option<String>,
129149
}
130150

151+
#[cfg(feature = "kdl")]
131152
/// Error for invalid arguments
132153
#[derive(Debug, Diagnostic, Error)]
133154
#[error("invalid arguments")]
@@ -140,6 +161,7 @@ pub struct InvalidArguments {
140161
pub advice: Option<String>,
141162
}
142163

164+
#[cfg(feature = "kdl")]
143165
/// Error for missing types
144166
#[derive(Debug, Diagnostic, Error)]
145167
#[error("missing type")]

crates/types/src/filesystem.rs

+4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
use std::{fmt, str::FromStr};
77

8+
#[cfg(feature = "kdl")]
89
use crate::{get_kdl_entry, kdl_value_to_integer, kdl_value_to_string};
910

11+
#[cfg(feature = "kdl")]
1012
use super::FromKdlProperty;
1113

1214
/// The filesystem information for a partition
@@ -57,6 +59,7 @@ impl FromStr for StandardFilesystemType {
5759
}
5860
}
5961

62+
#[cfg(feature = "kdl")]
6063
impl FromKdlProperty<'_> for StandardFilesystemType {
6164
fn from_kdl_property(entry: &kdl::KdlEntry) -> Result<Self, crate::Error> {
6265
let value = kdl_value_to_string(entry)?;
@@ -68,6 +71,7 @@ impl FromKdlProperty<'_> for StandardFilesystemType {
6871
}
6972
}
7073

74+
#[cfg(feature = "kdl")]
7175
impl Filesystem {
7276
pub fn from_kdl_node(node: &kdl::KdlNode) -> Result<Self, crate::Error> {
7377
let mut fs_type = None;

crates/types/src/kdl_helpers.rs

+52-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,59 @@
22
//
33
// SPDX-License-Identifier: MPL-2.0
44

5-
use kdl::{KdlEntry, KdlNode, NodeKey};
5+
use std::fmt;
66

7-
use crate::{Error, FromKdlType, InvalidType, KdlType, MissingEntry, MissingProperty, StorageUnit};
7+
use crate::{Error, InvalidType, MissingEntry, MissingProperty, StorageUnit};
8+
use kdl::{KdlEntry, KdlNode, KdlValue, NodeKey};
9+
10+
/// The type of a KDL value
11+
#[derive(Debug)]
12+
pub enum KdlType {
13+
/// A boolean value
14+
Boolean,
15+
/// A string value
16+
String,
17+
/// A null value
18+
Null,
19+
/// An integer value
20+
Integer,
21+
}
22+
23+
impl KdlType {
24+
// Determine the kdl value type
25+
pub fn for_value(value: &KdlValue) -> Result<Self, Error> {
26+
if value.is_bool() {
27+
Ok(Self::Boolean)
28+
} else if value.is_string() {
29+
Ok(Self::String)
30+
} else if value.is_null() {
31+
Ok(Self::Null)
32+
} else if value.is_integer() {
33+
Ok(Self::Integer)
34+
} else {
35+
Err(Error::UnknownType)
36+
}
37+
}
38+
}
39+
40+
impl fmt::Display for KdlType {
41+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42+
match self {
43+
KdlType::Boolean => f.write_str("boolean"),
44+
KdlType::String => f.write_str("string"),
45+
KdlType::Null => f.write_str("null"),
46+
KdlType::Integer => f.write_str("int"),
47+
}
48+
}
49+
}
50+
51+
pub trait FromKdlProperty<'a>: Sized {
52+
fn from_kdl_property(entry: &'a KdlEntry) -> Result<Self, Error>;
53+
}
54+
55+
pub trait FromKdlType<'a>: Sized {
56+
fn from_kdl_type(id: &'a KdlEntry) -> Result<Self, Error>;
57+
}
858

959
// Get a property from a node
1060
pub fn get_kdl_property<'a>(node: &'a KdlNode, name: &'static str) -> Result<&'a KdlEntry, Error> {

crates/types/src/lib.rs

+2-53
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
//
44
// SPDX-License-Identifier: MPL-2.0
55

6-
use std::fmt;
7-
8-
use kdl::{KdlEntry, KdlValue};
9-
6+
#[cfg(feature = "kdl")]
107
mod kdl_helpers;
8+
#[cfg(feature = "kdl")]
119
pub use kdl_helpers::*;
1210
mod errors;
1311
pub use errors::*;
@@ -27,52 +25,3 @@ pub mod filesystem;
2725
pub use filesystem::*;
2826
mod partition_type;
2927
pub use partition_type::*;
30-
31-
/// The type of a KDL value
32-
#[derive(Debug)]
33-
pub enum KdlType {
34-
/// A boolean value
35-
Boolean,
36-
/// A string value
37-
String,
38-
/// A null value
39-
Null,
40-
/// An integer value
41-
Integer,
42-
}
43-
44-
impl KdlType {
45-
// Determine the kdl value type
46-
pub fn for_value(value: &KdlValue) -> Result<Self, Error> {
47-
if value.is_bool() {
48-
Ok(Self::Boolean)
49-
} else if value.is_string() {
50-
Ok(Self::String)
51-
} else if value.is_null() {
52-
Ok(Self::Null)
53-
} else if value.is_integer() {
54-
Ok(Self::Integer)
55-
} else {
56-
Err(Error::UnknownType)
57-
}
58-
}
59-
}
60-
61-
impl fmt::Display for KdlType {
62-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63-
match self {
64-
KdlType::Boolean => f.write_str("boolean"),
65-
KdlType::String => f.write_str("string"),
66-
KdlType::Null => f.write_str("null"),
67-
KdlType::Integer => f.write_str("int"),
68-
}
69-
}
70-
}
71-
72-
pub trait FromKdlProperty<'a>: Sized {
73-
fn from_kdl_property(entry: &'a KdlEntry) -> Result<Self, Error>;
74-
}
75-
76-
pub trait FromKdlType<'a>: Sized {
77-
fn from_kdl_type(id: &'a KdlEntry) -> Result<Self, Error>;
78-
}

crates/types/src/partition_role.rs

+3
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55

66
use std::{fmt, str::FromStr};
77

8+
#[cfg(feature = "kdl")]
89
use crate::kdl_value_to_string;
910

11+
#[cfg(feature = "kdl")]
1012
use super::FromKdlProperty;
1113

1214
/// The role assigned to a partition
@@ -68,6 +70,7 @@ impl FromStr for PartitionRole {
6870
}
6971
}
7072

73+
#[cfg(feature = "kdl")]
7174
impl FromKdlProperty<'_> for PartitionRole {
7275
fn from_kdl_property(entry: &kdl::KdlEntry) -> Result<Self, crate::Error> {
7376
let value = kdl_value_to_string(entry)?;

crates/types/src/partition_table.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
use std::{fmt, str::FromStr};
77

8-
use crate::kdl_value_to_string;
9-
8+
#[cfg(feature = "kdl")]
109
use super::FromKdlProperty;
10+
#[cfg(feature = "kdl")]
11+
use crate::kdl_value_to_string;
1112

1213
/// The type of partition table to create
1314
#[derive(Debug, PartialEq)]
@@ -41,6 +42,7 @@ impl FromStr for PartitionTableType {
4142
}
4243
}
4344

45+
#[cfg(feature = "kdl")]
4446
impl FromKdlProperty<'_> for PartitionTableType {
4547
fn from_kdl_property(entry: &kdl::KdlEntry) -> Result<Self, crate::Error> {
4648
let value = kdl_value_to_string(entry)?;

0 commit comments

Comments
 (0)