-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
205 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
[workspace] | ||
members = ["rinja", "rinja_derive", "rinja_parser", "testing"] | ||
members = ["rinja", "rinja_derive", "rinja_parser", "testing", "testing-alloc", "testing-no-std"] | ||
resolver = "2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../.rustfmt.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "rinja_testing-alloc" | ||
version = "0.3.5" | ||
authors = ["rinja-rs developers"] | ||
workspace = ".." | ||
edition = "2021" | ||
rust-version = "1.81" | ||
publish = false | ||
|
||
[dev-dependencies] | ||
rinja = { path = "../rinja", version = "0.3.5", default-features = false, features = ["alloc"] } | ||
|
||
assert_matches = "1.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE-APACHE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE-MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../_typos.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../deny.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#![no_std] | ||
|
||
use core::fmt; | ||
use core::str::Utf8Error; | ||
|
||
use assert_matches::assert_matches; | ||
use rinja::Template; | ||
|
||
#[test] | ||
fn hello_world() { | ||
#[derive(Template)] | ||
#[template( | ||
ext = "html", | ||
source = "Hello {%- if let Some(user) = user? -%} , {{ user }} {%- endif -%}!" | ||
)] | ||
struct Hello<'a> { | ||
user: Result<Option<&'a str>, CustomError>, | ||
} | ||
|
||
let mut buffer = [0; 32]; | ||
|
||
let tmpl = Hello { user: Ok(None) }; | ||
let mut cursor = Cursor::new(&mut buffer); | ||
assert_matches!(tmpl.render_into(&mut cursor), Ok(())); | ||
assert_eq!(cursor.finalize(), Ok("Hello!")); | ||
|
||
let tmpl = Hello { | ||
user: Ok(Some("user")), | ||
}; | ||
let mut cursor = Cursor::new(&mut buffer); | ||
assert_matches!(tmpl.render_into(&mut cursor), Ok(())); | ||
assert_eq!(cursor.finalize(), Ok("Hello, user!")); | ||
|
||
let tmpl = Hello { | ||
user: Ok(Some("<user>")), | ||
}; | ||
let mut cursor = Cursor::new(&mut buffer); | ||
assert_matches!(tmpl.render_into(&mut cursor), Ok(())); | ||
assert_eq!(cursor.finalize(), Ok("Hello, <user>!")); | ||
|
||
let tmpl = Hello { | ||
user: Err(CustomError), | ||
}; | ||
let mut cursor = Cursor::new(&mut buffer); | ||
let err = match tmpl.render_into(&mut cursor) { | ||
Err(rinja::Error::Custom(err)) => err, | ||
err => panic!("Expected `Err(Custom(_))`, got {err:#?}"), | ||
}; | ||
assert!(err.is::<CustomError>()); | ||
} | ||
|
||
struct Cursor<'a> { | ||
data: &'a mut [u8], | ||
pos: usize, | ||
} | ||
|
||
impl<'a> Cursor<'a> { | ||
fn new(data: &'a mut [u8]) -> Self { | ||
Self { data, pos: 0 } | ||
} | ||
|
||
fn finalize(self) -> Result<&'a str, Utf8Error> { | ||
core::str::from_utf8(&self.data[..self.pos]) | ||
} | ||
} | ||
|
||
impl fmt::Write for Cursor<'_> { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
let data = &mut self.data[self.pos..]; | ||
if data.len() >= s.len() { | ||
data[..s.len()].copy_from_slice(s.as_bytes()); | ||
self.pos += s.len(); | ||
Ok(()) | ||
} else { | ||
Err(fmt::Error) | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy)] | ||
struct CustomError; | ||
|
||
impl fmt::Display for CustomError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
fmt::Debug::fmt(self, f) | ||
} | ||
} | ||
|
||
impl core::error::Error for CustomError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../tomlfmt.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../.rustfmt.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "rinja_testing-no-std" | ||
version = "0.3.5" | ||
authors = ["rinja-rs developers"] | ||
workspace = ".." | ||
edition = "2021" | ||
rust-version = "1.81" | ||
publish = false | ||
|
||
[dev-dependencies] | ||
rinja = { path = "../rinja", version = "0.3.5", default-features = false } | ||
|
||
assert_matches = "1.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE-APACHE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../LICENSE-MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../_typos.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../deny.toml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#![no_std] | ||
|
||
use core::fmt; | ||
use core::str::Utf8Error; | ||
|
||
use assert_matches::assert_matches; | ||
use rinja::Template; | ||
|
||
#[test] | ||
fn hello_world() { | ||
#[derive(Template)] | ||
#[template( | ||
ext = "html", | ||
source = "Hello {%- if let Some(user) = user? -%} , {{ user }} {%- endif -%}!" | ||
)] | ||
struct Hello<'a> { | ||
user: Result<Option<&'a str>, fmt::Error>, | ||
} | ||
|
||
let mut buffer = [0; 32]; | ||
|
||
let tmpl = Hello { user: Ok(None) }; | ||
let mut cursor = Cursor::new(&mut buffer); | ||
assert_matches!(tmpl.render_into(&mut cursor), Ok(())); | ||
assert_eq!(cursor.finalize(), Ok("Hello!")); | ||
|
||
let tmpl = Hello { | ||
user: Ok(Some("user")), | ||
}; | ||
let mut cursor = Cursor::new(&mut buffer); | ||
assert_matches!(tmpl.render_into(&mut cursor), Ok(())); | ||
assert_eq!(cursor.finalize(), Ok("Hello, user!")); | ||
|
||
let tmpl = Hello { | ||
user: Ok(Some("<user>")), | ||
}; | ||
let mut cursor = Cursor::new(&mut buffer); | ||
assert_matches!(tmpl.render_into(&mut cursor), Ok(())); | ||
assert_eq!(cursor.finalize(), Ok("Hello, <user>!")); | ||
|
||
let tmpl = Hello { | ||
user: Err(fmt::Error), | ||
}; | ||
let mut cursor = Cursor::new(&mut buffer); | ||
assert_matches!(tmpl.render_into(&mut cursor), Err(rinja::Error::Fmt)); | ||
} | ||
|
||
struct Cursor<'a> { | ||
data: &'a mut [u8], | ||
pos: usize, | ||
} | ||
|
||
impl<'a> Cursor<'a> { | ||
fn new(data: &'a mut [u8]) -> Self { | ||
Self { data, pos: 0 } | ||
} | ||
|
||
fn finalize(self) -> Result<&'a str, Utf8Error> { | ||
core::str::from_utf8(&self.data[..self.pos]) | ||
} | ||
} | ||
|
||
impl fmt::Write for Cursor<'_> { | ||
fn write_str(&mut self, s: &str) -> fmt::Result { | ||
let data = &mut self.data[self.pos..]; | ||
if data.len() >= s.len() { | ||
data[..s.len()].copy_from_slice(s.as_bytes()); | ||
self.pos += s.len(); | ||
Ok(()) | ||
} else { | ||
Err(fmt::Error) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../tomlfmt.toml |