Skip to content

Commit

Permalink
fix(serde_yml): 🎨 fix formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Jan 2, 2025
1 parent 1b37b92 commit 5875cf3
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 92 deletions.
15 changes: 9 additions & 6 deletions src/libyml/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Error {
/// This function is unsafe because it dereferences raw pointers and assumes
/// the validity of the `YamlParserT` pointer.
pub unsafe fn parse_error(parser: *const sys::YamlParserT) -> Self {
Error {
Error {
kind: unsafe { (*parser).error },

problem: NonNull::new((*parser).problem as *mut _).map_or_else(
Expand All @@ -76,16 +76,17 @@ impl Error {
sys: unsafe { (*parser).context_mark },
},
}
}

}

/// Constructs an `Error` from a `YamlEmitterT` pointer.
///
/// # Safety
///
/// This function is unsafe because it dereferences raw pointers and assumes
/// the validity of the `YamlEmitterT` pointer.
pub unsafe fn emit_error(emitter: *const sys::YamlEmitterT) -> Self {
pub unsafe fn emit_error(
emitter: *const sys::YamlEmitterT,
) -> Self {
Error {
kind: (*emitter).error,
problem: match NonNull::new((*emitter).problem as *mut _) {

Check warning on line 92 in src/libyml/error.rs

View check run for this annotation

Codecov / codecov/patch

src/libyml/error.rs#L91-L92

Added lines #L91 - L92 were not covered by tests
Expand All @@ -97,11 +98,13 @@ impl Error {
},
problem_offset: 0,
problem_mark: Mark {
sys: MaybeUninit::<sys::YamlMarkT>::zeroed().assume_init(),
sys: MaybeUninit::<sys::YamlMarkT>::zeroed()
.assume_init(),
},
context: None,
context_mark: Mark {
sys: MaybeUninit::<sys::YamlMarkT>::zeroed().assume_init(),
sys: MaybeUninit::<sys::YamlMarkT>::zeroed()
.assume_init(),
},
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/libyml/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ impl<'input> Parser<'input> {
let pin = unsafe {
let parser = addr_of_mut!((*owned.ptr).sys);
assert!(
!sys::yaml_parser_initialize(parser).fail,
"Failed to initialize YAML parser: {}",
Error::parse_error(parser)
);
!sys::yaml_parser_initialize(parser).fail,
"Failed to initialize YAML parser: {}",
Error::parse_error(parser)

Check warning on line 149 in src/libyml/parser.rs

View check run for this annotation

Codecov / codecov/patch

src/libyml/parser.rs#L147-L149

Added lines #L147 - L149 were not covered by tests
);
sys::yaml_parser_set_encoding(
parser,
sys::YamlUtf8Encoding,
Expand Down
173 changes: 91 additions & 82 deletions src/with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,10 +940,15 @@ pub mod singleton_map {
where
V: DeserializeSeed<'de>,
{
(self.delegate.next_key_seed(seed)?).map_or_else(|| Err(de::Error::invalid_value(
Unexpected::Map,
&"map with a single key",
)), |value| Ok((value, self)))
(self.delegate.next_key_seed(seed)?).map_or_else(
|| {
Err(de::Error::invalid_value(
Unexpected::Map,
&"map with a single key",

Check warning on line 947 in src/with.rs

View check run for this annotation

Codecov / codecov/patch

src/with.rs#L944-L947

Added lines #L944 - L947 were not covered by tests
))
},
|value| Ok((value, self)),
)
}
}

Expand Down Expand Up @@ -2733,12 +2738,17 @@ pub mod singleton_map_recursive {
where
V: DeserializeSeed<'de>,
{
(self.delegate.next_key_seed(seed)?).map_or_else(|| Err(de::Error::invalid_value(
Unexpected::Map,
&"map with a single key",
)), |value| Ok((value, self)))
(self.delegate.next_key_seed(seed)?).map_or_else(
|| {
Err(de::Error::invalid_value(
Unexpected::Map,
&"map with a single key",

Check warning on line 2745 in src/with.rs

View check run for this annotation

Codecov / codecov/patch

src/with.rs#L2742-L2745

Added lines #L2742 - L2745 were not covered by tests
))
},
|value| Ok((value, self)),
)
}
}
}

impl<'de, D> VariantAccess<'de> for SingletonMapRecursiveAsEnum<D>
where
Expand Down Expand Up @@ -2920,78 +2930,77 @@ pub mod nested_singleton_map {
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Serializes a value using the nested singleton map representation.
///
/// This function applies the singleton map representation recursively to all nested enums
/// within the value being serialized.
///
/// # Arguments
///
/// * `value` - A reference to the value to be serialized.
/// * `serializer` - The serializer to use for serializing the value.
///
/// # Returns
///
/// A result containing the serialization output or an error if serialization fails.
///
/// # Errors
///
/// This function will return an error if:
/// - The provided value cannot be converted to the expected format.
/// - There is an underlying I/O error or a serialization constraint is violated.
///
/// # Examples
///
/// ```
/// use serde::{Serialize, Deserialize};
/// use serde_yml::with::nested_singleton_map;
/// use serde_yml::Serializer;
/// use std::io::Write;
///
/// // Define enum InnerEnum and OuterEnum for serialization
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum InnerEnum {
/// Variant1,
/// Variant2(String),
/// }
///
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum OuterEnum {
/// Variant1(InnerEnum),
/// Variant2 { inner: InnerEnum },
/// }
///
/// // Test serialization for OuterEnum::Variant1(InnerEnum::Variant1)
/// let value = OuterEnum::Variant1(InnerEnum::Variant1);
/// let mut serializer = serde_yml::Serializer::new(Vec::new());
/// nested_singleton_map::serialize(&value, &mut serializer)
/// .unwrap();
/// let yaml = String::from_utf8(serializer.into_inner().unwrap())
/// .unwrap();
/// assert_eq!(yaml, "Variant1: Variant1\n");
///
/// // Test serialization for OuterEnum::Variant2 { inner: InnerEnum::Variant2("value".to_string()) }
/// let value = OuterEnum::Variant2 {
/// inner: InnerEnum::Variant2("value".to_string()),
/// };
/// let mut serializer = serde_yml::Serializer::new(Vec::new());
/// nested_singleton_map::serialize(&value, &mut serializer)
/// .unwrap();
/// let yaml = String::from_utf8(serializer.into_inner().unwrap())
/// .unwrap();
/// assert_eq!(yaml, "Variant2:\n inner:\n Variant2: value\n");
/// ```
///
pub fn serialize<T, S>(
value: &T,
serializer: S,
) -> Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer,
{
singleton_map_recursive::serialize(value, serializer)
}

///
/// This function applies the singleton map representation recursively to all nested enums
/// within the value being serialized.
///
/// # Arguments
///
/// * `value` - A reference to the value to be serialized.
/// * `serializer` - The serializer to use for serializing the value.
///
/// # Returns
///
/// A result containing the serialization output or an error if serialization fails.
///
/// # Errors
///
/// This function will return an error if:
/// - The provided value cannot be converted to the expected format.
/// - There is an underlying I/O error or a serialization constraint is violated.
///
/// # Examples
///
/// ```
/// use serde::{Serialize, Deserialize};
/// use serde_yml::with::nested_singleton_map;
/// use serde_yml::Serializer;
/// use std::io::Write;
///
/// // Define enum InnerEnum and OuterEnum for serialization
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum InnerEnum {
/// Variant1,
/// Variant2(String),
/// }
///
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum OuterEnum {
/// Variant1(InnerEnum),
/// Variant2 { inner: InnerEnum },
/// }
///
/// // Test serialization for OuterEnum::Variant1(InnerEnum::Variant1)
/// let value = OuterEnum::Variant1(InnerEnum::Variant1);
/// let mut serializer = serde_yml::Serializer::new(Vec::new());
/// nested_singleton_map::serialize(&value, &mut serializer)
/// .unwrap();
/// let yaml = String::from_utf8(serializer.into_inner().unwrap())
/// .unwrap();
/// assert_eq!(yaml, "Variant1: Variant1\n");
///
/// // Test serialization for OuterEnum::Variant2 { inner: InnerEnum::Variant2("value".to_string()) }
/// let value = OuterEnum::Variant2 {
/// inner: InnerEnum::Variant2("value".to_string()),
/// };
/// let mut serializer = serde_yml::Serializer::new(Vec::new());
/// nested_singleton_map::serialize(&value, &mut serializer)
/// .unwrap();
/// let yaml = String::from_utf8(serializer.into_inner().unwrap())
/// .unwrap();
/// assert_eq!(yaml, "Variant2:\n inner:\n Variant2: value\n");
/// ```
///
pub fn serialize<T, S>(
value: &T,
serializer: S,
) -> Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer,
{
singleton_map_recursive::serialize(value, serializer)
}

/// Deserializes a value using the nested singleton map representation.
///
Expand Down Expand Up @@ -3024,7 +3033,7 @@ where
/// Variant1,
/// Variant2(String),
/// }
///
///
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
/// enum OuterEnum {
/// Variant1(InnerEnum),
Expand Down

0 comments on commit 5875cf3

Please sign in to comment.