Skip to content

Commit 5040c87

Browse files
bagel897eacodegen
andauthored
Serialization support (#11)
* Serialization support * Automated pre-commit update * error handling * Reformat, use an arc for buffers, add compression * Use the same build id across threads * Automated pre-commit update * Add logging around serialization * Make methods public * Fix test * make all tests pass --------- Co-authored-by: eacodegen <[email protected]>
1 parent 202d31b commit 5040c87

File tree

22 files changed

+484
-53
lines changed

22 files changed

+484
-53
lines changed

Cargo.lock

Lines changed: 281 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ edition = "2021"
77
clap = { version = "4.5.28", features = ["derive"] }
88
codegen-sdk-analyzer = { path = "codegen-sdk-analyzer" }
99
codegen-sdk-cst = { path = "codegen-sdk-cst" , features = ["typescript", "javascript", "tsx", "jsx"]}
10-
codegen-sdk-common = { path = "codegen-sdk-common" }
10+
codegen-sdk-common = { path = "codegen-sdk-common"}
1111
crossbeam = "0.8.4"
1212
glob = "0.3.2"
1313
env_logger = { workspace = true }
1414
log = { workspace = true }
1515
rayon = { workspace = true}
1616
sysinfo = "0.33.1"
17+
rkyv.workspace = true
18+
1719
[workspace]
1820
members = [
1921
"codegen-sdk-analyzer",
@@ -38,3 +40,4 @@ convert_case = "0.7.1"
3840
serde = { version = "1.0.217", features = ["derive"] }
3941
serde_json = "1.0.138"
4042
anyhow = { version = "1.0.95", features = ["backtrace"] }
43+
rkyv = { version = "0.8.10", features = ["bytes-1","pointer_width_64"] }

codegen-sdk-ast/src/main.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.

codegen-sdk-common/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ convert_case = { workspace = true }
2121
tree-sitter-query = {git = "https://github.com/tree-sitter-grammars/tree-sitter-query", optional = true}
2222
tree-sitter-language = "0.1.4"
2323
phf = { version = "0.11.3", features = ["macros"] }
24+
rkyv = { workspace = true }
25+
xdg = "2.5.2"
26+
base64 = "0.22.1"
27+
buildid = "1.0.3"
28+
sha2 = "0.10.8"
29+
zstd = { version = "0.13.2", features = ["zstdmt"] }
2430
[features]
2531
python = ["dep:tree-sitter-python"]
2632
json = ["dep:tree-sitter-json"]

codegen-sdk-common/src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::backtrace::Backtrace;
2+
23
use thiserror::Error;
34
#[derive(Debug, Error)]
45
pub enum ParseError {
@@ -27,4 +28,6 @@ pub enum ParseError {
2728
node_type: String,
2829
backtrace: Backtrace,
2930
},
31+
#[error("Failed to serialize: {0}")]
32+
Serialize(#[from] rkyv::rancor::Error),
3033
}

codegen-sdk-common/src/language.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use convert_case::{Case, Casing};
2+
use tree_sitter::Parser;
3+
14
use crate::{
25
errors::ParseError,
36
parser::{Node, parse_node_types},
47
};
5-
use convert_case::{Case, Casing};
6-
use tree_sitter::Parser;
78
pub struct Language {
89
pub name: &'static str,
910
pub struct_name: &'static str,

codegen-sdk-common/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ pub mod parser;
1313
#[macro_use]
1414
extern crate lazy_static;
1515
pub mod naming;
16+
mod point;
17+
pub use point::Point;
18+
pub mod serialize;

codegen-sdk-common/src/naming.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use std::debug_assert;
2+
13
use convert_case::{Case, Casing};
24
use phf::phf_map;
3-
use std::debug_assert;
45

56
static MAPPINGS: phf::Map<char, &'static str> = phf_map! {
67
'-' => "Minus",

codegen-sdk-common/src/point.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use rkyv::{Archive, Deserialize, Serialize};
2+
3+
#[derive(Debug, Clone, Copy, Eq, PartialEq, Archive, Deserialize, Serialize)]
4+
pub struct Point {
5+
pub row: usize,
6+
pub column: usize,
7+
}
8+
impl From<tree_sitter::Point> for Point {
9+
fn from(value: tree_sitter::Point) -> Self {
10+
Point {
11+
row: value.row,
12+
column: value.column,
13+
}
14+
}
15+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::{
2+
fs::File,
3+
io::{BufReader, BufWriter, Read},
4+
path::PathBuf,
5+
};
6+
7+
use base64::{Engine as _, engine::general_purpose::URL_SAFE};
8+
use bytes::Bytes;
9+
use rkyv::ser::writer::IoWriter;
10+
use sha2::{Digest, Sha256};
11+
use zstd::stream::AutoFinishEncoder;
12+
13+
use crate::ParseError;
14+
pub struct Cache {
15+
base_dir: PathBuf,
16+
build_id: String,
17+
}
18+
impl Cache {
19+
pub fn new() -> anyhow::Result<Self> {
20+
let xdg_dirs = xdg::BaseDirectories::with_prefix("codegen")?;
21+
let build_id = buildid::build_id().unwrap();
22+
let encoded_build_id = URL_SAFE.encode(build_id);
23+
xdg_dirs.create_cache_directory(&encoded_build_id)?;
24+
Ok(Self {
25+
base_dir: xdg_dirs.get_cache_home(),
26+
build_id: encoded_build_id,
27+
})
28+
}
29+
pub fn get_path(&self, path: &PathBuf) -> PathBuf {
30+
let mut hasher = Sha256::new();
31+
hasher.update(path.as_os_str().to_str().unwrap().as_bytes());
32+
let path_hash = hasher.finalize();
33+
self.base_dir
34+
.join(format!("{}/{}", self.build_id, URL_SAFE.encode(path_hash)))
35+
}
36+
pub fn read_entry(&self, path: &PathBuf) -> Result<Bytes, ParseError> {
37+
let file = File::open(path)?;
38+
let mut buf = Vec::new();
39+
let mut reader = zstd::Decoder::new(BufReader::new(file))?;
40+
reader.read_to_end(&mut buf)?;
41+
Ok(Bytes::from(buf))
42+
}
43+
pub fn get_writer(
44+
&self,
45+
path: &PathBuf,
46+
) -> Result<
47+
IoWriter<
48+
AutoFinishEncoder<
49+
BufWriter<File>,
50+
Box<dyn FnMut(Result<BufWriter<File>, std::io::Error>) + Send>,
51+
>,
52+
>,
53+
ParseError,
54+
> {
55+
let file = File::create(path)?;
56+
let writer = zstd::Encoder::new(BufWriter::new(file), 1)?.auto_finish();
57+
Ok(IoWriter::new(writer))
58+
}
59+
}

0 commit comments

Comments
 (0)