Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! An SVG composer and parser.
2 //!
3 //! ## Example: Composing
4 //!
5 //! ```
6 //! # extern crate svg;
7 //! use svg::Document;
8 //! use svg::node::element::Path;
9 //! use svg::node::element::path::Data;
10 //!
11 //! # fn main() {
12 //! let data = Data::new()
13 //! .move_to((10, 10))
14 //! .line_by((0, 50))
15 //! .line_by((50, 0))
16 //! .line_by((0, -50))
17 //! .close();
18 //!
19 //! let path = Path::new()
20 //! .set("fill", "none")
21 //! .set("stroke", "black")
22 //! .set("stroke-width", 3)
23 //! .set("d", data);
24 //!
25 //! let document = Document::new()
26 //! .set("viewBox", (0, 0, 70, 70))
27 //! .add(path);
28 //!
29 //! svg::save("image.svg", &document).unwrap();
30 //! # std::fs::remove_file("image.svg");
31 //! # }
32 //! ```
33 //!
34 //! ## Example: Parsing
35 //!
36 //! ```
37 //! # extern crate svg;
38 //! use svg::node::element::path::{Command, Data};
39 //! use svg::node::element::tag::Path;
40 //! use svg::parser::Event;
41 //!
42 //! # fn main() {
43 //! let path = "image.svg";
44 //! # let path = "tests/fixtures/benton.svg";
45 //! let mut content = String::new();
46 //! for event in svg::open(path, &mut content).unwrap() {
47 //! match event {
48 //! Event::Tag(Path, _, attributes) => {
49 //! let data = attributes.get("d").unwrap();
50 //! let data = Data::parse(data).unwrap();
51 //! for command in data.iter() {
52 //! match command {
53 //! &Command::Move(..) => { /* … */ },
54 //! &Command::Line(..) => { /* … */ },
55 //! _ => {}
56 //! }
57 //! }
58 //! }
59 //! _ => {}
60 //! }
61 //! }
62 //! # }
63 //! ```
64
65 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#;
66 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#};
67 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#;
68
69 pub mod noderust-analyzer cargo svg 0.18.0 node/;
70 pub mod parserrust-analyzer cargo svg 0.18.0 parser/;
71
72 pub use craterust-analyzer cargo svg 0.18.0 crate/::noderust-analyzer cargo svg 0.18.0 node/::Noderust-analyzer cargo svg 0.18.0 node/Node#;
73 pub use craterust-analyzer cargo svg 0.18.0 crate/::parserrust-analyzer cargo svg 0.18.0 parser/::Parserrust-analyzer cargo svg 0.18.0 parser/Parser#;
74
75 /// A document.
76 pub type Documentrust-analyzer cargo svg 0.18.0 Document# = noderust-analyzer cargo svg 0.18.0 node/::elementrust-analyzer cargo svg 0.18.0 node/element/::SVGrust-analyzer cargo svg 0.18.0 node/element/SVG#;
77
78 /// Open a document.
79 pub fn openrust-analyzer cargo svg 0.18.0 open().<T>(path: T, content: &'_ mut Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Parserrust-analyzer cargo svg 0.18.0 parser/Parser#<'_>>
80 where
81 T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>,
82 {
83 let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
84 file.read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_string().(content)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
85 readrust-analyzer cargo svg 0.18.0 read().(content)
86 }
87
88 /// Read a document.
89 pub fn readrust-analyzer cargo svg 0.18.0 read().(content: &'_ str) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Parserrust-analyzer cargo svg 0.18.0 parser/Parser#<'_>> {
90 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Parserrust-analyzer cargo svg 0.18.0 parser/Parser#::newrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`]new().(content))
91 }
92
93 /// Save a document.
94 pub fn saverust-analyzer cargo svg 0.18.0 save().<T, U>(path: T, document: &U) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()>
95 where
96 T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>,
97 U: Noderust-analyzer cargo svg 0.18.0 node/Node#,
98 {
99 let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
100 file.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&document.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().())
101 }
102
103 /// Write a document.
104 pub fn writerust-analyzer cargo svg 0.18.0 write().<T, U>(mut target: T, document: &U) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()>
105 where
106 T: Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#,
107 U: Noderust-analyzer cargo svg 0.18.0 node/Node#,
108 {
109 target.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&document.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().into_bytesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]into_bytes().())
110 }
111
112 #[cfg(test)]
113 mod testsrust-analyzer cargo svg 0.18.0 tests/ {
114 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#;
115 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#;
116
117 use craterust-analyzer cargo svg 0.18.0 crate/::parserrust-analyzer cargo svg 0.18.0 parser/::{Eventrust-analyzer cargo svg 0.18.0 parser/Event#, Parserrust-analyzer cargo svg 0.18.0 parser/Parser#};
118
119 const TEST_PATHrust-analyzer cargo svg 0.18.0 tests/TEST_PATH.: &'static str = "tests/fixtures/benton.svg";
120
121 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
122 fn openrust-analyzer cargo svg 0.18.0 tests/open().() {
123 let mut content = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
124 exerciserust-analyzer cargo svg 0.18.0 tests/exercise().(craterust-analyzer cargo svg 0.18.0 crate/::openrust-analyzer cargo svg 0.18.0 open().(selfrust-analyzer cargo svg 0.18.0 tests/::TEST_PATHrust-analyzer cargo svg 0.18.0 tests/TEST_PATH., &mut content).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
125 }
126
127 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
128 fn readrust-analyzer cargo svg 0.18.0 tests/read().() {
129 let mut content = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]new().();
130 let mut file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(selfrust-analyzer cargo svg 0.18.0 tests/::TEST_PATHrust-analyzer cargo svg 0.18.0 tests/TEST_PATH.).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
131 file.read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_string().(&mut content).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
132
133 exerciserust-analyzer cargo svg 0.18.0 tests/exercise().(craterust-analyzer cargo svg 0.18.0 crate/::readrust-analyzer cargo svg 0.18.0 read().(&content).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().());
134 }
135
136 fn exerciserust-analyzer cargo svg 0.18.0 tests/exercise().<'l>(mut parser: Parserrust-analyzer cargo svg 0.18.0 parser/Parser#<'l>) {
137 macro_rules! testrust-analyzer cargo svg 0.18.0 tests/test!(
138 ($matcher:pat) => (match parser.next().unwrap() {
139 $matcher => {}
140 _ => unreachable!(),
141 });
142 );
143
144 testrust-analyzer cargo svg 0.18.0 tests/test!!(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Instructionrust-analyzer cargo svg 0.18.0 parser/Event#Instruction#(_));
145 testrust-analyzer cargo svg 0.18.0 tests/test!!(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Commentrust-analyzer cargo svg 0.18.0 parser/Event#Comment#(_));
146 testrust-analyzer cargo svg 0.18.0 tests/test!!(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Declarationrust-analyzer cargo svg 0.18.0 parser/Event#Declaration#(_));
147 testrust-analyzer cargo svg 0.18.0 tests/test!!(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Tagrust-analyzer cargo svg 0.18.0 parser/Event#Tag#("svg", _, _));
148 testrust-analyzer cargo svg 0.18.0 tests/test!!(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Tagrust-analyzer cargo svg 0.18.0 parser/Event#Tag#("path", _, _));
149 testrust-analyzer cargo svg 0.18.0 tests/test!!(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Tagrust-analyzer cargo svg 0.18.0 parser/Event#Tag#("path", _, _));
150 testrust-analyzer cargo svg 0.18.0 tests/test!!(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Tagrust-analyzer cargo svg 0.18.0 parser/Event#Tag#("path", _, _));
151 testrust-analyzer cargo svg 0.18.0 tests/test!!(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Tagrust-analyzer cargo svg 0.18.0 parser/Event#Tag#("path", _, _));
152 testrust-analyzer cargo svg 0.18.0 tests/test!!(Eventrust-analyzer cargo svg 0.18.0 parser/Event#::Tagrust-analyzer cargo svg 0.18.0 parser/Event#Tag#("svg", _, _));
153
154 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(parser.nextrust-analyzer cargo svg 0.18.0 parser/impl#[`Parser<'l>`][Iterator]next().().is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().());
155 }
156 }