Skip to content

Commit

Permalink
chore: lint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
prateekmedia committed Aug 15, 2024
1 parent cd39178 commit 4ccc937
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
40 changes: 23 additions & 17 deletions src/rust/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,26 @@ pub trait FromC<T> {
// }

pub trait CType<T> {
/// # Safety
/// This function is unsafe because it dereferences the pointer passed to it.
unsafe fn to_ctype(&self) -> T;
}
pub trait CType2<T, U> {
/// # Safety
/// This function is unsafe because it dereferences the pointer passed to it.
unsafe fn to_ctype(&self, value: U) -> T;
}
pub trait FromRust<T> {
unsafe fn from_rust(&self, options: T);
/// # Safety
/// This function is unsafe because it dereferences the pointer passed to it.
unsafe fn copy_from_rust(&self, options: T);
}

impl FromRust<Options> for *mut ccx_s_options {
/// # Safety
///
/// This function is unsafe because it dereferences the pointer passed to it.
unsafe fn from_rust(self: &*mut ccx_s_options, options: Options) {
unsafe fn copy_from_rust(self: &*mut ccx_s_options, options: Options) {
(**self).extract = options.extract as _;
(**self).no_rollup = options.no_rollup as _;
(**self).noscte20 = options.noscte20 as _;
Expand Down Expand Up @@ -215,7 +221,7 @@ impl FromRust<Options> for *mut ccx_s_options {

if options.sentence_cap_file.try_exists().unwrap_or_default() {
(**self).sentence_cap_file = string_to_c_char(
&options
options
.sentence_cap_file
.clone()
.to_str()
Expand All @@ -234,7 +240,7 @@ impl FromRust<Options> for *mut ccx_s_options {
.unwrap_or_default()
{
(**self).filter_profanity_file = string_to_c_char(
&options
options
.filter_profanity_file
.clone()
.to_str()
Expand Down Expand Up @@ -275,7 +281,7 @@ impl FromRust<Options> for *mut ccx_s_options {
(**self).dvblang = string_to_c_char(dvblang.to_ctype().as_str());
}
if options.ocrlang.try_exists().unwrap_or_default() {
(**self).ocrlang = string_to_c_char(&options.ocrlang.to_str().unwrap());
(**self).ocrlang = string_to_c_char(options.ocrlang.to_str().unwrap());
}
(**self).ocr_oem = options.ocr_oem as _;
(**self).ocr_quantmode = options.ocr_quantmode as _;
Expand Down Expand Up @@ -369,19 +375,19 @@ impl CType2<ccx_s_teletext_config, &Options> for TeletextConfig {
_bitfield_2: Default::default(),
_bitfield_align_1: Default::default(),
_bitfield_align_2: Default::default(),
page: (*self).page.get().page().into(),
page: self.user_page,
tid: 0,
offset: 0.0,
user_page: (*self).user_page,
dolevdist: (*self).dolevdist.into(),
levdistmincnt: (*self).levdistmincnt.into(),
levdistmaxpct: (*self).levdistmaxpct.into(),
extraction_start: (*self).extraction_start.to_ctype(),
extraction_end: (*self).extraction_end.to_ctype(),
write_format: (*self).write_format.to_ctype(),
user_page: self.user_page,
dolevdist: self.dolevdist.into(),
levdistmincnt: self.levdistmincnt.into(),
levdistmaxpct: self.levdistmaxpct.into(),
extraction_start: self.extraction_start.to_ctype(),
extraction_end: self.extraction_end.to_ctype(),
write_format: self.write_format.to_ctype(),
gui_mode_reports: value.gui_mode_reports as _,
date_format: (*self).date_format.to_ctype(),
noautotimeref: (*self).noautotimeref.into(),
date_format: self.date_format.to_ctype(),
noautotimeref: self.noautotimeref.into(),
send_to_srv: value.send_to_srv.into(),
encoding: value.enc_cfg.encoding.to_ctype(),
nofontcolor: self.nofontcolor.into(),
Expand Down Expand Up @@ -484,7 +490,7 @@ impl CType<u32> for Encoding {
impl CType<String> for Language {
/// Convert to C variant of `String`.
unsafe fn to_ctype(&self) -> String {
return self.to_str().to_lowercase();
self.to_str().to_lowercase()
}
}

Expand Down Expand Up @@ -729,7 +735,7 @@ impl CType<encoder_cfg> for EncoderConfig {
in_format: self.in_format,
nospupngocr: self.nospupngocr as _,
force_dropframe: self.force_dropframe as _,
render_font: string_to_c_char(&self.render_font.to_str().unwrap_or_default()),
render_font: string_to_c_char(self.render_font.to_str().unwrap_or_default()),
render_font_italics: string_to_c_char(
self.render_font_italics.to_str().unwrap_or_default(),
),
Expand Down
2 changes: 1 addition & 1 deletion src/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub unsafe extern "C" fn ccxr_parse_parameters(
opt.parse_parameters(&args, &mut _tlt_config);
tlt_config = _tlt_config.to_ctype(&opt);
// Convert the rust struct (CcxOptions) to C struct (ccx_s_options), so that it can be used by the C code
_options.from_rust(opt);
_options.copy_from_rust(opt);

0
}
Expand Down
24 changes: 10 additions & 14 deletions src/rust/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl OptionsExt for Options {

if charsets.len() > i && charsets[i].is_some() {
if let DtvccServiceCharset::Unique(unique) = &mut self.enc_cfg.services_charsets {
unique[svc - 1] = charsets[i].as_ref().unwrap().clone();
charsets[i].as_ref().unwrap().clone_from(&&unique[svc - 1]);
}
}
}
Expand Down Expand Up @@ -420,7 +420,7 @@ impl OptionsExt for Options {
if let Some(ref mut inputfile) = self.inputfile {
inputfile.resize(new_size, String::new());

let index = num_input_files as usize;
let index = num_input_files;
inputfile[index] = filename.to_string();
}
}
Expand Down Expand Up @@ -1132,13 +1132,13 @@ impl OptionsExt for Options {

if let Some(ref datastreamtype) = args.datastreamtype {
self.demux_cfg.ts_datastreamtype =
StreamType::from_repr(datastreamtype.clone().into()).unwrap_or_default();
StreamType::from_repr((*datastreamtype).into()).unwrap_or_default();
// TODO: Should I panick?
}

if let Some(ref streamtype) = args.streamtype {
self.demux_cfg.ts_forced_streamtype =
StreamType::from_repr(streamtype.clone().into()).unwrap_or_default();
StreamType::from_repr((*streamtype).into()).unwrap_or_default();
}

if let Some(ref tpage) = args.tpage {
Expand Down Expand Up @@ -1217,15 +1217,11 @@ impl OptionsExt for Options {
}

if args.sects {
self.date_format = TimestampFormat::Seconds {
millis_separator: millis_separator,
};
self.date_format = TimestampFormat::Seconds { millis_separator };
}

if args.datets {
self.date_format = TimestampFormat::Date {
millis_separator: millis_separator,
};
self.date_format = TimestampFormat::Date { millis_separator };
}

if args.teletext {
Expand Down Expand Up @@ -1389,7 +1385,7 @@ impl OptionsExt for Options {
unsafe {
CAPITALIZATION_LIST = get_vector_words(&CAPITALIZED_BUILTIN);
if self.sentence_cap_file.exists() {
if let Some(ref sentence_cap_file) = self.sentence_cap_file.to_str() {
if let Some(sentence_cap_file) = self.sentence_cap_file.to_str() {
let result =
process_word_file(sentence_cap_file, addr_of_mut!(CAPITALIZATION_LIST));

Expand All @@ -1405,7 +1401,7 @@ impl OptionsExt for Options {
unsafe {
PROFANE = get_vector_words(&PROFANE_BUILTIN);
if self.filter_profanity_file.exists() {
if let Some(ref profanityfile) = self.filter_profanity_file.to_str() {
if let Some(profanityfile) = self.filter_profanity_file.to_str() {
let result = process_word_file(profanityfile, addr_of_mut!(PROFANE));

if result.is_err() {
Expand All @@ -1421,8 +1417,8 @@ impl OptionsExt for Options {
tlt_config.dolevdist = self.dolevdist;
tlt_config.levdistmincnt = self.levdistmincnt;
tlt_config.levdistmaxpct = self.levdistmaxpct;
tlt_config.extraction_start = Some(self.extraction_start.clone());
tlt_config.extraction_end = Some(self.extraction_end.clone());
tlt_config.extraction_start = Some(self.extraction_start);
tlt_config.extraction_end = Some(self.extraction_end);
tlt_config.write_format = self.write_format;
tlt_config.date_format = self.date_format;
tlt_config.noautotimeref = self.noautotimeref;
Expand Down

0 comments on commit 4ccc937

Please sign in to comment.