1#![warn(missing_docs)]
2#![warn(clippy::pedantic)]
3
4use crate::data::{Shaft, Treadle, YarnSequence};
15#[doc(inline)]
16pub use data::RiseSink;
17#[doc(inline)]
18pub use data::Threading;
19#[doc(inline)]
20pub use data::TieUp;
21#[doc(inline)]
22pub use data::TieUpCreate;
23#[doc(inline)]
24pub use data::TieUpKind;
25#[doc(inline)]
26pub use data::TreadlingInfo;
27#[doc(inline)]
28pub use data::Yarn;
29#[doc(inline)]
30pub use data::YarnPalette;
31use std::cmp::Ordering;
32use std::collections::HashSet;
33use std::ops::RangeBounds;
34use std::rc::Rc;
35
36pub mod data;
37
38#[derive(Clone, Debug, PartialEq)]
40pub struct Draft {
41 threading: Threading,
42 treadling: TreadlingInfo,
43 yarn_palette: YarnPalette,
44 weft_yarns: YarnSequence,
45 warp_yarns: YarnSequence,
46}
47
48pub enum WeavingAxis {
50 Threading,
52 Treadling,
54}
55
56impl Draft {
57 #[must_use]
59 pub fn new(shaft_count: u32, tie_up_create: TieUpCreate, rise_sink: RiseSink) -> Self {
60 Self {
61 threading: Threading::new(shaft_count, Vec::new()),
62 treadling: TreadlingInfo::new(shaft_count, tie_up_create, rise_sink),
63 yarn_palette: YarnPalette::default(),
64 weft_yarns: YarnSequence::default(),
65 warp_yarns: YarnSequence::default(),
66 }
67 }
68
69 #[must_use]
71 pub fn threading(&self) -> &Threading {
72 &self.threading
73 }
74
75 #[must_use]
77 pub fn treadling_info(&self) -> &TreadlingInfo {
78 &self.treadling
79 }
80
81 #[must_use]
83 pub fn tie_up(&self) -> &TieUpKind {
84 self.treadling.tie_up()
85 }
86
87 pub fn make_rising(&mut self) {
89 self.treadling.make_rising();
90 }
91
92 pub fn make_sinking(&mut self) {
94 self.treadling.make_sinking();
95 }
96
97 pub fn make_lift_plan(&mut self) -> bool {
100 self.treadling.make_lift_plan()
101 }
102
103 #[must_use]
105 pub fn max_shaft(&self) -> (WeavingAxis, u32) {
106 let treadling_max = self.treadling.max_shaft_used();
107 let threading_max = self.threading.max_shaft();
108
109 match treadling_max.cmp(&threading_max) {
110 Ordering::Less | Ordering::Equal => (WeavingAxis::Threading, threading_max),
111 Ordering::Greater => (WeavingAxis::Treadling, treadling_max),
112 }
113 }
114
115 pub fn set_shaft_count(&mut self, shaft_count: u32) -> Result<(), (WeavingAxis, u32)> {
124 let (axis, max) = self.max_shaft();
125 if shaft_count >= max {
126 self.treadling.set_shaft_count(shaft_count).unwrap();
127 self.threading.set_shaft_count(shaft_count).unwrap();
128 Ok(())
129 } else {
130 Err((axis, max))
131 }
132 }
133
134 pub fn splice_threading<R>(&mut self, range: R, replace_with: &[u32]) -> Result<Vec<u32>, usize>
139 where
140 R: RangeBounds<usize>,
141 {
142 self.threading.splice(range, replace_with)
143 }
144
145 pub fn push_threading(&mut self, shaft: u32) -> Result<(), u32> {
150 self.threading.push(shaft)
151 }
152
153 pub fn insert_threading(&mut self, shaft: Shaft, index: usize) -> Result<(), Shaft> {
161 self.threading.insert(shaft, index)
162 }
163
164 pub fn try_insert_threading(
169 &mut self,
170 shaft: Shaft,
171 index: usize,
172 ) -> Result<Result<(), Shaft>, usize> {
173 self.threading.try_insert(shaft, index)
174 }
175
176 pub fn remove_threading(&mut self, index: usize) -> Shaft {
181 self.threading.remove(index)
182 }
183
184 #[must_use]
186 pub fn get_from_threading(&self, index: usize) -> Option<&u32> {
187 self.threading.get(index)
188 }
189
190 pub fn put_threading(&mut self, index: usize, shaft: Shaft) -> Shaft {
195 self.threading.put(index, shaft)
196 }
197
198 pub fn try_put_threading(
204 &mut self,
205 index: usize,
206 shaft: Shaft,
207 ) -> Result<Option<Shaft>, usize> {
208 self.threading.try_put(index, shaft)
209 }
210
211 pub fn flip_threading_vert(&mut self) {
213 self.threading.flip_vertical();
214 }
215
216 pub fn mirror_threading(&mut self) {
218 self.threading.mirror();
219 }
220
221 pub fn flip_threading_horiz(&mut self) {
223 self.threading.reverse();
224 }
225
226 pub fn push_single_treadling(&mut self, treadle: u32) -> Result<(), u32> {
231 self.treadling.push_single(treadle)
232 }
233
234 pub fn push_treadling(&mut self, treadles: HashSet<u32>) -> Result<(), u32> {
239 self.treadling.push(treadles)
240 }
241
242 pub fn toggle_treadle(&mut self, index: usize, treadle: Treadle) -> Result<bool, u32> {
249 self.treadling.toggle_treadle(index, treadle)
250 }
251
252 pub fn insert_treadle(&mut self, index: usize, treadles: HashSet<u32>) -> Result<(), u32> {
259 self.treadling.insert(index, treadles)
260 }
261
262 pub fn splice_treadling<R>(
269 &mut self,
270 range: R,
271 replace_with: Vec<HashSet<u32>>,
272 ) -> Result<Vec<HashSet<u32>>, u32>
273 where
274 R: RangeBounds<usize>,
275 {
276 self.treadling.splice(range, replace_with)
277 }
278 pub fn put_treadling(
286 &mut self,
287 index: usize,
288 treadles: HashSet<u32>,
289 ) -> Result<Option<HashSet<u32>>, u32> {
290 self.treadling.put(index, treadles)
291 }
292
293 #[must_use]
295 pub fn yarn_palette(&self) -> &YarnPalette {
296 &self.yarn_palette
297 }
298
299 #[must_use]
301 pub fn weft_yarns(&self) -> &YarnSequence {
302 &self.weft_yarns
303 }
304
305 #[must_use]
307 pub fn warp_yarns(&self) -> &YarnSequence {
308 &self.warp_yarns
309 }
310
311 pub fn set_weft_yarn_repeat<T>(&mut self, yarns: T)
313 where
314 T: IntoIterator<Item = Yarn>,
315 {
316 let yarns = self.yarn_palette.use_yarns(yarns);
317 self.weft_yarns.set_repeat(&yarns);
318 }
319
320 pub fn set_weft_yarn_offset(&mut self, offset: usize) {
322 self.weft_yarns.set_offset(offset);
323 }
324
325 pub fn set_weft_yarn(&mut self, index: usize, yarn: Yarn) {
327 let yarn = self.yarn_palette.use_yarn(yarn);
328 self.weft_yarns.set_yarn(index, yarn);
329 }
330
331 #[must_use]
333 pub fn try_get_weft_yarn(&self, index: usize) -> Option<&Rc<Yarn>> {
334 self.weft_yarns.try_get(index)
335 }
336
337 #[must_use]
341 pub fn get_weft_yarn(&self, index: usize) -> &Rc<Yarn> {
342 self.weft_yarns.get(index)
343 }
344
345 pub fn set_warp_yarn_repeat<T>(&mut self, yarns: T)
347 where
348 T: IntoIterator<Item = Yarn>,
349 {
350 let yarns = self.yarn_palette.use_yarns(yarns);
351 self.warp_yarns.set_repeat(&yarns);
352 }
353
354 pub fn set_warp_yarn_offset(&mut self, offset: usize) {
356 self.warp_yarns.set_offset(offset);
357 }
358
359 pub fn set_warp_yarn(&mut self, index: usize, yarn: Yarn) {
361 let yarn = self.yarn_palette.use_yarn(yarn);
362 self.warp_yarns.set_yarn(index, yarn);
363 }
364
365 #[must_use]
367 pub fn try_get_warp_yarn(&self, index: usize) -> Option<&Rc<Yarn>> {
368 self.warp_yarns.try_get(index)
369 }
370
371 #[must_use]
375 pub fn get_warp_yarn(&self, index: usize) -> &Rc<Yarn> {
376 self.warp_yarns.get(index)
377 }
378}