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

1.0 Roadmap #1

Open
malobre opened this issue Apr 21, 2022 · 2 comments
Open

1.0 Roadmap #1

malobre opened this issue Apr 21, 2022 · 2 comments

Comments

@malobre
Copy link
Owner

malobre commented Apr 21, 2022

This describes what I aim to achieve with the crate, not its current state.

Design Goals

Provide an idiomatic API to interact with Vue SFC, this library should let users parse, create, modify and print SFC.

Non-goals

  • Pretty-printing block content.

Vue SFC Specifications

From vuejs.org:

  • A Single File Component or SFC is a file syntactically-compatible with HTML.
  • It may contain:
    • A single template block.
    • A single script block.
    • A single script setup block.
    • style blocks.
    • Custom blocks.
    • HTML comments.

From the compiler-sfc repo:

  • template blocks with no lang attribute or a lang attribute set to html are to be parsed in Data State.
  • Other blocks are to be parsed in RAWTEXT State.
@malobre malobre pinned this issue Apr 21, 2022
@malobre
Copy link
Owner Author

malobre commented May 7, 2022

Potential interface:

use std::collections::HashMap;

type Attributes = HashMap<String, Option<String>>;
type Content = String;

trait Block {
    const NAME: &'static str; // or `Cow<'static, str>` for more flexibility

    fn attributes(&self) -> &Attributes;
    fn attributes_mut(&mut self) -> &mut Attributes;

    fn content(&self) -> &Content;
    fn content_mut(&mut self) -> &mut Content;
}

trait CustomBlock: Block {/* snip */}

struct Template {/* snip */};
impl Block for Template { /* snip */ }

struct Script {/* snip */};
impl Block for Script { /* snip */ }

struct Style {/* snip */};
impl Block for Style { /* snip */ }

struct Sfc {/* snip */}

impl Sfc {
    pub fn new() -> Self;

    pub fn template(&self) -> Option<&Template>;
    pub fn template_mut(&mut self) -> Option<&mut Template>;

    pub fn script(&self) -> Option<&Script>;
    pub fn script_mut(&mut self) -> Option<&mut Script>;

    pub fn script_setup(&self) -> Option<&Script>;
    pub fn script_setup_mut(&self) -> Option<&mut Script>;

    pub fn styles(&self) -> &[&Style];
    pub fn styles_mut(&self) -> &[&mut Style];

    pub fn get<B: CustomBlock>(&self) -> &[&B];
    pub fn get_mut<B: CustomBlock>(&self) -> &[&mut B];
}

However this raises multiple questions:

  • How to handle top-level comments ?
  • How to handle script setup / script ?

@malobre
Copy link
Owner Author

malobre commented May 8, 2022

Because of top-level comments, order matters (comment could refer to something above, below or something else entirely), as such it makes sense to have sorted iterators:

impl Sfc {
    pub fn iter(&self) -> impl Iterator<Item = &BlockOrComment>;
    pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut BlockOrComment>;
    pub fn into_iter(self) -> impl Iterator<Item = BlockOrComment>;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant