Skip to content

Commit c2751cd

Browse files
authored
chore: add docs and publish ci (#17)
* chore: add docs * default for concatsource * fix doc test * chore: publish ci
1 parent 75d4f02 commit c2751cd

11 files changed

+230
-17
lines changed

Diff for: .github/workflows/Publish.yaml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Publish
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
jobs:
8+
publish:
9+
name: Publish
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
- uses: actions-rs/toolchain@v1
14+
with:
15+
toolchain: nightly-2022-08-06
16+
profile: minimal
17+
override: true
18+
- name: publish
19+
env:
20+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
21+
run: cargo publish -vv

Diff for: rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
max_width = 80
22
tab_spaces = 2
3+
format_code_in_doc_comments = true

Diff for: src/cached_source.rs

+35-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,38 @@ use crate::{
1010
MapOptions, Source, SourceMap,
1111
};
1212

13+
/// It tries to reused cached results from other methods to avoid calculations,
14+
/// usally used after modify is finished.
15+
///
16+
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#cachedsource).
17+
///
18+
/// ```
19+
/// use rspack_sources::{
20+
/// BoxSource, CachedSource, ConcatSource, MapOptions, OriginalSource,
21+
/// RawSource, Source, SourceMap,
22+
/// };
23+
///
24+
/// let mut concat = ConcatSource::new([
25+
/// Box::new(RawSource::from("Hello World\n".to_string())) as BoxSource,
26+
/// Box::new(OriginalSource::new(
27+
/// "console.log('test');\nconsole.log('test2');\n",
28+
/// "console.js",
29+
/// )),
30+
/// ]);
31+
/// concat.add(OriginalSource::new("Hello2\n", "hello.md"));
32+
///
33+
/// let cached = CachedSource::new(concat);
34+
///
35+
/// assert_eq!(
36+
/// cached.source(),
37+
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
38+
/// );
39+
/// // second time will be fast.
40+
/// assert_eq!(
41+
/// cached.source(),
42+
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
43+
/// );
44+
/// ```
1345
#[derive(Debug)]
1446
pub struct CachedSource<T> {
1547
inner: T,
@@ -19,6 +51,7 @@ pub struct CachedSource<T> {
1951
}
2052

2153
impl<T> CachedSource<T> {
54+
/// Create a [CachedSource] with the original [Source].
2255
pub fn new(inner: T) -> Self {
2356
Self {
2457
inner,
@@ -28,7 +61,8 @@ impl<T> CachedSource<T> {
2861
}
2962
}
3063

31-
pub fn inner(&self) -> &T {
64+
/// Get the original [Source].
65+
pub fn original(&self) -> &T {
3266
&self.inner
3367
}
3468
}

Diff for: src/concat_source.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,54 @@ use crate::{
1111
BoxSource, MapOptions, Source, SourceMap,
1212
};
1313

14-
#[derive(Debug)]
14+
/// Concatenate multiple [Source]s to a single [Source].
15+
///
16+
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#concatsource).
17+
///
18+
/// ```
19+
/// use rspack_sources::{
20+
/// BoxSource, ConcatSource, MapOptions, OriginalSource, RawSource, Source,
21+
/// SourceMap,
22+
/// };
23+
///
24+
/// let mut source = ConcatSource::new([
25+
/// Box::new(RawSource::from("Hello World\n".to_string())) as BoxSource,
26+
/// Box::new(OriginalSource::new(
27+
/// "console.log('test');\nconsole.log('test2');\n",
28+
/// "console.js",
29+
/// )),
30+
/// ]);
31+
/// source.add(OriginalSource::new("Hello2\n", "hello.md"));
32+
///
33+
/// assert_eq!(source.size(), 62);
34+
/// assert_eq!(
35+
/// source.source(),
36+
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
37+
/// );
38+
/// assert_eq!(
39+
/// source.map(&MapOptions::new(false)).unwrap(),
40+
/// SourceMap::from_json(
41+
/// r#"{
42+
/// "version": 3,
43+
/// "mappings": ";AAAA;AACA;ACDA",
44+
/// "names": [],
45+
/// "sources": ["console.js", "hello.md"],
46+
/// "sourcesContent": [
47+
/// "console.log('test');\nconsole.log('test2');\n",
48+
/// "Hello2\n"
49+
/// ]
50+
/// }"#,
51+
/// )
52+
/// .unwrap()
53+
/// );
54+
/// ```
55+
#[derive(Debug, Default)]
1556
pub struct ConcatSource {
1657
children: Vec<BoxSource>,
1758
}
1859

1960
impl ConcatSource {
61+
/// Create a [ConcatSource] with [Source]s.
2062
pub fn new<S, T>(sources: S) -> Self
2163
where
2264
T: Into<BoxSource>,
@@ -27,6 +69,7 @@ impl ConcatSource {
2769
}
2870
}
2971

72+
/// Add a [Source] to concat.
3073
pub fn add<S: Into<BoxSource>>(&mut self, item: S) {
3174
self.children.push(item.into());
3275
}

Diff for: src/error.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::{error, fmt, result};
22

3+
/// An alias for [std::result::Result<T, rspack_sources::Error>].
34
pub type Result<T> = result::Result<T, Error>;
45

6+
/// Error for this crate.
57
#[derive(Debug)]
68
pub enum Error {
79
/// a VLQ string was malformed and data was left over
@@ -10,12 +12,6 @@ pub enum Error {
1012
VlqNoValues,
1113
/// Overflow in Vlq handling
1214
VlqOverflow,
13-
/// a mapping segment had an unsupported size
14-
BadSegmentSize(u32),
15-
/// a reference to a non existing source was encountered
16-
BadSourceReference(u32),
17-
/// a reference to a non existing name was encountered
18-
BadNameReference(u32),
1915
/// a JSON parsing related failure
2016
BadJson(serde_json::Error),
2117
}
@@ -26,13 +22,6 @@ impl fmt::Display for Error {
2622
Error::VlqLeftover => write!(f, "leftover cur/shift in vlq decode"),
2723
Error::VlqNoValues => write!(f, "vlq decode did not produce any values"),
2824
Error::VlqOverflow => write!(f, "vlq decode caused an overflow"),
29-
Error::BadSegmentSize(size) => {
30-
write!(f, "got {} segments, expected 4 or 5", size)
31-
}
32-
Error::BadSourceReference(id) => {
33-
write!(f, "bad reference to source #{}", id)
34-
}
35-
Error::BadNameReference(id) => write!(f, "bad reference to name #{}", id),
3625
Error::BadJson(err) => write!(f, "bad json: {}", err),
3726
}
3827
}

Diff for: src/lib.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
//! Rusty [`webpack-sources`](https://github.com/webpack/webpack-sources) port.
2+
3+
#![forbid(unsafe_code)]
4+
#![deny(missing_docs)]
5+
16
mod cached_source;
27
mod concat_source;
38
mod error;
@@ -16,7 +21,8 @@ pub use original_source::OriginalSource;
1621
pub use raw_source::RawSource;
1722
pub use replace_source::ReplaceSource;
1823
pub use source::{
19-
BoxSource, MapOptions, Mapping, OriginalLocation, Source, SourceMap,
24+
BoxSource, MapOptions, Mapping, OriginalLocation, Source, SourceExt,
25+
SourceMap,
2026
};
2127
pub use source_map_source::{
2228
SourceMapSource, SourceMapSourceOptions, WithoutOriginalOptions,

Diff for: src/original_source.rs

+22
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,35 @@ use crate::{
1313
MapOptions, Source, SourceMap,
1414
};
1515

16+
/// Represents source code, it will create source map for the source code,
17+
/// but the source map is created by splitting the source code at typical
18+
/// statement borders (`;`, `{`, `}`).
19+
///
20+
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#originalsource).
21+
///
22+
/// ```
23+
/// use rspack_sources::{OriginalSource, MapOptions, Source};
24+
///
25+
/// let input = "if (hello()) { world(); hi(); there(); } done();\nif (hello()) { world(); hi(); there(); } done();";
26+
/// let source = OriginalSource::new(input, "file.js");
27+
/// assert_eq!(source.source(), input);
28+
/// assert_eq!(
29+
/// source.map(&MapOptions::default()).unwrap().mappings(),
30+
/// "AAAA,eAAe,SAAS,MAAM,WAAW;AACzC,eAAe,SAAS,MAAM,WAAW",
31+
/// );
32+
/// assert_eq!(
33+
/// source.map(&MapOptions::new(false)).unwrap().mappings(),
34+
/// "AAAA;AACA",
35+
/// );
36+
/// ```
1637
#[derive(Debug, Clone)]
1738
pub struct OriginalSource {
1839
value: String,
1940
name: String,
2041
}
2142

2243
impl OriginalSource {
44+
/// Create a [OriginalSource].
2345
pub fn new(value: impl Into<String>, name: impl Into<String>) -> Self {
2446
Self {
2547
value: value.into(),

Diff for: src/raw_source.rs

+16
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,29 @@ use crate::{
1111
MapOptions, Source, SourceMap,
1212
};
1313

14+
/// Represents source code without source map, it will not create source map for the source code.
15+
///
16+
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#rawsource).
17+
///
18+
/// ```
19+
/// use rspack_sources::{MapOptions, RawSource, Source};
20+
///
21+
/// let code = "some source code";
22+
/// let s = RawSource::from(code.to_string());
23+
/// assert_eq!(s.source(), code);
24+
/// assert_eq!(s.map(&MapOptions::default()), None);
25+
/// assert_eq!(s.size(), 16);
26+
/// ```
1427
#[derive(Debug, Clone)]
1528
pub enum RawSource {
29+
/// Represent buffer.
1630
Buffer(Vec<u8>),
31+
/// Represent string.
1732
Source(String),
1833
}
1934

2035
impl RawSource {
36+
/// Whether the [RawSource] represent a buffer.
2137
pub fn is_buffer(&self) -> bool {
2238
matches!(self, Self::Buffer(_))
2339
}

Diff for: src/replace_source.rs

+23
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ use crate::{
1212
MapOptions, Mapping, OriginalLocation, Source, SourceMap,
1313
};
1414

15+
/// Decorates a Source with replacements and insertions of source code,
16+
/// usally used in dependencies
17+
///
18+
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#replacesource).
19+
///
20+
/// ```
21+
/// use rspack_sources::{OriginalSource, ReplaceSource, Source};
22+
///
23+
/// let code = "hello world\n";
24+
/// let mut source = ReplaceSource::new(OriginalSource::new(code, "file.txt"));
25+
///
26+
/// source.insert(0, "start1\n", None);
27+
/// source.replace(0, 0, "start2\n", None);
28+
/// source.replace(999, 10000, "end2", None);
29+
/// source.insert(888, "end1\n", None);
30+
/// source.replace(0, 999, "replaced!\n", Some("whole"));
31+
///
32+
/// assert_eq!(source.source(), "start1\nstart2\nreplaced!\nend1\nend2");
33+
/// ```
1534
#[derive(Debug)]
1635
pub struct ReplaceSource<T> {
1736
inner: T,
@@ -27,17 +46,20 @@ struct Replacement {
2746
}
2847

2948
impl<T> ReplaceSource<T> {
49+
/// Create a [ReplaceSource].
3050
pub fn new(source: T) -> Self {
3151
Self {
3252
inner: source,
3353
replacements: Mutex::new(Vec::new()),
3454
}
3555
}
3656

57+
/// Get the original [Source].
3758
pub fn original(&self) -> &T {
3859
&self.inner
3960
}
4061

62+
/// Insert a content at start.
4163
pub fn insert(&mut self, start: u32, content: &str, name: Option<&str>) {
4264
self.replacements.lock().push(Replacement {
4365
start,
@@ -47,6 +69,7 @@ impl<T> ReplaceSource<T> {
4769
});
4870
}
4971

72+
/// Create a replacement with content at `[start, end)`.
5073
pub fn replace(
5174
&mut self,
5275
start: u32,

0 commit comments

Comments
 (0)