Skip to content

Commit

Permalink
tested and fixed stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistonight committed Oct 8, 2023
1 parent 4e925e7 commit b9b54de
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 49 deletions.
39 changes: 38 additions & 1 deletion compiler-core/src/lang/rich/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,27 @@ fn parse_tagexp(pt: &pt::TagExp) -> DocRichText {
arg.push_str(str);
}
for pt_unit in pt.m_arg.iter() {
append_unit_to_string(pt_unit, &mut arg);
append_unit_inside_tag_to_string(pt_unit, &mut arg);
}
DocRichText {
tag: Some(tag),
text: arg,
link: None,
}
}
fn append_unit_inside_tag_to_string(pt: &pt::UnitInsideTag, out: &mut String) {
match pt {
pt::UnitInsideTag::Unit(pt) => {
append_unit_to_string(pt, out);
}
pt::UnitInsideTag::UnitDotSymbol(_) => {
out.push('.');
}
pt::UnitInsideTag::UnitOpenParenSymbol(_) => {
out.push('(');
}
}
}

fn append_unit_to_string(pt: &pt::Unit, out: &mut String) {
match pt {
Expand Down Expand Up @@ -253,4 +266,28 @@ mod test {
}]
);
}

#[test]
fn test_dot_in_text() {
assert_eq!(
parse_rich(".tag([hello]continue.me)"),
vec![DocRichText {
tag: Some("tag".to_string()),
text: "[hello]continue.me".to_string(),
link: None
}]
);
}

#[test]
fn test_open_paren_in_text() {
assert_eq!(
parse_rich(".tag([hello]co(ntinue.me)"),
vec![DocRichText {
tag: Some("tag".to_string()),
text: "[hello]co(ntinue.me".to_string(),
link: None
}]
);
}
}
5 changes: 4 additions & 1 deletion compiler-core/src/lang/rich/rich.grammar
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ rule TagExp(
(Tag) tag: token Identifier,
_: token Symbol"(",
space: optional token Space,
(Arg) arg: optional Unit+,
(Arg) arg: optional UnitInsideTag+,
_: token Symbol")"
);

rule Unit = UnitId | UnitEscape;
rule UnitInsideTag = Unit | UnitDotSymbol | UnitOpenParenSymbol;
rule UnitId(t: token Identifier, s: optional token Space);
rule UnitEscape(t: token Escape, s: optional token Space);
rule UnitDotSymbol((Text)_: token Symbol".", s: optional token Space);
rule UnitOpenParenSymbol((Text)_: token Symbol"(", s: optional token Space);
rule Symbol((Text)t: token Symbol);
rule Space(t: token Space);
17 changes: 6 additions & 11 deletions compiler-core/src/pack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! The output of the packer is a [`RouteMetadata`](celerctypes::RouteMetadata)
//! and a json blob of the route.

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::collections::BTreeMap;
use std::convert::Infallible;
Expand Down Expand Up @@ -36,7 +37,8 @@ use crate::lang::parse_poor;
#[cfg(feature = "wasm")]
use crate::util::WasmError;

#[derive(Debug, Clone, PartialEq, thiserror::Error)]
#[derive(Debug, Clone, PartialEq, thiserror::Error, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", tag = "type", content="data")]
pub enum PackerError {
#[error("The project file (project.yaml) is missing or invalid.")]
InvalidProject,
Expand Down Expand Up @@ -120,10 +122,6 @@ pub enum PackerError {

#[error("{0}")]
NotImpl(String),

#[cfg(feature = "wasm")]
#[error("Wasm execution error: {0}")]
Wasm(#[from] WasmError),
}

impl PackerError {
Expand All @@ -136,11 +134,7 @@ impl PackerError {
}

pub fn is_cancel(&self) -> bool {
#[cfg(feature = "wasm")]
let x = matches!(self, Self::Wasm(WasmError::Cancel));
#[cfg(not(feature = "wasm"))]
let x = false;
x
false
}
}

Expand All @@ -156,7 +150,8 @@ pub type PackerResult<T> = Result<T, PackerError>;
///
/// This is used to expose errors to the compiler, so it can be displayed
/// using the diagnostics API
#[derive(Debug, PartialEq)]
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "camelCase")]
pub enum PackerValue {
Ok(Value),
Err(PackerError),
Expand Down
27 changes: 14 additions & 13 deletions compiler-core/src/pack/pack_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async fn process_config(
let config = config.try_into_object().map_err(|_| PackerError::InvalidConfigType(index))?;

// add values to builder
async_for!((key, value) in config.into_iter(), {
let _ = async_for!((key, value) in config.into_iter(), {
match key.as_ref() {
prop::MAP => {
if builder.map.is_some() {
Expand All @@ -82,16 +82,16 @@ async fn process_config(
}
prop::TAGS => {
let tags = value.try_into_object().map_err(|_| PackerError::InvalidConfigProperty(index, prop::TAGS.to_string()))?;
async_for!((key, value) in tags.into_iter(), {
let _ = async_for!((key, value) in tags.into_iter(), {
let tag = serde_json::from_value::<DocTag>(value).map_err(|_| PackerError::InvalidConfigProperty(index, format!("{}.{}", prop::TAGS, key)))?;
builder.tags.insert(key, tag);
})?;
});
}
prop::PRESETS => {
let presets = pack_presets(value, index, setting.max_preset_namespace_depth).await?;
async_for!((key, value) in presets.into_iter(), {
let _ = async_for!((key, value) in presets.into_iter(), {
builder.presets.insert(key, value);
})?;
});
}
prop::DEFAULT_ICON_PRIORITY => {
let priority = value.try_coerce_to_i64().ok_or_else(|| PackerError::InvalidConfigProperty(index, prop::DEFAULT_ICON_PRIORITY.to_string()))?;
Expand All @@ -102,7 +102,7 @@ async fn process_config(
}
_ => return Err(PackerError::UnusedConfigProperty(index, key)),
}
})?;
});

Ok(())

Expand All @@ -119,7 +119,7 @@ async fn process_icons_config(
) -> PackerResult<()> {
let icons = icons.try_into_object().map_err(|_| PackerError::InvalidConfigProperty(index, prop::ICONS.to_string()))?;

async_for!((key, v) in icons.into_iter(), {
let _ = async_for!((key, v) in icons.into_iter(), {
match Use::try_from(v) {
Err(v) => {
// not a use, just a icon url
Expand All @@ -135,7 +135,7 @@ async fn process_icons_config(
builder.icons.insert(key, image_url);
}
}
})?;
});

Ok(())
}
Expand All @@ -151,17 +151,18 @@ async fn process_plugins_config(
) -> PackerResult<()> {
let plugins = plugins.try_into_array().map_err(|_| PackerError::InvalidConfigProperty(index, prop::PLUGINS.to_string()))?;

async_for!((i, v) in plugins.into_iter().enumerate(), {
let _ = async_for!((i, v) in plugins.into_iter().enumerate(), {
let v = v.try_into_object().map_err(|_| PackerError::InvalidConfigProperty(index, format!("{}[{}]", prop::PLUGINS, i)))?;
let mut plugin = None;
let mut props = json!(null);
async_for!((key, value) in v.into_iter(), {
let _ = async_for!((key, value) in v.into_iter(), {
match key.as_ref() {
prop::USE => {
let use_path_string = value.coerce_to_string();
plugin = match serde_json::from_value::<BuiltInPlugin>(value) {
Ok(built_in) => Some(Plugin::BuiltIn(built_in)),
Err(_) => {
Err(e) => {
log::info!("e: {e}");
// it's a script path, parse as use
match Use::from(use_path_string) {
Use::Invalid(path) => return Err(PackerError::InvalidPlugin(path)),
Expand All @@ -180,7 +181,7 @@ async fn process_plugins_config(
}
_ => return Err(PackerError::UnusedConfigProperty(index, format!("{}[{}].{}", prop::PLUGINS, i, key))),
}
})?;
});
let plugin = match plugin {
Some(v) => v,
None => return Err(PackerError::MissingConfigProperty(index, format!("{}[{}].{}", prop::PLUGINS, i, prop::USE))),
Expand All @@ -189,7 +190,7 @@ async fn process_plugins_config(
plugin,
props,
});
})?;
});

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/pack/pack_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ pub async fn pack_map(value: Value, index: usize) -> PackerResult<MapMetadata> {

let layers = {
let mut packed_layers = Vec::with_capacity(layers.len());
async_for!((i, layer) in layers.into_iter().enumerate(), {
let _ = async_for!((i, layer) in layers.into_iter().enumerate(), {
packed_layers.push(pack_map_layer(layer, index, i)?);
})?;
});
packed_layers
};

Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/pack/pack_preset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async fn pack_presets_internal(
}
})?;

async_for!((key, value) in obj.into_iter(), {
let _ = async_for!((key, value) in obj.into_iter(), {
if let Some(namespace) = key.strip_prefix('_') {
// sub namespace
let full_key = if preset_name.is_empty() {
Expand All @@ -63,7 +63,7 @@ async fn pack_presets_internal(
})?;
output.push((full_key, preset));
}
})?;
});

Ok(())
}
4 changes: 2 additions & 2 deletions compiler-core/src/pack/pack_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ pub async fn pack_project(
.map_err(|_| PackerError::InvalidMetadataPropertyType(prop::CONFIG.to_string()))?;

let mut builder = ConfigBuilder::default();
async_for!((i, config) in config.into_iter().enumerate(), {
let _ = async_for!((i, config) in config.into_iter().enumerate(), {
pack_config(&mut builder, project_resource, config, i, setting).await?;
})?;
});

let route_metadata = RouteMetadata {
name: project_resource.name().to_string(),
Expand Down
2 changes: 2 additions & 0 deletions compiler-core/src/plug/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::comp::prop;
use super::operation;

pub async fn run_link_plugin(comp_doc: &mut CompDoc) {
// add the link tag if not defined already
comp_doc.project.tags.entry(prop::LINK.to_string()).or_default();
operation::for_all_lines(comp_doc, |mut line| async {
operation::for_all_rich_text(&mut line, transform_link_tag).await;
line
Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/plug/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub enum Plugin {
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged, rename_all = "kebab-case")]
#[serde(rename_all = "kebab-case")]
pub enum BuiltInPlugin {
/// Transform link tags to clickable links. See [`link`]
Link
Expand Down
4 changes: 4 additions & 0 deletions compiler-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,16 @@ pub struct RouteMetadata {
#[ts(export)]
pub struct DocTag {
/// Bold style
#[serde(default)]
bold: bool,
/// Italic style
#[serde(default)]
italic: bool,
/// Underline style
#[serde(default)]
underline: bool,
/// Strikethrough style
#[serde(default)]
strikethrough: bool,
/// Color of the text
color: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion compiler-wasm/src/loader_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl FileLoader {
#[async_trait::async_trait(?Send)]
impl ResourceLoader for FileLoader {
async fn load_raw(&self, path: &str) -> PackerResult<Vec<u8>> {
yield_now!()?;
let _ = yield_now!();
let result: Result<Uint8Array, JsValue> = async {
let promise = self
.load_file
Expand Down
2 changes: 1 addition & 1 deletion compiler-wasm/src/loader_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl UrlLoader {
#[async_trait::async_trait(?Send)]
impl ResourceLoader for UrlLoader {
async fn load_raw(&self, url: &str) -> PackerResult<Vec<u8>> {
yield_now!()?;
let _ = yield_now!();
let result: Result<Uint8Array, JsValue> = async {
let promise = self
.fetch
Expand Down
11 changes: 1 addition & 10 deletions docs/src/.vitepress/nav/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ export const pluginsSideBar = {
text: "Built-in Plugins",
items: [
{ text: "Getting Started", link: "/plugin/getting-started" },
// { text: "Configuration", link: "/route/configuration" },
// { text: "File Structure", link: "/route/file-structure" },
// { text: "Route Structure", link: "/route/route-structure" },
// { text: "Customizing Lines", link: "/route/customizing-lines" },
// { text: "Customizing Text", link: "/route/customizing-text" },
// {
// text: "Customizing Movements",
// link: "/route/customizing-movements",
// },
// { text: "Using Presets", link: "/route/using-presets" },
{ text: "Link", link: "/plugin/link" },
],
},
// {
Expand Down
6 changes: 5 additions & 1 deletion docs/src/plugin/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
# Getting Started
:::tip
The plugin system is currently unstable.
:::
The principle of the plugin system is to separate core Celer functionalities from additional (and mostly optional) functionalities.

A plugin in Celer is a piece of program that runs as part of the compiler. The process goes as the following:

1. The compiler parses the income files and celer-specific syntax like presets
2. The compiler hands the compiled document to a plugin
3. The plugin is free to make any changes to the document. Then it hands the document to the next plugin in line.
4. After the last plugin is done modifying the document, it hands the document back to the compiler.

The principle of the plugin system is to separate core Celer functionalities from additional (and mostly optional) functionalities.

## Configuration
To add a plugin to the compiler, use the `plugins` property in your `config`.
Expand Down
3 changes: 3 additions & 0 deletions docs/src/plugin/index.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Plugins
:::tip
The plugin system is currently unstable.
:::
This section contains guides for how to utilize Celer's plugin system to add
extra features to your route doc.

Expand Down
Loading

0 comments on commit b9b54de

Please sign in to comment.