Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default value for Config #35

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions crates/binding/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ impl TryFrom<JsConfig> for Config {
fn try_from(val: JsConfig) -> Result<Self, Self::Error> {
let expand_props = match val.expand_props {
Some(raw) => match raw {
Either::A(b) => ExpandProps::Bool(b),
Either::A(b) => {
if b {
ExpandProps::End
} else {
ExpandProps::None
}
}
Either::B(s) => match s.as_str() {
"start" => ExpandProps::Start,
"end" => ExpandProps::End,
Expand All @@ -142,8 +148,8 @@ impl TryFrom<JsConfig> for Config {
});

let svg_props = match val.svg_props {
Some(raw) => Some(raw.0),
None => None,
Some(raw) => raw.0,
None => vec![],
};

let jsx_runtime = match val.jsx_runtime {
Expand Down Expand Up @@ -184,21 +190,21 @@ impl TryFrom<JsConfig> for Config {
};

Ok(Self {
r#ref: val.r#ref,
title_prop: val.title_prop,
desc_prop: val.desc_prop,
r#ref: val.r#ref.unwrap_or(false),
title_prop: val.title_prop.unwrap_or(false),
desc_prop: val.desc_prop.unwrap_or(false),
expand_props,
dimensions: val.dimensions,
dimensions: val.dimensions.unwrap_or(true),
icon,
native: val.native,
native: val.native.unwrap_or(false),
svg_props,
typescript: val.typescript,
memo: val.memo,
typescript: val.typescript.unwrap_or(false),
memo: val.memo.unwrap_or(false),
replace_attr_values,
jsx_runtime: Some(jsx_runtime),
jsx_runtime,
jsx_runtime_import,
named_export,
export_type: Some(export_type),
export_type,
})
}
}
11 changes: 6 additions & 5 deletions crates/binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use state::JsState;
use svgr_rs::{transform, Config};

pub struct TransformTask {
code: String,
code: Option<String>,
config: Option<JsConfig>,
state: Option<JsState>,
}
Expand All @@ -23,12 +23,13 @@ impl Task for TransformTask {
type JsValue = JsString;

fn compute(&mut self) -> Result<Self::Output> {
let config: Config = match self.config.clone() {
let config: Config = match self.config.take() {
Some(val) => val.try_into()?,
None => Config::default(),
};
let state = self.state.clone().map(|s| s.into()).unwrap_or_default();
match transform(self.code.clone(), config, state) {
let state = self.state.take().map(|s| s.into()).unwrap_or_default();
let code = self.code.take().unwrap();
match transform(code, config, state) {
Ok(result) => napi::Result::Ok(result),
Err(reason) => napi::Result::Err(napi::Error::from_reason(reason.to_string())),
}
Expand All @@ -46,7 +47,7 @@ pub fn transform_node(
state: Option<JsState>,
) -> AsyncTask<TransformTask> {
AsyncTask::new(TransformTask {
code,
code: Some(code),
config,
state,
})
Expand Down
32 changes: 12 additions & 20 deletions crates/core/src/add_jsx_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ impl Visitor {
pub fn new(config: &core::config::Config) -> Self {
let mut attributes = Vec::new();

if let Some(svg_props) = &config.svg_props {
for SvgProp { key, value } in svg_props {
let attr = svg_prop_to_attr(key, value);
attributes.push(attr);
}
for SvgProp { key, value } in &config.svg_props {
let attr = svg_prop_to_attr(key, value);
attributes.push(attr);
}

let r#ref = config.r#ref.unwrap_or(false);
if r#ref {
if config.r#ref {
attributes.push(Attribute {
name: "ref".to_string(),
value: Some("ref".to_string()),
Expand All @@ -47,8 +44,7 @@ impl Visitor {
});
}

let title_prop = config.title_prop.unwrap_or(false);
if title_prop {
if config.title_prop {
attributes.push(Attribute {
name: "aria-labelledby".to_string(),
value: Some("titleId".to_string()),
Expand All @@ -57,8 +53,7 @@ impl Visitor {
});
}

let desc_prop = config.desc_prop.unwrap_or(false);
if desc_prop {
if config.desc_prop {
attributes.push(Attribute {
name: "aria-describedby".to_string(),
value: Some("descId".to_string()),
Expand All @@ -67,16 +62,13 @@ impl Visitor {
});
}

let expand_props = match config.expand_props {
core::config::ExpandProps::Bool(b) => b,
_ => true,
};
let position = match config.expand_props {
core::config::ExpandProps::Start => Some(AttributePosition::Start),
core::config::ExpandProps::End => Some(AttributePosition::End),
_ => None,
};
let expand_props = !matches!(config.expand_props, core::config::ExpandProps::None);
if expand_props {
let position = match config.expand_props {
core::config::ExpandProps::Start => Some(AttributePosition::Start),
core::config::ExpandProps::End => Some(AttributePosition::End),
core::config::ExpandProps::None => None,
};
attributes.push(Attribute {
name: "props".to_string(),
spread: true,
Expand Down
54 changes: 28 additions & 26 deletions crates/core/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ impl Default for Icon {

#[derive(Debug, Clone, Default)]
pub enum ExpandProps {
Bool(bool),
None,
Start,
#[default]
End,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub enum JSXRuntime {
#[default]
Classic,
ClassicPreact,
Automatic,
Expand All @@ -36,9 +37,10 @@ pub struct JSXRuntimeImport {
pub specifiers: Option<Vec<String>>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub enum ExportType {
Named,
#[default]
Default,
}

Expand All @@ -52,40 +54,40 @@ pub struct SvgProp {
#[derive(Debug, Clone)]
pub struct Config {
/// Setting this to `true` will forward ref to the root SVG tag.
pub r#ref: Option<bool>,
pub r#ref: bool,

/// Add title tag via title property.
/// If title_prop is set to true and no title is provided at render time, this will fallback to an existing title element in the svg if exists.
pub title_prop: Option<bool>,
pub title_prop: bool,

/// Add desc tag via desc property.
/// If desc_prop is set to true and no description is provided at render time, this will fallback to an existing desc element in the svg if exists.
pub desc_prop: Option<bool>,
pub desc_prop: bool,

/// All properties given to component will be forwarded on SVG tag.
/// Possible values: "start", "end" or false.
pub expand_props: ExpandProps,

/// Keep `width` and `height` attributes from the root SVG tag.
/// Removal is guaranteed if `dimensions: false`, unlike the `remove_dimensions: true` SVGO plugin option which also generates a `viewBox` from the dimensions if no `viewBox` is present.
pub dimensions: Option<bool>,
pub dimensions: bool,

/// Replace SVG `width` and `height` by a custom value.
/// If value is omitted, it uses `1em` in order to make SVG size inherits from text size.
pub icon: Option<Icon>,

/// Modify all SVG nodes with uppercase and use a specific template with `react-native-svg` imports.
/// All unsupported nodes will be removed.
pub native: Option<bool>,
pub native: bool,

/// Add props to the root SVG tag.
pub svg_props: Option<Vec<SvgProp>>,
pub svg_props: Vec<SvgProp>,

/// Generates `.tsx` files with TypeScript typings.
pub typescript: Option<bool>,
pub typescript: bool,

/// Setting this to `true` will wrap the exported component in `React.memo`.
pub memo: Option<bool>,
pub memo: bool,

/// Replace an attribute value by an other.
/// The main usage of this option is to change an icon color to "currentColor" in order to inherit from text color.
Expand All @@ -95,7 +97,7 @@ pub struct Config {
/// * "classic": adds `import * as React from 'react'` on the top of file
/// * "automatic": do not add anything
/// * "classic-preact": adds `import { h } from 'preact'` on the top of file
pub jsx_runtime: Option<JSXRuntime>,
pub jsx_runtime: JSXRuntime,

/// Specify a custom JSX runtime source to use. Allows to customize the import added at the top of generated file.
pub jsx_runtime_import: Option<JSXRuntimeImport>,
Expand All @@ -104,27 +106,27 @@ pub struct Config {
pub named_export: String,

/// If you prefer named export in any case, you may set the `export_type` option to `named`.
pub export_type: Option<ExportType>,
pub export_type: ExportType,
}

impl Default for Config {
fn default() -> Self {
Self {
r#ref: Default::default(),
title_prop: Default::default(),
desc_prop: Default::default(),
expand_props: Default::default(),
dimensions: Default::default(),
icon: Default::default(),
native: Default::default(),
svg_props: Default::default(),
typescript: Default::default(),
memo: Default::default(),
replace_attr_values: Default::default(),
jsx_runtime: Default::default(),
r#ref: false,
title_prop: false,
desc_prop: false,
expand_props: ExpandProps::End,
dimensions: true,
icon: None,
native: false,
svg_props: vec![],
typescript: false,
memo: false,
replace_attr_values: None,
jsx_runtime: JSXRuntime::Classic,
jsx_runtime_import: Default::default(),
named_export: "ReactComponent".to_string(),
export_type: Default::default(),
export_type: ExportType::Default,
}
}
}
12 changes: 4 additions & 8 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ pub fn transform(code: String, config: Config, state: State) -> Result<String, S
None => false,
_ => true,
};
let dimensions = config.dimensions.unwrap_or(true);
let m = if icon && dimensions {
let m = if icon && config.dimensions {
m.fold_with(&mut as_folder(svg_em_dimensions::Visitor::new(&config)))
} else {
m
Expand All @@ -91,26 +90,23 @@ pub fn transform(code: String, config: Config, state: State) -> Result<String, S
m
};

let title_prop = config.title_prop.unwrap_or(false);
let m = if title_prop {
let m = if config.title_prop {
m.fold_with(&mut as_folder(svg_dynamic_title::Visitor::new(
"title".to_string(),
)))
} else {
m
};

let desc_prop = config.desc_prop.unwrap_or(false);
let m = if desc_prop {
let m = if config.desc_prop {
m.fold_with(&mut as_folder(svg_dynamic_title::Visitor::new(
"desc".to_string(),
)))
} else {
m
};

let native = config.native.unwrap_or(false);
let m = if native {
let m = if config.native {
let comments = SingleThreadedComments::default();
m.fold_with(&mut as_folder(transform_react_native_svg::Visitor::new(
&comments,
Expand Down
3 changes: 1 addition & 2 deletions crates/core/src/remove_jsx_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ impl Visitor {
pub fn new(config: &core::config::Config) -> Self {
let mut attributes = vec!["version".to_string()];

let dimensions = config.dimensions.unwrap_or(true);
if !dimensions {
if !config.dimensions {
attributes.push("width".to_string());
attributes.push("height".to_string());
}
Expand Down
3 changes: 1 addition & 2 deletions crates/core/src/svg_em_dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ impl Visitor {
width = Some(Size::Num(n));
}
core::config::Icon::Bool(_) => {
let native = config.native.unwrap_or(false);
if native {
if config.native {
height = Some(Size::Num(24.0));
width = Some(Size::Num(24.0));
} else {
Expand Down
Loading
Loading