Skip to content

Commit

Permalink
fixup! feat(crate): Implement From<String> for option types
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed Dec 2, 2024
1 parent ae55299 commit 794b3f2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ impl From<&String> for Case {
}
}

impl From<&[u8]> for Case {
fn from(s: &[u8]) -> Self {
let s = String::from_utf8(s.to_vec()).unwrap();
Self::from_str(s.as_ref()).unwrap()
}
}

impl FromStr for StyleGuide {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
Expand Down
25 changes: 17 additions & 8 deletions typst/decasify.typ
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
#let _plugin = plugin("decasify.wasm")

#let string-to-titlecase(text, lang, style) = {
#let decasify-string(text, case, lang, style) = {
str(_plugin.case(bytes(text), bytes(case), bytes(lang), bytes(style)))
}

#let decasify(body, case, style: "default") = {
show regex(".+"): it => decasify(it.text, case, text.lang, style)
body
}

#let titlecase-string(text, lang, style) = {
str(_plugin.titlecase(bytes(text), bytes(lang), bytes(style)))
}

#let titlecase(body, style: "default") = {
show regex(".+"): it => string-to-titlecase(it.text, text.lang, style)
show regex(".+"): it => titlecase-string(it.text, text.lang, style)
body
}

#let string-to-lowercase(text, lang) = {
#let lowercase-string(text, lang) = {
str(_plugin.lowercase(bytes(text), bytes(lang)))
}

#let lowercase(body) = {
show regex(".+"): it => string-to-lowercase(it.text, text.lang)
show regex(".+"): it => lowercase-string(it.text, text.lang)
body
}

#let string-to-uppercase(text, lang) = {
#let uppercase-string(text, lang) = {
str(_plugin.uppercase(bytes(text), bytes(lang)))
}

#let uppercase(body) = {
show regex(".+"): it => string-to-uppercase(it.text, text.lang)
show regex(".+"): it => uppercase-string(it.text, text.lang)
body
}

#let string-to-sentencecase(text, lang) = {
#let sentencecase-string(text, lang) = {
str(_plugin.sentencecase(bytes(text), bytes(lang)))
}

#let sentencecase(body) = {
show regex(".+"): it => string-to-sentencecase(it.text, text.lang)
show regex(".+"): it => sentencecase-string(it.text, text.lang)
body
}
11 changes: 10 additions & 1 deletion typst/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
use anyhow::Result;
use decasify::{Locale, StyleGuide};
use decasify::{Case, Locale, StyleGuide};
use wasm_minimal_protocol::{initiate_protocol, wasm_func};

initiate_protocol!();

#[wasm_func]
pub fn decasify(data: &[u8], case: &[u8], lang: &[u8], style: &[u8]) -> Result<Vec<u8>> {
let chunk = String::from_utf8(data.to_vec())?;
let case = Case::from(case);
let locale = Locale::from(lang);
let style = StyleGuide::from(style);
Ok(decasify::case(&chunk, case, locale, style).into_bytes())
}

#[wasm_func]
pub fn titlecase(data: &[u8], lang: &[u8], style: &[u8]) -> Result<Vec<u8>> {
let chunk = String::from_utf8(data.to_vec())?;
Expand Down

0 comments on commit 794b3f2

Please sign in to comment.