Skip to content

Commit

Permalink
refactor(serde_yml): 🚨 fixes on linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Jan 2, 2025
1 parent 8f1327f commit 3765f0f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/libyml/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub struct MappingStart {
}

/// Represents an anchor in a YAML document.
#[derive(Ord, PartialOrd, Eq, PartialEq)]
#[derive(Clone, Default, Eq, Ord, PartialEq, PartialOrd)]
pub struct Anchor(Box<[u8]>);

/// Represents the style of a scalar value in a YAML document.
Expand Down Expand Up @@ -282,8 +282,7 @@ unsafe fn convert_event<'input>(
})
}
sys::YamlMappingEndEvent => Event::MappingEnd,
sys::YamlNoEvent => unreachable!(),
_ => unreachable!(),
sys::YamlNoEvent | _ => unreachable!(),
}
}

Expand All @@ -303,8 +302,8 @@ unsafe fn optional_repr<'input>(
sys: &sys::YamlEventT,
input: &'input Cow<'input, [u8]>,
) -> Option<&'input [u8]> {
let start = sys.start_mark.index as usize;
let end = sys.end_mark.index as usize;
let start = usize::try_from(sys.start_mark.index).ok()?;
let end = usize::try_from(sys.end_mark.index).ok()?;
Some(&input[start..end])
}

Expand Down
22 changes: 15 additions & 7 deletions src/libyml/safe_cstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::{
};

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// A custom error type for CStr operations.
/// A custom error type for `CStr` operations.
///
/// This struct represents an error that occurs during CStr operations.
/// This struct represents an error that occurs during `CStr` operations.
///
/// # Implementations
///
Expand Down Expand Up @@ -73,7 +73,8 @@ impl<'a> CStr<'a> {
/// # Returns
///
/// A new `CStr` instance representing the input pointer.
pub fn from_ptr(ptr: NonNull<std::ffi::c_char>) -> Self {
#[must_use]
pub const fn from_ptr(ptr: NonNull<std::ffi::c_char>) -> Self {
CStr {
// Cast the input pointer to a `NonNull<u8>` pointer
ptr: ptr.cast(),
Expand All @@ -97,11 +98,18 @@ impl<'a> CStr<'a> {
end = unsafe { end.add(1) };
}

// Calculate the length of the C-style string, but only if the input is not empty
if end != start {
unsafe { end.offset_from(start) as usize }
} else {
// If the string is empty, return 0. Otherwise, compute the offset safely.
if end == start {
0
} else {
let offset = unsafe { end.offset_from(start) };
usize::try_from(offset).unwrap_or_else(|_| {
debug_assert!(
false,
"offset_from returned a negative value, which shouldn't happen in a forward scan"

Check warning on line 109 in src/libyml/safe_cstr.rs

View check run for this annotation

Codecov / codecov/patch

src/libyml/safe_cstr.rs#L107-L109

Added lines #L107 - L109 were not covered by tests
);
0

Check warning on line 111 in src/libyml/safe_cstr.rs

View check run for this annotation

Codecov / codecov/patch

src/libyml/safe_cstr.rs#L111

Added line #L111 was not covered by tests
})
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/modules/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ pub struct Location {

impl Location {
/// Returns the byte index where the error occurred.
#[must_use]
pub const fn index(&self) -> usize {

Check warning on line 36 in src/modules/error.rs

View check run for this annotation

Codecov / codecov/patch

src/modules/error.rs#L36

Added line #L36 was not covered by tests
self.index
}

/// Returns the line number where the error occurred.
#[must_use]
pub const fn line(&self) -> usize {

Check warning on line 42 in src/modules/error.rs

View check run for this annotation

Codecov / codecov/patch

src/modules/error.rs#L42

Added line #L42 was not covered by tests
self.line
}

/// Returns the column number where the error occurred.
#[must_use]
pub const fn column(&self) -> usize {

Check warning on line 48 in src/modules/error.rs

View check run for this annotation

Codecov / codecov/patch

src/modules/error.rs#L48

Added line #L48 was not covered by tests
self.column
}
Expand Down

0 comments on commit 3765f0f

Please sign in to comment.