Skip to content

Commit

Permalink
Merge pull request #2 from kaleidawave/number-parsing-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleidawave authored May 21, 2024
2 parents edbac6a + aacab87 commit 3d61f23
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 43 deletions.
77 changes: 39 additions & 38 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,59 +19,60 @@ jobs:
validity:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: ${{ env.CACHE_PATHS }}
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Check source is valid
run: cargo check --workspace
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ${{ env.CACHE_PATHS }}
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Check source is valid
run: cargo check --workspace

formating:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check formatting with rustfmt
run: cargo fmt --all --check
- uses: brndnmtthws/rust-action-cargo-binstall@v1
with:
packages: taplo-cli
- name: Check TOML formatting with taplo
run: |
taplo fmt --check **/*/Cargo.toml
- uses: actions/checkout@v4
- name: Check formatting with rustfmt
run: cargo fmt --all --check
- uses: brndnmtthws/rust-action-cargo-binstall@v1
with:
packages: taplo-cli
- name: Check TOML formatting with taplo
run: |
taplo fmt --check **/*/Cargo.toml
tests:
needs: validity
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: ${{ env.CACHE_PATHS }}
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Run all tests
run: cargo test --workspace --verbose --all-features
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ${{ env.CACHE_PATHS }}
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Run all tests
run: cargo test --workspace --verbose --all-features

clippy:
needs: validity
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: ${{ env.CACHE_PATHS }}
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Lint code with clippy
run: cargo clippy
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: ${{ env.CACHE_PATHS }}
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
- name: Lint code with clippy
run: cargo clippy

publish-ability:
runs-on: ubuntu-latest
if: false
steps:
- uses: actions/checkout@v3
- name: Check that it will publish to crates
run: |
cargo metadata --offline --format-version 1 --no-deps | jq -r ".workspace_members[]" | while read -r _n _v pathInfo ; do
cd ${pathInfo:13:-1}
cargo publish --no-verify --dry-run
done
shell: bash
- uses: actions/checkout@v4
- name: Check that it will publish to crates
run: |
cargo metadata --offline --format-version 1 --no-deps | jq -r ".workspace_members[]" | while read -r _n _v pathInfo ; do
cd ${pathInfo:13:-1}
cargo publish --no-verify --dry-run
done
shell: bash
10 changes: 8 additions & 2 deletions examples/package_json.rs → examples/scan.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use simple_json_parser::{parse, JSONParseError};

fn main() {
let content = r#"{
let base = r#"{
"name": "ezno",
"version": "0.0.14",
"description": "A JavaScript compiler and TypeScript checker written in Rust with a focus on static analysis and runtime performance",
Expand Down Expand Up @@ -81,7 +81,13 @@ fn main() {
}
}"#;

let result = parse(content, |keys, value| eprintln!("{keys:?} -> {value:?}"));
let content = if let Some(path) = std::env::args().nth(1) {
std::fs::read_to_string(path).unwrap()
} else {
base.to_owned()
};

let result = parse(&content, |keys, value| eprintln!("{keys:?} -> {value:?}"));

if let Err(JSONParseError { at, reason }) = result {
eprintln!("{reason:?} @ {at}");
Expand Down
2 changes: 1 addition & 1 deletion examples/to_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

parse(&content, |keys, value| root.set(keys, value))?;

eprintln!("Object:\n{root:#?}");
eprintln!("Parsed: {root:#?}");
Ok(())
}
2 changes: 1 addition & 1 deletion examples/to_object_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
});

eprintln!("Object:\n{root:#?}");
eprintln!("Parsed: {root:#?}");
Ok(())
}
11 changes: 10 additions & 1 deletion lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ pub fn parse_with_exit_signal<'a>(
key_chain.pop();
} else if let (']', Some(JSONKey::Index(..))) = (char, key_chain.last()) {
key_chain.pop();
} else if let c @ ('/' | '#') = char {
key_chain.pop();
*state = State::Comment {
last_was_asterisk: false,
start: idx,
multiline: false,
hash: c == '#',
};
} else if !char.is_whitespace() {
return Err(JSONParseError {
at: idx,
Expand Down Expand Up @@ -240,11 +248,12 @@ pub fn parse_with_exit_signal<'a>(
}
State::NumberValue { start } => {
// TODO actual number handing
if matches!(char, '\n' | ' ' | '}' | ',' | ']') {
if char.is_whitespace() || matches!(char, '}' | ',' | ']') {
let res = cb(&key_chain, RootJSONValue::Number(&on[start..idx]));
if res {
return Ok(());
}
state = State::EndOfValue;
end_of_value(idx, char, &mut state, &mut key_chain)?;
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fn main() {
"email": "[email protected]",
"url": "https://kaleidawave.github.io/"
},
"some_number": 4,
"funding": {
"type": "individual",
/*
Expand Down

0 comments on commit 3d61f23

Please sign in to comment.