Repository: GraphiteEditor/Graphite
File: node-graph/nodes/vector/src/generator_nodes.rs (Rust)

Code before:
	#[unit(" px")]
	#[default(50)]
	radius_1: f64,
	#[unit(" px")]
	#[default(25)]
	radius_2: f64,
) -> Table<Vector> {
	let points = sides.as_u64();
	let diameter: f64 = radius_1 * 2.;
	let inner_diameter = radius_2 * 2.;

	Table::new_from_element(Vector::from_subpath(subpath::Subpath::new_star_polygon(DVec2::splat(-diameter), points, diameter, inner_diameter)))
}

/// Generates a QR code from the input text.
#[node_macro::node(category("Vector: Shape"), name("QR Code"))]
fn qr_code(
	_: impl Ctx,
	_primary: (),
	#[default("https://graphite.art")] text: String,
	/// Error correction level, from low (0) to high (3).
	#[default(1)]
	error_correction: u32,
	#[default(false)] individual_squares: bool,
) -> Table<Vector> {
	let ecc = match error_correction.max(3) {
		0 => qrcodegen::QrCodeEcc::Low,
		1 => qrcodegen::QrCodeEcc::Medium,
		2 => qrcodegen::QrCodeEcc::Quartile,
		3 => qrcodegen::QrCodeEcc::High,
		_ => unreachable!(),
	};

	let Ok(qr_code) = qrcodegen::QrCode::encode_text(&text, ecc) else {
		return Table::default();
	};

	let size = qr_code.size() as usize;
	let mut vector = Vector::default();

	if individual_squares {
		for y in 0..size {
			for x in 0..size {
				if qr_code.get_module(x as i32, y as i32) {
					let corner1 = DVec2::new(x as f64, y as f64);
					let corner2 = corner1 + DVec2::splat(1.);
					vector.append_subpath(
						subpath::Subpath::from_anchors([corner1, DVec2::new(corner2.x, corner1.y), corner2, DVec2::new(corner1.x, corner2.y)], true),
						false,
					);
				}

Review comment (bug):
This should be

`let ecc = match error_correction.min(3)`

@TrueDoctor @timon-schelling @Keavon , let me know if I am wrong here , but since the intent here seems to be clamp the input to a valid range of 0 - 3 , 

by using max here , wouldn't it give wrong outputs

User Input: 0 (wants Low)
  → 0.max(3) = 3
**User gets HIGH instead of LOW**

User Input: 1 (wants Medium) [THE DEFAULT]
  → 1.max(3) = 3
**User gets HIGH instead of MEDIUM**

User Input: 2 (wants Quartile)
  → 2.max(3) = 3
**User gets HIGH instead of QUARTILE**

Code after:
	#[unit(" px")]
	#[default(50)]
	radius_1: f64,
	#[unit(" px")]
	#[default(25)]
	radius_2: f64,
) -> Table<Vector> {
	let points = sides.as_u64();
	let diameter: f64 = radius_1 * 2.;
	let inner_diameter = radius_2 * 2.;

	Table::new_from_element(Vector::from_subpath(subpath::Subpath::new_star_polygon(DVec2::splat(-diameter), points, diameter, inner_diameter)))
}

/// Generates a QR code from the input text.
#[node_macro::node(category("Vector: Shape"), name("QR Code"))]
fn qr_code(
	_: impl Ctx,
	_primary: (),
	#[default("https://graphite.art")] text: String,
	/// Error correction level, from low (0) to high (3).
	#[default(1)]
	error_correction: u32,
	#[default(false)] individual_squares: bool,
) -> Table<Vector> {
	let ecc = match error_correction.min(3) {
		0 => qrcodegen::QrCodeEcc::Low,
		1 => qrcodegen::QrCodeEcc::Medium,
		2 => qrcodegen::QrCodeEcc::Quartile,
		3 => qrcodegen::QrCodeEcc::High,
		_ => unreachable!(),
	};

	let Ok(qr_code) = qrcodegen::QrCode::encode_text(&text, ecc) else {
		return Table::default();
	};

	let size = qr_code.size() as usize;
	let mut vector = Vector::default();

	if individual_squares {
		for y in 0..size {
			for x in 0..size {
				if qr_code.get_module(x as i32, y as i32) {
					let corner1 = DVec2::new(x as f64, y as f64);
					let corner2 = corner1 + DVec2::splat(1.);
					vector.append_subpath(
						subpath::Subpath::from_anchors([corner1, DVec2::new(corner2.x, corner1.y), corner2, DVec2::new(corner1.x, corner2.y)], true),
						false,
					);
				}