lichtung_plot/composition/
plan.rs1use std::{path::Path, sync::Arc};
4
5use crate::{
6 operation::{self, OperationOptions},
7 recipe::Recipe,
8 Error, Result,
9};
10
11use super::{ComposeOptions, Composite, CompositionSource, Placement};
12
13#[derive(Clone)]
14pub(super) struct PlanStep {
15 pub(super) items: Vec<Placement>,
16 pub(super) options: ComposeOptions,
17}
18
19impl PlanStep {
20 fn new(items: impl IntoIterator<Item = Placement>, options: ComposeOptions) -> Result<Self> {
21 let items = crate::data::source::collect_source(items.into_iter().map(|item| {
22 item.validate()?;
23 Ok(item)
24 }))?;
25 Ok(Self { items, options })
26 }
27}
28
29#[derive(Clone)]
34pub struct CompositionPlan {
35 pub(super) base: CompositionSource,
36 pub(super) steps: Arc<Vec<PlanStep>>,
37}
38
39impl CompositionPlan {
40 pub fn new(
42 base: impl TryInto<CompositionSource, Error: Into<Error>>,
43 items: impl IntoIterator<Item = Placement>,
44 ) -> Result<Self> {
45 Self::with_options(base, items, ComposeOptions::default())
46 }
47
48 pub fn with_options(
50 base: impl TryInto<CompositionSource, Error: Into<Error>>,
51 items: impl IntoIterator<Item = Placement>,
52 options: ComposeOptions,
53 ) -> Result<Self> {
54 Self::with_operation(base, items, options, &OperationOptions::default())
55 }
56
57 pub fn with_operation(
59 base: impl TryInto<CompositionSource, Error: Into<Error>>,
60 items: impl IntoIterator<Item = Placement>,
61 options: ComposeOptions,
62 operation: &OperationOptions,
63 ) -> Result<Self> {
64 operation::run(operation, || {
65 let base = base.try_into().map_err(Into::into)?;
66 let step = PlanStep::new(items, options)?;
67 Ok(Self::from_parts(base, vec![step]))
68 })
69 }
70
71 pub(super) fn from_parts(base: CompositionSource, steps: Vec<PlanStep>) -> Self {
72 debug_assert!(!steps.is_empty());
73 Self {
74 base,
75 steps: Arc::new(steps),
76 }
77 }
78
79 pub fn then(self, items: impl IntoIterator<Item = Placement>) -> Result<Self> {
81 self.then_with_options(items, ComposeOptions::default())
82 }
83
84 pub fn then_with_options(
86 self,
87 items: impl IntoIterator<Item = Placement>,
88 options: ComposeOptions,
89 ) -> Result<Self> {
90 self.then_with_operation(items, options, &OperationOptions::default())
91 }
92
93 pub fn then_with_operation(
95 mut self,
96 items: impl IntoIterator<Item = Placement>,
97 options: ComposeOptions,
98 operation: &OperationOptions,
99 ) -> Result<Self> {
100 operation::run(operation, || {
101 let step = PlanStep::new(items, options)?;
102 let steps = Arc::make_mut(&mut self.steps);
103 crate::data::source::reserve_source(steps, 1)?;
104 steps.push(step);
105 Ok(self)
106 })
107 }
108
109 pub fn compose(&self) -> Result<Composite> {
111 self.compose_with_operation(&OperationOptions::default())
112 }
113
114 pub fn compose_with_operation(&self, options: &OperationOptions) -> Result<Composite> {
116 operation::run(options, || self.compose_inner())
117 }
118
119 pub(crate) fn compose_inner(&self) -> Result<Composite> {
120 let mut steps = self.steps.iter();
121 let first = steps.next().expect("actual first step");
122 let mut composite = Composite::prepare(
123 self.base.clone(),
124 first.items.iter().cloned(),
125 first.options,
126 None,
127 )?;
128 for step in steps {
129 operation::checkpoint()?;
130 composite = Composite::prepare(
131 composite.into(),
132 step.items.iter().cloned(),
133 step.options,
134 None,
135 )?;
136 }
137 Ok(composite)
138 }
139
140 pub(crate) fn check_recipe_science(&self) -> Result<()> {
141 self.base.check_recipe_science(None)?;
142 for step in self.steps.iter() {
143 for item in &step.items {
144 operation::check()?;
145 item.check_recipe_science(None)?;
146 }
147 }
148 Ok(())
149 }
150
151 pub fn get_recipe(&self) -> Result<Recipe> {
153 self.get_recipe_with_operation(&OperationOptions::default())
154 }
155
156 pub fn get_recipe_with_operation(&self, options: &OperationOptions) -> Result<Recipe> {
158 operation::run(options, || self.capture_declaration())
159 }
160
161 pub fn recipe(&self, path: impl AsRef<Path>) -> Result<Recipe> {
163 self.recipe_with_operation(path, &OperationOptions::default())
164 }
165
166 pub fn recipe_with_operation(
168 &self,
169 path: impl AsRef<Path>,
170 options: &OperationOptions,
171 ) -> Result<Recipe> {
172 operation::run(options, || {
173 let recipe = self.capture_declaration()?;
174 recipe.write_document(path.as_ref())?;
175 Ok(recipe)
176 })
177 }
178}