@@ -38,21 +38,23 @@ Each of the methods accepts two arguments a list of CSP directives you wish to u
3838** Example Code**
3939
4040``` rust
41- use csp_generator :: directives;
41+ use csp_generator :: { directives, Csp } ;
4242
43- let json = r # "
43+ fn main () {
44+ let json = r # "
4445 [
45- {"domain": "example.com", "directive ": ["connect-src"]},
46- {"domain": "test.com", "directive ": ["connect-src", "script-src"]}
46+ {"domain": "example.com", "directives ": ["connect-src"]},
47+ {"domain": "test.com", "directives ": ["connect-src", "script-src"]}
4748 ]
48- " # ;
49+ " # ;
4950
50- let csp : String = csp_generator :: enforce (directives :: get_directives (), json );
51+ let csp : Csp = csp_generator :: enforce (directives :: directives (), json );
5152
52- println! (" This is the CSP Header: {}" , csp . header);
53- // This is the CSP Header: Content-Security-Policy
54- println! (" This is the CSP Directives String: {}" , csp . csp);
55- // This is the CSP Directives String: script-src test.com; connect-src example.com test.com;
53+ println! (" This is the CSP Header: {}" , csp . header);
54+ // This is the CSP Header: Content-Security-Policy
55+ println! (" This is the CSP Directives String: {}" , csp . csp);
56+ // This is the CSP Directives String: script-src test.com; connect-src example.com test.com;
57+ }
5658```
5759
5860## JSON Configuration
@@ -88,7 +90,8 @@ You just need to comply with the `csp_generator::directives::GetDirectives` trai
8890This override will generate a CSP directive string which only makes use of the script-src and connect-src.
8991
9092``` rust
91- use crate :: GetDirectives ;
93+ use csp_generator :: directives :: GetDirectives ;
94+ use csp_generator :: Csp ;
9295
9396pub struct MyDirectives {
9497 list : Vec <String >,
@@ -101,14 +104,30 @@ impl GetDirectives for MyDirectives {
101104}
102105
103106// Construct MyDirectives Struct with the directives you wish to use.
104- pub fn my_directives () -> Directives {
107+ fn my_directives () -> MyDirectives {
105108 MyDirectives {
106109 list : vec! [
107110 String :: from (" script-src" ),
108111 String :: from (" connect-src" ),
109112 ],
110113 }
111114}
115+
116+ pub fn main () {
117+ let json = r # "
118+ [
119+ {"domain": "example.com", "directives": ["connect-src"]},
120+ {"domain": "test.com", "directives": ["connect-src", "img-src"]}
121+ ]
122+ " # ;
123+
124+ let csp : Csp = csp_generator :: report_only (my_directives (), json );
125+
126+ println! (" This is the CSP Header: {}" , csp . header);
127+ // This is the CSP Header: Content-Security-Policy-Report-Only
128+ println! (" This is the CSP Directives String: {}" , csp . csp);
129+ // This is the CSP Directives String: connect-src example.com test.com;
130+ }
112131```
113132
114133## License
0 commit comments