Skip to content

Commit

Permalink
Replace Success with Result<(), ()>
Browse files Browse the repository at this point in the history
  • Loading branch information
simonask committed Jan 26, 2024
1 parent fbdff98 commit dc639b9
Show file tree
Hide file tree
Showing 11 changed files with 510 additions and 494 deletions.
66 changes: 33 additions & 33 deletions src/api.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::externs::{free, malloc, memcpy, memmove, memset, realloc, strdup, strlen};
use crate::ops::{ForceAdd as _, ForceMul as _};
use crate::success::{Success, FAIL, OK};
use crate::success::{FAIL, OK};
use crate::yaml::{size_t, yaml_char_t};
use crate::{
libc, yaml_break_t, yaml_document_t, yaml_emitter_state_t, yaml_emitter_t, yaml_encoding_t,
Expand Down Expand Up @@ -164,7 +164,7 @@ pub(crate) unsafe fn yaml_queue_extend(
///
/// This function creates a new parser object. An application is responsible
/// for destroying the object using the yaml_parser_delete() function.
pub unsafe fn yaml_parser_initialize(parser: *mut yaml_parser_t) -> Success {
pub unsafe fn yaml_parser_initialize(parser: *mut yaml_parser_t) -> Result<(), ()> {
__assert!(!parser.is_null());
memset(
parser as *mut libc::c_void,
Expand Down Expand Up @@ -296,7 +296,7 @@ pub unsafe fn yaml_parser_set_encoding(parser: *mut yaml_parser_t, encoding: yam
///
/// This function creates a new emitter object. An application is responsible
/// for destroying the object using the yaml_emitter_delete() function.
pub unsafe fn yaml_emitter_initialize(emitter: *mut yaml_emitter_t) -> Success {
pub unsafe fn yaml_emitter_initialize(emitter: *mut yaml_emitter_t) -> Result<(), ()> {
__assert!(!emitter.is_null());
memset(
emitter as *mut libc::c_void,
Expand Down Expand Up @@ -494,7 +494,7 @@ pub unsafe fn yaml_token_delete(token: *mut yaml_token_t) {
);
}

unsafe fn yaml_check_utf8(start: *const yaml_char_t, length: size_t) -> Success {
unsafe fn yaml_check_utf8(start: *const yaml_char_t, length: size_t) -> Result<(), ()> {
let end: *const yaml_char_t = start.wrapping_offset(length as isize);
let mut pointer: *const yaml_char_t = start;
while pointer < end {
Expand Down Expand Up @@ -555,7 +555,7 @@ unsafe fn yaml_check_utf8(start: *const yaml_char_t, length: size_t) -> Success
pub unsafe fn yaml_stream_start_event_initialize(
event: *mut yaml_event_t,
encoding: yaml_encoding_t,
) -> Success {
) -> Result<(), ()> {
let mark = yaml_mark_t {
index: 0_u64,
line: 0_u64,
Expand All @@ -575,7 +575,7 @@ pub unsafe fn yaml_stream_start_event_initialize(
}

/// Create the STREAM-END event.
pub unsafe fn yaml_stream_end_event_initialize(event: *mut yaml_event_t) -> Success {
pub unsafe fn yaml_stream_end_event_initialize(event: *mut yaml_event_t) -> Result<(), ()> {
let mark = yaml_mark_t {
index: 0_u64,
line: 0_u64,
Expand Down Expand Up @@ -603,7 +603,7 @@ pub unsafe fn yaml_document_start_event_initialize(
tag_directives_start: *mut yaml_tag_directive_t,
tag_directives_end: *mut yaml_tag_directive_t,
implicit: bool,
) -> Success {
) -> Result<(), ()> {
let current_block: u64;
let mark = yaml_mark_t {
index: 0_u64,
Expand Down Expand Up @@ -652,7 +652,7 @@ pub unsafe fn yaml_document_start_event_initialize(
(*tag_directive).handle,
strlen((*tag_directive).handle as *mut libc::c_char),
)
.fail
.is_err()
{
current_block = 14964981520188694172;
break;
Expand All @@ -661,7 +661,7 @@ pub unsafe fn yaml_document_start_event_initialize(
(*tag_directive).prefix,
strlen((*tag_directive).prefix as *mut libc::c_char),
)
.fail
.is_err()
{
current_block = 14964981520188694172;
break;
Expand Down Expand Up @@ -717,7 +717,7 @@ pub unsafe fn yaml_document_start_event_initialize(
pub unsafe fn yaml_document_end_event_initialize(
event: *mut yaml_event_t,
implicit: bool,
) -> Success {
) -> Result<(), ()> {
let mark = yaml_mark_t {
index: 0_u64,
line: 0_u64,
Expand All @@ -740,15 +740,15 @@ pub unsafe fn yaml_document_end_event_initialize(
pub unsafe fn yaml_alias_event_initialize(
event: *mut yaml_event_t,
anchor: *const yaml_char_t,
) -> Success {
) -> Result<(), ()> {
let mark = yaml_mark_t {
index: 0_u64,
line: 0_u64,
column: 0_u64,
};
__assert!(!event.is_null());
__assert!(!anchor.is_null());
if yaml_check_utf8(anchor, strlen(anchor as *mut libc::c_char)).fail {
if yaml_check_utf8(anchor, strlen(anchor as *mut libc::c_char)).is_err() {
return FAIL;
}
let anchor_copy: *mut yaml_char_t = yaml_strdup(anchor);
Expand Down Expand Up @@ -784,7 +784,7 @@ pub unsafe fn yaml_scalar_event_initialize(
plain_implicit: bool,
quoted_implicit: bool,
style: yaml_scalar_style_t,
) -> Success {
) -> Result<(), ()> {
let mut current_block: u64;
let mark = yaml_mark_t {
index: 0_u64,
Expand All @@ -797,7 +797,7 @@ pub unsafe fn yaml_scalar_event_initialize(
__assert!(!event.is_null());
__assert!(!value.is_null());
if !anchor.is_null() {
if yaml_check_utf8(anchor, strlen(anchor as *mut libc::c_char)).fail {
if yaml_check_utf8(anchor, strlen(anchor as *mut libc::c_char)).is_err() {
current_block = 16285396129609901221;
} else {
anchor_copy = yaml_strdup(anchor);
Expand All @@ -812,7 +812,7 @@ pub unsafe fn yaml_scalar_event_initialize(
}
if current_block == 8515828400728868193 {
if !tag.is_null() {
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).fail {
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).is_err() {
current_block = 16285396129609901221;
} else {
tag_copy = yaml_strdup(tag);
Expand All @@ -829,7 +829,7 @@ pub unsafe fn yaml_scalar_event_initialize(
if length < 0 {
length = strlen(value as *mut libc::c_char) as libc::c_int;
}
if yaml_check_utf8(value, length as size_t).ok {
if yaml_check_utf8(value, length as size_t).is_ok() {
value_copy = yaml_malloc(length.force_add(1) as size_t) as *mut yaml_char_t;
memcpy(
value_copy as *mut libc::c_void,
Expand Down Expand Up @@ -876,7 +876,7 @@ pub unsafe fn yaml_sequence_start_event_initialize(
tag: *const yaml_char_t,
implicit: bool,
style: yaml_sequence_style_t,
) -> Success {
) -> Result<(), ()> {
let mut current_block: u64;
let mark = yaml_mark_t {
index: 0_u64,
Expand All @@ -887,7 +887,7 @@ pub unsafe fn yaml_sequence_start_event_initialize(
let mut tag_copy: *mut yaml_char_t = ptr::null_mut::<yaml_char_t>();
__assert!(!event.is_null());
if !anchor.is_null() {
if yaml_check_utf8(anchor, strlen(anchor as *mut libc::c_char)).fail {
if yaml_check_utf8(anchor, strlen(anchor as *mut libc::c_char)).is_err() {
current_block = 8817775685815971442;
} else {
anchor_copy = yaml_strdup(anchor);
Expand All @@ -903,7 +903,7 @@ pub unsafe fn yaml_sequence_start_event_initialize(
match current_block {
11006700562992250127 => {
if !tag.is_null() {
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).fail {
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).is_err() {
current_block = 8817775685815971442;
} else {
tag_copy = yaml_strdup(tag);
Expand Down Expand Up @@ -942,7 +942,7 @@ pub unsafe fn yaml_sequence_start_event_initialize(
}

/// Create a SEQUENCE-END event.
pub unsafe fn yaml_sequence_end_event_initialize(event: *mut yaml_event_t) -> Success {
pub unsafe fn yaml_sequence_end_event_initialize(event: *mut yaml_event_t) -> Result<(), ()> {
let mark = yaml_mark_t {
index: 0_u64,
line: 0_u64,
Expand Down Expand Up @@ -971,7 +971,7 @@ pub unsafe fn yaml_mapping_start_event_initialize(
tag: *const yaml_char_t,
implicit: bool,
style: yaml_mapping_style_t,
) -> Success {
) -> Result<(), ()> {
let mut current_block: u64;
let mark = yaml_mark_t {
index: 0_u64,
Expand All @@ -982,7 +982,7 @@ pub unsafe fn yaml_mapping_start_event_initialize(
let mut tag_copy: *mut yaml_char_t = ptr::null_mut::<yaml_char_t>();
__assert!(!event.is_null());
if !anchor.is_null() {
if yaml_check_utf8(anchor, strlen(anchor as *mut libc::c_char)).fail {
if yaml_check_utf8(anchor, strlen(anchor as *mut libc::c_char)).is_err() {
current_block = 14748279734549812740;
} else {
anchor_copy = yaml_strdup(anchor);
Expand All @@ -997,7 +997,7 @@ pub unsafe fn yaml_mapping_start_event_initialize(
}
if current_block == 11006700562992250127 {
if !tag.is_null() {
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).fail {
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).is_err() {
current_block = 14748279734549812740;
} else {
tag_copy = yaml_strdup(tag);
Expand Down Expand Up @@ -1034,7 +1034,7 @@ pub unsafe fn yaml_mapping_start_event_initialize(
}

/// Create a MAPPING-END event.
pub unsafe fn yaml_mapping_end_event_initialize(event: *mut yaml_event_t) -> Success {
pub unsafe fn yaml_mapping_end_event_initialize(event: *mut yaml_event_t) -> Result<(), ()> {
let mark = yaml_mark_t {
index: 0_u64,
line: 0_u64,
Expand Down Expand Up @@ -1100,7 +1100,7 @@ pub unsafe fn yaml_document_initialize(
tag_directives_end: *mut yaml_tag_directive_t,
start_implicit: bool,
end_implicit: bool,
) -> Success {
) -> Result<(), ()> {
let current_block: u64;
struct Nodes {
start: *mut yaml_node_t,
Expand Down Expand Up @@ -1160,7 +1160,7 @@ pub unsafe fn yaml_document_initialize(
(*tag_directive).handle,
strlen((*tag_directive).handle as *mut libc::c_char),
)
.fail
.is_err()
{
current_block = 8142820162064489797;
break;
Expand All @@ -1169,7 +1169,7 @@ pub unsafe fn yaml_document_initialize(
(*tag_directive).prefix,
strlen((*tag_directive).prefix as *mut libc::c_char),
)
.fail
.is_err()
{
current_block = 8142820162064489797;
break;
Expand Down Expand Up @@ -1330,13 +1330,13 @@ pub unsafe fn yaml_document_add_scalar(
if tag.is_null() {
tag = b"tag:yaml.org,2002:str\0" as *const u8 as *const libc::c_char as *mut yaml_char_t;
}
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).ok {
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).is_ok() {
tag_copy = yaml_strdup(tag);
if !tag_copy.is_null() {
if length < 0 {
length = strlen(value as *mut libc::c_char) as libc::c_int;
}
if yaml_check_utf8(value, length as size_t).ok {
if yaml_check_utf8(value, length as size_t).is_ok() {
value_copy = yaml_malloc(length.force_add(1) as size_t) as *mut yaml_char_t;
memcpy(
value_copy as *mut libc::c_void,
Expand Down Expand Up @@ -1399,7 +1399,7 @@ pub unsafe fn yaml_document_add_sequence(
if tag.is_null() {
tag = b"tag:yaml.org,2002:seq\0" as *const u8 as *const libc::c_char as *mut yaml_char_t;
}
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).ok {
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).is_ok() {
tag_copy = yaml_strdup(tag);
if !tag_copy.is_null() {
STACK_INIT!(items, yaml_node_item_t);
Expand Down Expand Up @@ -1458,7 +1458,7 @@ pub unsafe fn yaml_document_add_mapping(
if tag.is_null() {
tag = b"tag:yaml.org,2002:map\0" as *const u8 as *const libc::c_char as *mut yaml_char_t;
}
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).ok {
if yaml_check_utf8(tag, strlen(tag as *mut libc::c_char)).is_ok() {
tag_copy = yaml_strdup(tag);
if !tag_copy.is_null() {
STACK_INIT!(pairs, yaml_node_pair_t);
Expand Down Expand Up @@ -1489,7 +1489,7 @@ pub unsafe fn yaml_document_append_sequence_item(
document: *mut yaml_document_t,
sequence: libc::c_int,
item: libc::c_int,
) -> Success {
) -> Result<(), ()> {
__assert!(!document.is_null());
__assert!(
sequence > 0
Expand Down Expand Up @@ -1520,7 +1520,7 @@ pub unsafe fn yaml_document_append_mapping_pair(
mapping: libc::c_int,
key: libc::c_int,
value: libc::c_int,
) -> Success {
) -> Result<(), ()> {
__assert!(!document.is_null());
__assert!(
mapping > 0
Expand Down
6 changes: 3 additions & 3 deletions src/bin/run-emitter-test-suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub(crate) unsafe fn unsafe_main(
) -> Result<(), Box<dyn Error>> {
let mut emitter = MaybeUninit::<yaml_emitter_t>::uninit();
let emitter = emitter.as_mut_ptr();
if yaml_emitter_initialize(emitter).fail {
if yaml_emitter_initialize(emitter).is_err() {
return Err("Could not initalize the emitter object".into());
}

Expand Down Expand Up @@ -133,10 +133,10 @@ pub(crate) unsafe fn unsafe_main(
break Err(format!("Unknown event: '{}'", CStr::from_ptr(line)).into());
};

if result.fail {
if result.is_err() {
break Err("Memory error: Not enough memory for creating an event".into());
}
if yaml_emitter_emit(emitter, event).fail {
if yaml_emitter_emit(emitter, event).is_err() {
break Err(match (*emitter).error {
YAML_MEMORY_ERROR => "Memory error: Not enough memory for emitting".into(),
YAML_WRITER_ERROR => {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/run-parser-test-suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) unsafe fn unsafe_main(
) -> Result<(), Box<dyn Error>> {
let mut parser = MaybeUninit::<yaml_parser_t>::uninit();
let parser = parser.as_mut_ptr();
if yaml_parser_initialize(parser).fail {
if yaml_parser_initialize(parser).is_err() {
return Err("Could not initialize the parser object".into());
}

Expand All @@ -66,7 +66,7 @@ pub(crate) unsafe fn unsafe_main(
let mut event = MaybeUninit::<yaml_event_t>::uninit();
let event = event.as_mut_ptr();
loop {
if yaml_parser_parse(parser, event).fail {
if yaml_parser_parse(parser, event).is_err() {
let mut error = format!("Parse error: {}", CStr::from_ptr((*parser).problem));
if (*parser).problem_mark.line != 0 || (*parser).problem_mark.column != 0 {
let _ = write!(
Expand Down

0 comments on commit dc639b9

Please sign in to comment.