Skip to content

Commit 44bed6b

Browse files
authored
feat: add docs for StringAdapter::new() (#4)
1 parent 5e911a6 commit 44bed6b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/lib.rs

+27
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,33 @@ pub struct StringAdapter {
2828
}
2929

3030
impl StringAdapter {
31+
/// # Examples
32+
///
33+
///```rust
34+
/// # use casbin::{CoreApi, DefaultModel, Enforcer, Result};
35+
/// # use string_adapter::StringAdapter;
36+
/// # #[tokio::main]
37+
/// # async fn main() -> Result<()> {
38+
/// # let m = DefaultModel::from_file("examples/rbac_model.conf").await?;
39+
/// let a = StringAdapter::new(
40+
/// r#"
41+
/// p, alice, data1, read
42+
/// p, bob, data2, write
43+
/// p, data2_admin, data2, read
44+
/// p, data2_admin, data2, write
45+
/// g, alice, data2_admin
46+
/// "#,
47+
/// );
48+
/// let e = Enforcer::new(m, a).await?;
49+
///
50+
/// assert_eq!(true, e.enforce(("alice", "data1", "read"))?);
51+
/// assert_eq!(true, e.enforce(("alice", "data2", "read"))?);
52+
/// assert_eq!(true, e.enforce(("bob", "data2", "write"))?);
53+
/// assert_eq!(false, e.enforce(("bob", "data1", "write"))?);
54+
/// # Ok(())
55+
/// # }
56+
///
57+
/// ```
3158
pub fn new(s: impl ToString) -> Self {
3259
Self {
3360
policy: s.to_string(),

0 commit comments

Comments
 (0)