Skip to content

Commit

Permalink
finish updating to magnus 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
gjtorikian committed Oct 12, 2023
1 parent 0967958 commit 5ddd9d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 30 deletions.
10 changes: 3 additions & 7 deletions ext/commonmarker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ mod utils;

pub const EMPTY_STR: &str = "";

fn commonmark_to_html<'a>(args: &[Value]) -> Result<String, magnus::Error> {
let args = scan_args::scan_args(args)?;
fn commonmark_to_html(args: &[Value]) -> Result<String, magnus::Error> {
let args = scan_args::scan_args::<_, (), (), (), _, ()>(args)?;
let (rb_commonmark,): (String,) = args.required;
let _: () = args.optional;
let _: () = args.splat;
let _: () = args.trailing;
let _: () = args.block;

let kwargs = scan_args::get_kwargs::<_, (), (Option<RHash>, Option<RHash>), ()>(
args.keywords,
Expand Down Expand Up @@ -76,7 +72,7 @@ fn commonmark_to_html<'a>(args: &[Value]) -> Result<String, magnus::Error> {
if !path.eq(&PathBuf::from("".to_string())) && !path.exists() {
return Err(Error::new(
exception::arg_error(),
format!("path does not exist"),
"path does not exist".to_string(),
));
}

Expand Down
32 changes: 17 additions & 15 deletions ext/commonmarker/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::borrow::Cow;

use comrak::ComrakOptions;

use magnus::value::ReprValue;
use magnus::TryConvert;
use magnus::{class, r_hash::ForEach, Error, RHash, Symbol, Value};

use crate::utils::try_convert_string;
Expand All @@ -14,7 +16,7 @@ fn iterate_parse_options(comrak_options: &mut ComrakOptions, options_hash: RHash
.foreach(|key: Symbol, value: Value| {
match key.name() {
Ok(Cow::Borrowed(PARSE_SMART)) => {
comrak_options.parse.smart = value.try_convert::<bool>()?;
comrak_options.parse.smart = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(PARSE_DEFAULT_INFO_STRING)) => {
comrak_options.parse.default_info_string = try_convert_string(value);
Expand All @@ -37,19 +39,19 @@ fn iterate_render_options(comrak_options: &mut ComrakOptions, options_hash: RHas
.foreach(|key: Symbol, value: Value| {
match key.name() {
Ok(Cow::Borrowed(RENDER_HARDBREAKS)) => {
comrak_options.render.hardbreaks = value.try_convert::<bool>()?;
comrak_options.render.hardbreaks = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(RENDER_GITHUB_PRE_LANG)) => {
comrak_options.render.github_pre_lang = value.try_convert::<bool>()?;
comrak_options.render.github_pre_lang = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(RENDER_WIDTH)) => {
comrak_options.render.width = value.try_convert::<usize>()?;
comrak_options.render.width = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(RENDER_UNSAFE)) => {
comrak_options.render.unsafe_ = value.try_convert::<bool>()?;
comrak_options.render.unsafe_ = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(RENDER_ESCAPE)) => {
comrak_options.render.escape = value.try_convert::<bool>()?;
comrak_options.render.escape = TryConvert::try_convert(value)?;
}
_ => {}
}
Expand All @@ -75,37 +77,37 @@ fn iterate_extension_options(comrak_options: &mut ComrakOptions, options_hash: R
.foreach(|key: Symbol, value: Value| {
match key.name() {
Ok(Cow::Borrowed(EXTENSION_STRIKETHROUGH)) => {
comrak_options.extension.strikethrough = value.try_convert::<bool>()?;
comrak_options.extension.strikethrough = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_TAGFILTER)) => {
comrak_options.extension.tagfilter = value.try_convert::<bool>()?;
comrak_options.extension.tagfilter = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_TABLE)) => {
comrak_options.extension.table = value.try_convert::<bool>()?;
comrak_options.extension.table = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_AUTOLINK)) => {
comrak_options.extension.autolink = value.try_convert::<bool>()?;
comrak_options.extension.autolink = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_TASKLIST)) => {
comrak_options.extension.tasklist = value.try_convert::<bool>()?;
comrak_options.extension.tasklist = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_SUPERSCRIPT)) => {
comrak_options.extension.superscript = value.try_convert::<bool>()?;
comrak_options.extension.superscript = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_HEADER_IDS)) => {
comrak_options.extension.header_ids = try_convert_string(value);
}
Ok(Cow::Borrowed(EXTENSION_FOOTNOTES)) => {
comrak_options.extension.footnotes = value.try_convert::<bool>()?;
comrak_options.extension.footnotes = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_DESCRIPTION_LISTS)) => {
comrak_options.extension.description_lists = value.try_convert::<bool>()?;
comrak_options.extension.description_lists = TryConvert::try_convert(value)?;
}
Ok(Cow::Borrowed(EXTENSION_FRONT_MATTER_DELIMITER)) => {
comrak_options.extension.front_matter_delimiter = try_convert_string(value);
}
Ok(Cow::Borrowed(EXTENSION_SHORTCODES)) => {
comrak_options.extension.shortcodes = value.try_convert::<bool>()?;
comrak_options.extension.shortcodes = TryConvert::try_convert(value)?;
}
_ => {}
}
Expand Down
13 changes: 7 additions & 6 deletions ext/commonmarker/src/plugins/syntax_highlighting.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use magnus::{RHash, Symbol, Value};
use magnus::value::ReprValue;
use magnus::{RHash, Symbol, TryConvert, Value};

use crate::EMPTY_STR;

Expand All @@ -14,7 +15,7 @@ pub fn fetch_syntax_highlighter_theme(value: Value) -> Result<String, magnus::Er
return Ok(EMPTY_STR.to_string());
}

let syntax_highlighter_plugin = value.try_convert::<RHash>()?;
let syntax_highlighter_plugin: RHash = TryConvert::try_convert(value)?;
let theme_key = Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN_THEME_KEY);

match syntax_highlighter_plugin.get(theme_key) {
Expand All @@ -23,7 +24,7 @@ pub fn fetch_syntax_highlighter_theme(value: Value) -> Result<String, magnus::Er
// `syntax_highlighter: { theme: nil }`
return Ok(EMPTY_STR.to_string());
}
Ok(theme.try_convert::<String>()?)
Ok(TryConvert::try_convert(value)?)
}
None => {
// `syntax_highlighter: { }`
Expand All @@ -38,7 +39,7 @@ pub fn fetch_syntax_highlighter_path(value: Value) -> Result<PathBuf, magnus::Er
return Ok(PathBuf::from(EMPTY_STR));
}

let syntax_highlighter_plugin = value.try_convert::<RHash>()?;
let syntax_highlighter_plugin: RHash = TryConvert::try_convert(value)?;
let path_key = Symbol::new(SYNTAX_HIGHLIGHTER_PLUGIN_PATH_KEY);

match syntax_highlighter_plugin.get(path_key) {
Expand All @@ -47,8 +48,8 @@ pub fn fetch_syntax_highlighter_path(value: Value) -> Result<PathBuf, magnus::Er
// `syntax_highlighter: { path: nil }`
return Ok(PathBuf::from(EMPTY_STR));
}

Ok(PathBuf::from(path.try_convert::<String>()?))
let val: String = TryConvert::try_convert(path)?;
Ok(PathBuf::from(val))
}
None => {
// `syntax_highlighter: { }`
Expand Down
4 changes: 2 additions & 2 deletions ext/commonmarker/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use magnus::Value;
use magnus::{TryConvert, Value};

pub fn try_convert_string(value: Value) -> Option<String> {
match value.try_convert::<String>() {
match TryConvert::try_convert(value) {
Ok(s) => Some(s),
Err(_) => None,
}
Expand Down

0 comments on commit 5ddd9d4

Please sign in to comment.